将Particle转成UGUI
在unity官方论坛看到的一个解决方案,可以将Particle直接转换成CanvasRenderer元素显示。
新建一个UIParticleSystem.cs脚本,将以下代码复制进去:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; [ExecuteInEditMode] [RequireComponent( typeof (CanvasRenderer))] [RequireComponent( typeof (ParticleSystem))] public class UIParticleSystem : MaskableGraphic { public Texture particleTexture; public Sprite particleSprite; private Transform _transform; private ParticleSystem _particleSystem; private ParticleSystem.Particle[] _particles; private UIVertex[] _quad = new UIVertex[4]; private Vector4 _uv = Vector4.zero; private ParticleSystem.TextureSheetAnimationModule _textureSheetAnimation; private int _textureSheetAnimationFrames; private Vector2 _textureSheedAnimationFrameSize; public override Texture mainTexture { get { if (particleTexture) { return particleTexture; } if (particleSprite) { return particleSprite.texture; } return null ; } } protected bool Initialize() { // initialize members if (_transform == null ) { _transform = transform; } // prepare particle system ParticleSystemRenderer renderer = GetComponent<ParticleSystemRenderer>(); bool setParticleSystemMaterial = false ; if (_particleSystem == null ) { _particleSystem = GetComponent<ParticleSystem>(); if (_particleSystem == null ) { return false ; } // get current particle texture if (renderer == null ) { renderer = _particleSystem.gameObject.AddComponent<ParticleSystemRenderer>(); } Material currentMaterial = renderer.sharedMaterial; if (currentMaterial && currentMaterial.HasProperty( "_MainTex" )) { particleTexture = currentMaterial.mainTexture; } // automatically set scaling _particleSystem.scalingMode = ParticleSystemScalingMode.Local; _particles = null ; setParticleSystemMaterial = true ; } else { if (Application.isPlaying) { setParticleSystemMaterial = (renderer.material == null ); } #if UNITY_EDITOR else { setParticleSystemMaterial = (renderer.sharedMaterial == null ); } #endif } // automatically set material to UI/Particles/Hidden shader, and get previous texture if (setParticleSystemMaterial) { Material material = new Material(Shader.Find( "UI/Particles/Hidden" )); if (Application.isPlaying) { renderer.material = material; } #if UNITY_EDITOR else { material.hideFlags = HideFlags.DontSave; renderer.sharedMaterial = material; } #endif } // prepare particles array if (_particles == null ) { _particles = new ParticleSystem.Particle[_particleSystem.maxParticles]; } // prepare uvs if (particleTexture) { _uv = new Vector4(0, 0, 1, 1); } else if (particleSprite) { _uv = UnityEngine.Sprites.DataUtility.GetOuterUV(particleSprite); } // prepare texture sheet animation _textureSheetAnimation = _particleSystem.textureSheetAnimation; _textureSheetAnimationFrames = 0; _textureSheedAnimationFrameSize = Vector2.zero; if (_textureSheetAnimation.enabled) { _textureSheetAnimationFrames = _textureSheetAnimation.numTilesX * _textureSheetAnimation.numTilesY; _textureSheedAnimationFrameSize = new Vector2(1f / _textureSheetAnimation.numTilesX, 1f / _textureSheetAnimation.numTilesY); } return true ; } protected override void Awake() { base .Awake(); if (!Initialize()) { enabled = false ; } } protected override void OnPopulateMesh(VertexHelper vh) { #if UNITY_EDITOR if (!Application.isPlaying) { if (!Initialize()) { return ; } } #endif // prepare vertices vh.Clear(); if (!gameObject.activeInHierarchy) { return ; } // iterate through current particles int count = _particleSystem.GetParticles(_particles); for ( int i = 0; i < count; ++i) { ParticleSystem.Particle particle = _particles[i]; // get particle properties Vector2 position = (_particleSystem.simulationSpace == ParticleSystemSimulationSpace.Local ? particle.position : _transform.InverseTransformPoint(particle.position)); float rotation = -particle.rotation * Mathf.Deg2Rad; float rotation90 = rotation + Mathf.PI / 2; Color32 color = particle.GetCurrentColor(_particleSystem); float size = particle.GetCurrentSize(_particleSystem) * 0.5f; // apply scale if (_particleSystem.scalingMode == ParticleSystemScalingMode.Shape) { position /= canvas.scaleFactor; } // apply texture sheet animation Vector4 particleUV = _uv; if (_textureSheetAnimation.enabled) { float frameProgress = 1 - (particle.lifetime / particle.startLifetime); // float frameProgress = textureSheetAnimation.frameOverTime.curveMin.Evaluate(1 - (particle.lifetime / particle.startLifetime)); // TODO - once Unity allows MinMaxCurve reading frameProgress = Mathf.Repeat(frameProgress * _textureSheetAnimation.cycleCount, 1); int frame = 0; switch (_textureSheetAnimation.animation) { case ParticleSystemAnimationType.WholeSheet: frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimationFrames); break ; case ParticleSystemAnimationType.SingleRow: frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimation.numTilesX); int row = _textureSheetAnimation.rowIndex; // if (textureSheetAnimation.useRandomRow) { // FIXME - is this handled internally by rowIndex? // row = Random.Range(0, textureSheetAnimation.numTilesY, using: particle.randomSeed); // } frame += row * _textureSheetAnimation.numTilesX; break ; } frame %= _textureSheetAnimationFrames; particleUV.x = (frame % _textureSheetAnimation.numTilesX) * _textureSheedAnimationFrameSize.x; particleUV.y = Mathf.FloorToInt(frame / _textureSheetAnimation.numTilesX) * _textureSheedAnimationFrameSize.y; particleUV.z = particleUV.x + _textureSheedAnimationFrameSize.x; particleUV.w = particleUV.y + _textureSheedAnimationFrameSize.y; } _quad[0] = UIVertex.simpleVert; _quad[0].color = color; _quad[0].uv0 = new Vector2(particleUV.x, particleUV.y); _quad[1] = UIVertex.simpleVert; _quad[1].color = color; _quad[1].uv0 = new Vector2(particleUV.x, particleUV.w); _quad[2] = UIVertex.simpleVert; _quad[2].color = color; _quad[2].uv0 = new Vector2(particleUV.z, particleUV.w); _quad[3] = UIVertex.simpleVert; _quad[3].color = color; _quad[3].uv0 = new Vector2(particleUV.z, particleUV.y); if (rotation == 0) { // no rotation Vector2 corner1 = new Vector2(position.x - size, position.y - size); Vector2 corner2 = new Vector2(position.x + size, position.y + size); _quad[0].position = new Vector2(corner1.x, corner1.y); _quad[1].position = new Vector2(corner1.x, corner2.y); _quad[2].position = new Vector2(corner2.x, corner2.y); _quad[3].position = new Vector2(corner2.x, corner1.y); } else { // apply rotation Vector2 right = new Vector2(Mathf.Cos(rotation), Mathf.Sin(rotation)) * size; Vector2 up = new Vector2(Mathf.Cos(rotation90), Mathf.Sin(rotation90)) * size; _quad[0].position = position - right - up; _quad[1].position = position - right + up; _quad[2].position = position + right + up; _quad[3].position = position + right - up; } vh.AddUIVertexQuad(_quad); } } void Update() { if (Application.isPlaying) { // unscaled animation within UI _particleSystem.Simulate(Time.unscaledDeltaTime, false , false ); SetAllDirty(); } } #if UNITY_EDITOR void LateUpdate() { if (!Application.isPlaying) { SetAllDirty(); } } #endif } |
脚本依赖ParticleSystem控件,只能挂载在Paricle物体上。新建一个ParticleSystem将脚本拖上去就能用了!
https://blog.csdn.net/dark00800/article/details/73729947
https://blog.csdn.net/qq_14914623
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)