UIPartical.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. Animator.SetTrigger("Play");
  46. ParticleSystem.Play();
  47. }
  48. protected override void OnPopulateMesh(VertexHelper toFill)
  49. {
  50. if (ParticleSystem == null)
  51. {
  52. Debug.Log("There is no ParticalSystem");
  53. return;
  54. }
  55. toFill.Clear();
  56. ParticleSystem.Particle[] particles = new ParticleSystem.Particle[ParticleSystem.particleCount];
  57. ParticleSystem.GetParticles(particles);
  58. for (int i = 0; i < particles.Length; i++)
  59. {
  60. DrawParticle(toFill, particles[i]);
  61. }
  62. }
  63. protected void DrawParticle(VertexHelper toFill, ParticleSystem.Particle particle)
  64. {
  65. UIVertex[] vertices = new UIVertex[4];
  66. Vector3 upAxis = new Vector3(0, 1, 0);
  67. Vector3 rightAxis = new Vector3(1, 0, 0);
  68. if (Math.Abs(particle.rotation) > 0.0005f)
  69. {
  70. particle.rotation *= Mathf.Deg2Rad;
  71. upAxis = new Vector3(-Mathf.Sin(particle.rotation), Mathf.Cos(particle.rotation), 0);
  72. rightAxis = new Vector3(Mathf.Cos(particle.rotation), Mathf.Sin(particle.rotation), 0);
  73. }
  74. vertices[0].position = particle.position + upAxis * sprite.rect.height / 50 - rightAxis * sprite.rect.width / 50;
  75. vertices[1].position = particle.position + upAxis * sprite.rect.height / 50 + rightAxis * sprite.rect.width / 50;
  76. vertices[2].position = particle.position - upAxis * sprite.rect.height / 50 + rightAxis * sprite.rect.width / 50;
  77. vertices[3].position = particle.position - upAxis * sprite.rect.height / 50 - rightAxis * sprite.rect.width / 50;
  78. vertices[0].color = particle.GetCurrentColor(ParticleSystem);
  79. vertices[1].color = particle.GetCurrentColor(ParticleSystem);
  80. vertices[2].color = particle.GetCurrentColor(ParticleSystem);
  81. vertices[3].color = particle.GetCurrentColor(ParticleSystem);
  82. vertices[0].uv0 = new Vector2(0, 1);
  83. vertices[1].uv0 = new Vector2(1, 1);
  84. vertices[2].uv0 = new Vector2(1, 0);
  85. vertices[3].uv0 = new Vector2(0, 0);
  86. toFill.AddUIVertexQuad(vertices);
  87. }
  88. }