UIEffectCfg.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. public class UIEffectCfg:MonoBehaviour
  5. {
  6. [Serializable]
  7. public struct stUIEffect
  8. {
  9. /// <summary>
  10. /// 特效资源名字
  11. /// </summary>
  12. public string assetName;
  13. /// <summary>
  14. /// 特效挂载的parent路径
  15. /// </summary>
  16. public Transform parent;
  17. }
  18. public stUIEffect[] UsedEffect;
  19. bool mReady = false;
  20. List<String> mEffectNames;
  21. List<GameObject> mInstGoes;
  22. long loadingSeqId = 0;
  23. public bool Ready
  24. {
  25. get { return mReady; }
  26. }
  27. public void LoadEffect()
  28. {
  29. if (UsedEffect == null || UsedEffect.Length == 0) return;
  30. mEffectNames = new List<string>();
  31. for (int idx = 0; idx < UsedEffect.Length; idx++)
  32. {
  33. stUIEffect effectCfg = UsedEffect[idx];
  34. string effectName = effectCfg.assetName;
  35. if (!string.IsNullOrEmpty(effectName))
  36. {
  37. if (!mEffectNames.Contains(effectName))
  38. mEffectNames.Add(effectName);
  39. }
  40. }
  41. if (mEffectNames.Count > 0)
  42. {
  43. mReady = false;
  44. loadingSeqId = ResourceMgr.Instance.LoadAsset<List<GameObject>>(OnLoadUIEffectCompleted, Constants.UIEffectPath, mEffectNames.ToArray());
  45. }
  46. }
  47. void OnLoadUIEffectCompleted(List<GameObject> prefabs, string assetPath, string[] assetNames)
  48. {
  49. mReady = true;
  50. loadingSeqId = 0;
  51. mInstGoes = new List<GameObject>();
  52. for (int idx = 0; idx < UsedEffect.Length; idx++)
  53. {
  54. stUIEffect effectCfg = UsedEffect[idx];
  55. if (!string.IsNullOrEmpty(effectCfg.assetName))
  56. {
  57. GameObject effectGo = ResourceMgr.Instance.GetGoFromPool(Constants.UIEffectPath, effectCfg.assetName);
  58. effectGo.name = effectCfg.assetName;
  59. effectGo.transform.SetParent(effectCfg.parent);
  60. effectGo.transform.localPosition = Vector3.zero;
  61. effectGo.transform.localRotation = Quaternion.identity;
  62. effectGo.transform.localScale = Vector3.one;
  63. effectGo.SetActive(false);
  64. mInstGoes.Add(effectGo);
  65. }
  66. }
  67. }
  68. void OnDestroy()
  69. {
  70. if (UsedEffect == null && UsedEffect.Length == 0) return;
  71. Clean();
  72. mInstGoes = null;
  73. UsedEffect = null;
  74. }
  75. /// <summary>
  76. /// 显示特效
  77. /// </summary>
  78. /// <param name="name"></param>
  79. /// <param name="parent"></param>
  80. /// <returns></returns>
  81. public void ShowEffect(string name, Transform parent)
  82. {
  83. if (string.IsNullOrEmpty(name) || parent == null) return;
  84. Transform effectNode = parent.Find(name);
  85. if (effectNode == null) return;
  86. effectNode.gameObject.SetActive(true);
  87. }
  88. public void HideEffect(string name, Transform parent)
  89. {
  90. if (string.IsNullOrEmpty(name) || parent == null) return;
  91. Transform effectNode = parent.Find(name);
  92. if (effectNode == null) return;
  93. effectNode.gameObject.SetActive(false);
  94. }
  95. public void Clean()
  96. {
  97. if(loadingSeqId > 0)
  98. {
  99. ResourceMgr.Instance.UnloadAssetBySeqId(loadingSeqId);
  100. loadingSeqId = 0;
  101. }
  102. if (mInstGoes != null && mInstGoes.Count > 0)
  103. {
  104. for (int idx = 0; idx < mInstGoes.Count; idx++)
  105. {
  106. ResourceMgr.Instance.RecycleGO(Constants.UIEffectPath, mInstGoes[idx].name, mInstGoes[idx]);
  107. }
  108. mInstGoes.Clear();
  109. }
  110. mReady = false;
  111. }
  112. }