PointsUI.cs 2.8 KB

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