ParticleSystemPool.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using Fort23.Core;
  3. using Fort23.Mono;
  4. #if !COMBAT_SERVER
  5. using CombatLibrary.CombatLibrary.CombatCore.GPool;
  6. using UnityEngine;
  7. #endif
  8. namespace Fort23.UTool
  9. {
  10. public class ParticleSystemPool : GObjectPoolBasic
  11. {
  12. #if !COMBAT_SERVER
  13. //public string endPlayerPartic;
  14. protected ParticleSystem[] m_particles;
  15. protected RenderSetting[] m_renderSetting;
  16. protected DelayShow[] delayShows;
  17. public float delayTime;
  18. protected float currDelayTime;
  19. public string endFx;
  20. private bool isShow;
  21. public System.Action<ParticleSystemPool> OnEndFx;
  22. /// <summary>
  23. /// 播放速度
  24. /// </summary>
  25. public float palySpeed = (float)1;
  26. private BetterList<ParticleSystem> _lastLoopFx = new BetterList<ParticleSystem>();
  27. private LoopDelayHide _loopDelayHide;
  28. protected HideShowEndFx _hideShowEndFx;
  29. public ParticleSystem[] Particles
  30. {
  31. get
  32. {
  33. if (m_particles == null && transform != null)
  34. {
  35. m_particles = transform.GetComponentsInChildren<ParticleSystem>(false);
  36. }
  37. return m_particles;
  38. }
  39. }
  40. public Animator[] Animators
  41. {
  42. get
  43. {
  44. if (m_animators == null && transform != null)
  45. {
  46. m_animators = transform.GetComponentsInChildren<Animator>(false);
  47. }
  48. return m_animators;
  49. }
  50. }
  51. private Animator[] m_animators;
  52. public RenderSetting[] RenderSetting
  53. {
  54. get
  55. {
  56. if (m_renderSetting == null && transform != null)
  57. {
  58. m_renderSetting = transform.GetComponentsInChildren<RenderSetting>(false);
  59. }
  60. return m_renderSetting;
  61. }
  62. }
  63. public DelayShow[] DelayShows
  64. {
  65. get
  66. {
  67. if (delayShows == null)
  68. {
  69. delayShows = transform.GetComponentsInChildren<DelayShow>(false);
  70. }
  71. return delayShows;
  72. }
  73. }
  74. [ContextMenu("Replay")]
  75. public void Replay()
  76. {
  77. ResetData();
  78. ActiveObj();
  79. }
  80. private float m_duration = -1f;
  81. protected override void ProResetData()
  82. {
  83. try
  84. {
  85. //ParticleSystem a;
  86. //a.SendMessageUpwards
  87. if (gameObject == null)
  88. {
  89. return;
  90. }
  91. if (Particles != null)
  92. {
  93. for (int i = 0; i < Particles.Length; i++)
  94. {
  95. Particles[i].Stop();
  96. Particles[i].Clear();
  97. }
  98. }
  99. //NsEffectManager.RunReplayEffect(gameObject, true);
  100. }
  101. catch (Exception ex)
  102. {
  103. LogTool.Warning(ex.ToString());
  104. }
  105. }
  106. public override async CTask DelayHide()
  107. {
  108. if (_loopDelayHide == null)
  109. {
  110. await base.DelayHide();
  111. return;
  112. }
  113. bool loopTime = false;
  114. if (Particles != null)
  115. {
  116. for (int i = 0; i < Particles.Length; i++)
  117. {
  118. bool isLoop = Particles[i].main.loop;
  119. if (isLoop && !_loopDelayHide.excludeObject.Contains(Particles[i].gameObject))
  120. {
  121. loopTime = true;
  122. _lastLoopFx.Add(Particles[i]);
  123. ParticleSystem.MainModule mainModule = Particles[i].main;
  124. mainModule.loop = false;
  125. }
  126. }
  127. }
  128. if (loopTime)
  129. {
  130. await TimerComponent.Instance.WaitAsync((int)(_loopDelayHide.delayTime * 1000));
  131. }
  132. }
  133. protected override void ProActiveObj()
  134. {
  135. //gameObject.SetActive(true);
  136. _loopDelayHide = own.GetComponent<LoopDelayHide>();
  137. _hideShowEndFx = own.GetComponent<HideShowEndFx>();
  138. for (int i = 0; i < Particles.Length; i++)
  139. {
  140. if (_lastLoopFx.Contains(Particles[i]))
  141. {
  142. ParticleSystem.MainModule main = Particles[i].main;
  143. main.loop = true;
  144. }
  145. Particles[i].Stop();
  146. Particles[i].Clear(true);
  147. Particles[i].Play(true);
  148. }
  149. if (DelayShows != null)
  150. {
  151. for (int i = 0; i < DelayShows.Length; i++)
  152. {
  153. DelayShows[i].Init();
  154. }
  155. }
  156. currDelayTime = (float)0;
  157. StaticUpdater.Instance.AddRenderUpdateCallBack(UpdatePool);
  158. isShow = true;
  159. }
  160. protected virtual void UpdatePool()
  161. {
  162. currDelayTime += Time.deltaTime;
  163. if (IsFinish())
  164. {
  165. OnDurationUp();
  166. }
  167. }
  168. public bool IsFinish()
  169. {
  170. if (Particles.Length <= 0 && delayTime <= 0) //没有特效的表现
  171. {
  172. delayTime = 30;
  173. }
  174. if (currDelayTime < delayTime)
  175. {
  176. return false;
  177. }
  178. bool isFinish = true;
  179. for (int i = 0; i < Particles.Length; i++)
  180. {
  181. if (!Particles[i])
  182. continue;
  183. if (Particles[i].IsAlive())
  184. {
  185. isFinish = false;
  186. break;
  187. }
  188. }
  189. return isFinish;
  190. }
  191. protected virtual void OnDurationUp()
  192. {
  193. if (this == null ||
  194. gameObject == null)
  195. {
  196. return;
  197. }
  198. CloseData();
  199. GObjectPool.Instance.Recycle(this);
  200. }
  201. private void CloseData()
  202. {
  203. StaticUpdater.Instance.RemoveRenderUpdateCallBack(UpdatePool);
  204. }
  205. protected override void ProDormancyObj()
  206. {
  207. if (isShow)
  208. {
  209. if (!string.IsNullOrEmpty(endFx))
  210. {
  211. // LoadResourceTask fxTask = CombatController.Instance.LoadCombatFX(endFx,
  212. // null, delegate(IPoolObject pool)
  213. // {
  214. // if (pool != null)
  215. // {
  216. // pool.myTran.position = transform.position;
  217. // }
  218. // });
  219. }
  220. isShow = false;
  221. }
  222. CloseData();
  223. if (gameObject == null)
  224. {
  225. return;
  226. }
  227. for (int i = 0; i < Particles.Length; i++)
  228. {
  229. Particles[i].Stop();
  230. }
  231. gameObject.SetActive(false);
  232. OnEndFx?.Invoke(this);
  233. OnEndFx = null;
  234. }
  235. //private void PlayerEnd()
  236. //{
  237. // if (endPlayerPartic != null)
  238. // {
  239. // CombatController.Instance.LoadCombatFX(endPlayerPartic, delegate (IPoolObject pool)
  240. // {
  241. // }, transform.parent, null, null);
  242. // }
  243. //}
  244. protected override void ProOnDestroy()
  245. {
  246. if (!gameObject)
  247. {
  248. return;
  249. }
  250. base.ProOnDestroy();
  251. //if (endPlayerPartic != null)
  252. //{
  253. //}
  254. // Destroy(gameObject);
  255. }
  256. #endif
  257. }
  258. }