UIParticleHelper.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class UIParticleHelper : MonoBehaviour
  5. {
  6. public int AddSortingNum = 0;
  7. ParticleSystem[] pSystems;
  8. Renderer[] pRenderers;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. pSystems = GetComponentsInChildren<ParticleSystem>();
  13. pRenderers = GetComponentsInChildren<Renderer>();
  14. }
  15. public void Init(int baseSortingOrder)
  16. {
  17. if (pSystems == null)
  18. pSystems = GetComponentsInChildren<ParticleSystem>();
  19. if (pRenderers == null)
  20. pRenderers = GetComponentsInChildren<Renderer>();
  21. foreach (var renderer in pRenderers)
  22. {
  23. renderer.sortingOrder += baseSortingOrder + AddSortingNum;
  24. }
  25. }
  26. public void OnDestroy()
  27. {
  28. pSystems = null;
  29. pRenderers = null;
  30. }
  31. public void Update()
  32. {
  33. if (pSystems == null) return;
  34. bool isPlaying = false;
  35. foreach(var system in pSystems)
  36. {
  37. if (system.isPlaying)
  38. {
  39. isPlaying = true;
  40. return;
  41. }
  42. }
  43. if (!isPlaying)
  44. {
  45. gameObject.SetActive(false);
  46. }
  47. }
  48. public void StartPlayParticle()
  49. {
  50. gameObject.SetActive(true);
  51. }
  52. }