using UnityEngine; using UnityEngine.UI; using System; using System.Collections; public class LightwallUI : Image { #region 变量 public ParticleSystem ParticleSystem { get { if (_ParticleSystem == null) { _ParticleSystem = GetComponent(); } return _ParticleSystem; } set { _ParticleSystem = value; } } private ParticleSystem _ParticleSystem; #endregion private void Update() { SetAllDirty(); } public void Begin() { if (ParticleSystem == null) { return; } 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); } Vector3 pos = particle.position; pos.x *= 140; float spriteScale = 25f; float particalScale = particle.GetCurrentSize(ParticleSystem) * 15f; vertices[0].position = pos + 0.5f*(1+particalScale)*upAxis*sprite.rect.height/spriteScale; vertices[1].position = pos + 0.5f * (1 + particalScale) * upAxis * sprite.rect.height / spriteScale + 5 * (1 + particalScale) * rightAxis * sprite.rect.width / spriteScale; vertices[2].position = pos - 0.5f * (1 + particalScale) * upAxis * sprite.rect.height / spriteScale + 5 * (1 + particalScale) * rightAxis * sprite.rect.width / spriteScale; vertices[3].position = pos - 0.5f * (1 + particalScale) * upAxis * sprite.rect.height / spriteScale; 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); } }