EffectUtil.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class EffectUtil
  5. {
  6. public static GameObject CreateLanding()
  7. {
  8. GameObject gameObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Effects/Landing"));
  9. return gameObj;
  10. }
  11. public static GameObject CreateDisappear()
  12. {
  13. GameObject gameObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Effects/Disappear"));
  14. return gameObj;
  15. }
  16. private static Dictionary<string, List<GameObject>> effectCacheDict = new Dictionary<string, List<GameObject>>();
  17. public static GameObject CreateEffect(string path)
  18. {
  19. GameObject gameObj = null;
  20. if(effectCacheDict.ContainsKey(path) && effectCacheDict[path].Count > 0)
  21. {
  22. gameObj = effectCacheDict[path][0];
  23. effectCacheDict[path].RemoveAt(0);
  24. gameObj.SetActive(true);
  25. ParticleSystem ps = gameObj.GetComponent<ParticleSystem>();
  26. if(ps != null)
  27. ps.Play();
  28. }
  29. else
  30. {
  31. GameObject prefabObj = Resources.Load<GameObject>("Prefabs/Effects/"+path);
  32. if(prefabObj == null)
  33. {
  34. Debuger.LogError("Could not load effect from : "+path);
  35. return null;
  36. }
  37. gameObj = GameObject.Instantiate<GameObject>(prefabObj);
  38. }
  39. EffectDelayDestroy delayDes = gameObj.GetComponent<EffectDelayDestroy>();
  40. if(delayDes != null)
  41. delayDes.path = path;
  42. return gameObj;
  43. }
  44. public static void Recycle(string path, GameObject obj)
  45. {
  46. if(StringUtil.Empty(path))
  47. {
  48. GameObject.Destroy(obj);
  49. Debuger.LogError("Recycal Effect path is empty "+obj);
  50. return;
  51. }
  52. // Debuger.Log("Recycal Effect["+path+"] "+obj);
  53. if(obj.transform.parent != null)
  54. obj.transform.SetParent(null);
  55. ParticleSystem ps = obj.GetComponent<ParticleSystem>();
  56. if(ps != null)
  57. ps.Stop();
  58. obj.SetActive(false);
  59. if(effectCacheDict.ContainsKey(path))
  60. {
  61. effectCacheDict[path].Add(obj);
  62. }
  63. else
  64. {
  65. List<GameObject> list = new List<GameObject>();
  66. list.Add(obj);
  67. effectCacheDict.Add(path, list);
  68. }
  69. }
  70. public static void ClearCache()
  71. {
  72. effectCacheDict.Clear();
  73. }
  74. }