using UnityEngine; using UnityEngine.UI; using System; using System.Collections; public class UIPartical : Image { #region 变量 public Animator Animator { get { if (_Animator == null) { _Animator = GetComponentInParent(); } return _Animator; } set { _Animator = value; } } public ParticleSystem ParticleSystem { get { if (_ParticleSystem == null) { _ParticleSystem = GetComponent(); } return _ParticleSystem; } set { _ParticleSystem = value; } } private Animator _Animator; private ParticleSystem _ParticleSystem; #endregion private void Update() { SetAllDirty(); } public void Begin() { if (Animator == null || ParticleSystem == null) { return; } if (Animator.isInitialized) { Animator.SetTrigger("Play"); } ParticleSystem.Play(); } protected override void OnPopulateMesh(VertexHelper toFill) { if (ParticleSystem == null) { Debug.Log("There is no ParticalSystem"); return; } toFill.Clear(); ParticleSystem.Particle[] particles = new ParticleSystem.Particle[ParticleSystem.particleCount]; ParticleSystem.GetParticles(particles); for (int i = 0; i < particles.Length; i++) { DrawParticle(toFill, particles[i]); } } protected void DrawParticle(VertexHelper toFill, ParticleSystem.Particle particle) { UIVertex[] vertices = new UIVertex[4]; Vector3 upAxis = new Vector3(0, 1, 0); Vector3 rightAxis = new Vector3(1, 0, 0); if (Math.Abs(particle.rotation) > 0.0005f) { particle.rotation *= Mathf.Deg2Rad; upAxis = new Vector3(-Mathf.Sin(particle.rotation), Mathf.Cos(particle.rotation), 0); rightAxis = new Vector3(Mathf.Cos(particle.rotation), Mathf.Sin(particle.rotation), 0); } vertices[0].position = particle.position + upAxis * sprite.rect.height / 50 - rightAxis * sprite.rect.width / 50; vertices[1].position = particle.position + upAxis * sprite.rect.height / 50 + rightAxis * sprite.rect.width / 50; vertices[2].position = particle.position - upAxis * sprite.rect.height / 50 + rightAxis * sprite.rect.width / 50; vertices[3].position = particle.position - upAxis * sprite.rect.height / 50 - rightAxis * sprite.rect.width / 50; vertices[0].color = particle.GetCurrentColor(ParticleSystem); vertices[1].color = particle.GetCurrentColor(ParticleSystem); vertices[2].color = particle.GetCurrentColor(ParticleSystem); vertices[3].color = particle.GetCurrentColor(ParticleSystem); vertices[0].uv0 = new Vector2(0, 1); vertices[1].uv0 = new Vector2(1, 1); vertices[2].uv0 = new Vector2(1, 0); vertices[3].uv0 = new Vector2(0, 0); toFill.AddUIVertexQuad(vertices); } }