ParticleSystemPool.cs 8.5 KB

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