UIParticleSystem.cs 15 KB

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