ParticleSystemPool.cs 8.3 KB

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