UIParticleSystem_Demo.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /// Credit glennpow, Zarlang
  2. /// Sourced from - http://forum.unity3d.com/threads/free-script-particle-systems-in-ui-screen-space-overlay.406862/
  3. /// Updated by Zarlang with a more robust implementation, including TextureSheet annimation support
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Coffee.UIExtensions.Demos
  7. {
  8. #if UNITY_5_3_OR_NEWER
  9. [ExecuteInEditMode]
  10. [RequireComponent(typeof(CanvasRenderer), typeof(ParticleSystem))]
  11. public class UIParticleSystem_Demo : MaskableGraphic
  12. {
  13. [Tooltip("Having this enabled run the system in LateUpdate rather than in Update making it faster but less precise (more clunky)")]
  14. public bool fixedTime = true;
  15. private Transform _transform;
  16. private ParticleSystem pSystem;
  17. private ParticleSystem.Particle[] particles;
  18. private UIVertex[] _quad = new UIVertex[4];
  19. private Vector4 imageUV = Vector4.zero;
  20. private ParticleSystem.TextureSheetAnimationModule textureSheetAnimation;
  21. private int textureSheetAnimationFrames;
  22. private Vector2 textureSheetAnimationFrameSize;
  23. private ParticleSystemRenderer pRenderer;
  24. private Material currentMaterial;
  25. private Texture currentTexture;
  26. #if UNITY_5_5_OR_NEWER
  27. private ParticleSystem.MainModule mainModule;
  28. #endif
  29. public override Texture mainTexture
  30. {
  31. get
  32. {
  33. return currentTexture;
  34. }
  35. }
  36. protected bool Initialize()
  37. {
  38. // initialize members
  39. if (_transform == null)
  40. {
  41. _transform = transform;
  42. }
  43. if (pSystem == null)
  44. {
  45. pSystem = GetComponent<ParticleSystem>();
  46. if (pSystem == null)
  47. {
  48. return false;
  49. }
  50. #if UNITY_5_5_OR_NEWER
  51. mainModule = pSystem.main;
  52. if (pSystem.main.maxParticles > 14000)
  53. {
  54. mainModule.maxParticles = 14000;
  55. }
  56. #else
  57. if (pSystem.maxParticles > 14000)
  58. pSystem.maxParticles = 14000;
  59. #endif
  60. pRenderer = pSystem.GetComponent<ParticleSystemRenderer>();
  61. if (pRenderer != null)
  62. pRenderer.enabled = false;
  63. //Shader foundShader = Shader.Find("UI Extensions/Particles/Additive");
  64. //Material pMaterial = new Material(foundShader);
  65. //if (material == null)
  66. //material = pMaterial;
  67. currentMaterial = material;
  68. if (currentMaterial && currentMaterial.HasProperty("_MainTex"))
  69. {
  70. currentTexture = currentMaterial.mainTexture;
  71. if (currentTexture == null)
  72. currentTexture = Texture2D.whiteTexture;
  73. }
  74. material = currentMaterial;
  75. // automatically set scaling
  76. #if UNITY_5_5_OR_NEWER
  77. mainModule.scalingMode = ParticleSystemScalingMode.Hierarchy;
  78. #else
  79. pSystem.scalingMode = ParticleSystemScalingMode.Hierarchy;
  80. #endif
  81. particles = null;
  82. }
  83. #if UNITY_5_5_OR_NEWER
  84. if (particles == null)
  85. particles = new ParticleSystem.Particle[pSystem.main.maxParticles];
  86. #else
  87. if (particles == null)
  88. particles = new ParticleSystem.Particle[pSystem.maxParticles];
  89. #endif
  90. imageUV = new Vector4(0, 0, 1, 1);
  91. // prepare texture sheet animation
  92. textureSheetAnimation = pSystem.textureSheetAnimation;
  93. textureSheetAnimationFrames = 0;
  94. textureSheetAnimationFrameSize = Vector2.zero;
  95. if (textureSheetAnimation.enabled)
  96. {
  97. textureSheetAnimationFrames = textureSheetAnimation.numTilesX * textureSheetAnimation.numTilesY;
  98. textureSheetAnimationFrameSize = new Vector2(1f / textureSheetAnimation.numTilesX, 1f / textureSheetAnimation.numTilesY);
  99. }
  100. return true;
  101. }
  102. protected override void Awake()
  103. {
  104. base.Awake();
  105. if (!Initialize())
  106. enabled = false;
  107. }
  108. protected override void OnPopulateMesh(VertexHelper vh)
  109. {
  110. #if UNITY_EDITOR
  111. if (!Application.isPlaying)
  112. {
  113. if (!Initialize())
  114. {
  115. return;
  116. }
  117. }
  118. #endif
  119. // prepare vertices
  120. vh.Clear();
  121. if (!gameObject.activeInHierarchy)
  122. {
  123. return;
  124. }
  125. Vector2 temp = Vector2.zero;
  126. Vector2 corner1 = Vector2.zero;
  127. Vector2 corner2 = Vector2.zero;
  128. // iterate through current particles
  129. int count = pSystem.GetParticles(particles);
  130. for (int i = 0; i < count; ++i)
  131. {
  132. ParticleSystem.Particle particle = particles[i];
  133. // get particle properties
  134. #if UNITY_5_5_OR_NEWER
  135. Vector2 position = (mainModule.simulationSpace == ParticleSystemSimulationSpace.Local ? particle.position : _transform.InverseTransformPoint(particle.position));
  136. #else
  137. Vector2 position = (pSystem.simulationSpace == ParticleSystemSimulationSpace.Local ? particle.position : _transform.InverseTransformPoint(particle.position));
  138. #endif
  139. float rotation = -particle.rotation * Mathf.Deg2Rad;
  140. float rotation90 = rotation + Mathf.PI / 2;
  141. Color32 color = particle.GetCurrentColor(pSystem);
  142. float size = particle.GetCurrentSize(pSystem) * 0.5f;
  143. // apply scale
  144. #if UNITY_5_5_OR_NEWER
  145. if (mainModule.scalingMode == ParticleSystemScalingMode.Shape)
  146. position /= canvas.scaleFactor;
  147. #else
  148. if (pSystem.scalingMode == ParticleSystemScalingMode.Shape)
  149. position /= canvas.scaleFactor;
  150. #endif
  151. // apply texture sheet animation
  152. Vector4 particleUV = imageUV;
  153. if (textureSheetAnimation.enabled)
  154. {
  155. #if UNITY_5_5_OR_NEWER
  156. float frameProgress = 1 - (particle.remainingLifetime / particle.startLifetime);
  157. if (textureSheetAnimation.frameOverTime.curveMin != null)
  158. {
  159. frameProgress = textureSheetAnimation.frameOverTime.curveMin.Evaluate(1 - (particle.remainingLifetime / particle.startLifetime));
  160. }
  161. else if (textureSheetAnimation.frameOverTime.curve != null)
  162. {
  163. frameProgress = textureSheetAnimation.frameOverTime.curve.Evaluate(1 - (particle.remainingLifetime / particle.startLifetime));
  164. }
  165. else if (textureSheetAnimation.frameOverTime.constant > 0)
  166. {
  167. frameProgress = textureSheetAnimation.frameOverTime.constant - (particle.remainingLifetime / particle.startLifetime);
  168. }
  169. #else
  170. float frameProgress = 1 - (particle.lifetime / particle.startLifetime);
  171. #endif
  172. frameProgress = Mathf.Repeat(frameProgress * textureSheetAnimation.cycleCount, 1);
  173. int frame = 0;
  174. switch (textureSheetAnimation.animation)
  175. {
  176. case ParticleSystemAnimationType.WholeSheet:
  177. frame = Mathf.FloorToInt(frameProgress * textureSheetAnimationFrames);
  178. break;
  179. case ParticleSystemAnimationType.SingleRow:
  180. frame = Mathf.FloorToInt(frameProgress * textureSheetAnimation.numTilesX);
  181. int row = textureSheetAnimation.rowIndex;
  182. // if (textureSheetAnimation.useRandomRow) { // FIXME - is this handled internally by rowIndex?
  183. // row = Random.Range(0, textureSheetAnimation.numTilesY, using: particle.randomSeed);
  184. // }
  185. frame += row * textureSheetAnimation.numTilesX;
  186. break;
  187. }
  188. frame %= textureSheetAnimationFrames;
  189. particleUV.x = (frame % textureSheetAnimation.numTilesX) * textureSheetAnimationFrameSize.x;
  190. particleUV.y = Mathf.FloorToInt(frame / textureSheetAnimation.numTilesX) * textureSheetAnimationFrameSize.y;
  191. particleUV.z = particleUV.x + textureSheetAnimationFrameSize.x;
  192. particleUV.w = particleUV.y + textureSheetAnimationFrameSize.y;
  193. }
  194. temp.x = particleUV.x;
  195. temp.y = particleUV.y;
  196. _quad[0] = UIVertex.simpleVert;
  197. _quad[0].color = color;
  198. _quad[0].uv0 = temp;
  199. temp.x = particleUV.x;
  200. temp.y = particleUV.w;
  201. _quad[1] = UIVertex.simpleVert;
  202. _quad[1].color = color;
  203. _quad[1].uv0 = temp;
  204. temp.x = particleUV.z;
  205. temp.y = particleUV.w;
  206. _quad[2] = UIVertex.simpleVert;
  207. _quad[2].color = color;
  208. _quad[2].uv0 = temp;
  209. temp.x = particleUV.z;
  210. temp.y = particleUV.y;
  211. _quad[3] = UIVertex.simpleVert;
  212. _quad[3].color = color;
  213. _quad[3].uv0 = temp;
  214. if (rotation == 0)
  215. {
  216. // no rotation
  217. corner1.x = position.x - size;
  218. corner1.y = position.y - size;
  219. corner2.x = position.x + size;
  220. corner2.y = position.y + size;
  221. temp.x = corner1.x;
  222. temp.y = corner1.y;
  223. _quad[0].position = temp;
  224. temp.x = corner1.x;
  225. temp.y = corner2.y;
  226. _quad[1].position = temp;
  227. temp.x = corner2.x;
  228. temp.y = corner2.y;
  229. _quad[2].position = temp;
  230. temp.x = corner2.x;
  231. temp.y = corner1.y;
  232. _quad[3].position = temp;
  233. }
  234. else
  235. {
  236. // apply rotation
  237. Vector2 right = new Vector2(Mathf.Cos(rotation), Mathf.Sin(rotation)) * size;
  238. Vector2 up = new Vector2(Mathf.Cos(rotation90), Mathf.Sin(rotation90)) * size;
  239. _quad[0].position = position - right - up;
  240. _quad[1].position = position - right + up;
  241. _quad[2].position = position + right + up;
  242. _quad[3].position = position + right - up;
  243. }
  244. vh.AddUIVertexQuad(_quad);
  245. }
  246. }
  247. void Update()
  248. {
  249. if (!fixedTime && Application.isPlaying)
  250. {
  251. pSystem.Simulate(Time.unscaledDeltaTime, false, false, true);
  252. SetAllDirty();
  253. if ((currentMaterial != null && currentTexture != currentMaterial.mainTexture) ||
  254. (material != null && currentMaterial != null && material.shader != currentMaterial.shader))
  255. {
  256. pSystem = null;
  257. Initialize();
  258. }
  259. }
  260. }
  261. void LateUpdate()
  262. {
  263. if (!Application.isPlaying)
  264. {
  265. SetAllDirty();
  266. }
  267. else
  268. {
  269. if (fixedTime)
  270. {
  271. pSystem.Simulate(Time.unscaledDeltaTime, false, false, true);
  272. SetAllDirty();
  273. if ((currentMaterial != null && currentTexture != currentMaterial.mainTexture) ||
  274. (material != null && currentMaterial != null && material.shader != currentMaterial.shader))
  275. {
  276. pSystem = null;
  277. Initialize();
  278. }
  279. }
  280. }
  281. if (material == currentMaterial)
  282. return;
  283. pSystem = null;
  284. Initialize();
  285. }
  286. }
  287. #endif
  288. }