UIPartical.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. using System.Collections;
  5. public class UIPartical : Image
  6. {
  7. #region 变量
  8. public Animator Animator
  9. {
  10. get
  11. {
  12. if (_Animator == null)
  13. {
  14. _Animator = GetComponentInParent<Animator>();
  15. }
  16. return _Animator;
  17. }
  18. set { _Animator = value; }
  19. }
  20. public ParticleSystem ParticleSystem
  21. {
  22. get
  23. {
  24. if (_ParticleSystem == null)
  25. {
  26. _ParticleSystem = GetComponent<ParticleSystem>();
  27. }
  28. return _ParticleSystem;
  29. }
  30. set { _ParticleSystem = value; }
  31. }
  32. private Animator _Animator;
  33. private ParticleSystem _ParticleSystem;
  34. #endregion
  35. private void Update()
  36. {
  37. SetAllDirty();
  38. }
  39. public void Begin()
  40. {
  41. if (Animator == null || ParticleSystem == null)
  42. {
  43. return;
  44. }
  45. if (Animator.isInitialized)
  46. {
  47. Animator.SetTrigger("Play");
  48. }
  49. ParticleSystem.Play();
  50. }
  51. protected override void OnPopulateMesh(VertexHelper toFill)
  52. {
  53. if (ParticleSystem == null)
  54. {
  55. Debug.Log("There is no ParticalSystem");
  56. return;
  57. }
  58. toFill.Clear();
  59. ParticleSystem.Particle[] particles = new ParticleSystem.Particle[ParticleSystem.particleCount];
  60. ParticleSystem.GetParticles(particles);
  61. for (int i = 0; i < particles.Length; i++)
  62. {
  63. DrawParticle(toFill, particles[i]);
  64. }
  65. }
  66. protected void DrawParticle(VertexHelper toFill, ParticleSystem.Particle particle)
  67. {
  68. UIVertex[] vertices = new UIVertex[4];
  69. Vector3 upAxis = new Vector3(0, 1, 0);
  70. Vector3 rightAxis = new Vector3(1, 0, 0);
  71. if (Math.Abs(particle.rotation) > 0.0005f)
  72. {
  73. particle.rotation *= Mathf.Deg2Rad;
  74. upAxis = new Vector3(-Mathf.Sin(particle.rotation), Mathf.Cos(particle.rotation), 0);
  75. rightAxis = new Vector3(Mathf.Cos(particle.rotation), Mathf.Sin(particle.rotation), 0);
  76. }
  77. vertices[0].position = particle.position + upAxis * sprite.rect.height / 50 - rightAxis * sprite.rect.width / 50;
  78. vertices[1].position = particle.position + upAxis * sprite.rect.height / 50 + rightAxis * sprite.rect.width / 50;
  79. vertices[2].position = particle.position - upAxis * sprite.rect.height / 50 + rightAxis * sprite.rect.width / 50;
  80. vertices[3].position = particle.position - upAxis * sprite.rect.height / 50 - rightAxis * sprite.rect.width / 50;
  81. vertices[0].color = particle.GetCurrentColor(ParticleSystem);
  82. vertices[1].color = particle.GetCurrentColor(ParticleSystem);
  83. vertices[2].color = particle.GetCurrentColor(ParticleSystem);
  84. vertices[3].color = particle.GetCurrentColor(ParticleSystem);
  85. vertices[0].uv0 = new Vector2(0, 1);
  86. vertices[1].uv0 = new Vector2(1, 1);
  87. vertices[2].uv0 = new Vector2(1, 0);
  88. vertices[3].uv0 = new Vector2(0, 0);
  89. toFill.AddUIVertexQuad(vertices);
  90. }
  91. }