ParticleSystemPool.cs 7.8 KB

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