ParticleSystemPool.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 float trailTime = 0.1f;
  85. protected bool isUpdateTrail = false;
  86. protected override void ProResetData()
  87. {
  88. try
  89. {
  90. //ParticleSystem a;
  91. //a.SendMessageUpwards
  92. if (gameObject == null)
  93. {
  94. return;
  95. }
  96. if (_trailRenderers == null)
  97. {
  98. _trailRenderers = gameObject.GetComponentsInChildren<TrailRenderer>(false);
  99. }
  100. if (_trailRenderers != null)
  101. {
  102. for (int i = 0; i < _trailRenderers.Length; i++)
  103. {
  104. _trailRenderers[i].Clear();
  105. _trailRenderers[i].gameObject.SetActive(false);
  106. }
  107. trailTime = 0;
  108. isUpdateTrail = true;
  109. }
  110. if (Particles != null)
  111. {
  112. for (int i = 0; i < Particles.Length; i++)
  113. {
  114. Particles[i].Stop();
  115. Particles[i].Clear();
  116. }
  117. }
  118. //NsEffectManager.RunReplayEffect(gameObject, true);
  119. }
  120. catch (Exception ex)
  121. {
  122. LogTool.Warning(ex.ToString());
  123. }
  124. }
  125. public override async CTask DelayHide()
  126. {
  127. if (_loopDelayHide == null)
  128. {
  129. await base.DelayHide();
  130. return;
  131. }
  132. bool loopTime = false;
  133. if (Particles != null)
  134. {
  135. for (int i = 0; i < Particles.Length; i++)
  136. {
  137. bool isLoop = Particles[i].main.loop;
  138. if (isLoop && !_loopDelayHide.excludeObject.Contains(Particles[i].gameObject))
  139. {
  140. loopTime = true;
  141. _lastLoopFx.Add(Particles[i]);
  142. ParticleSystem.MainModule mainModule = Particles[i].main;
  143. mainModule.loop = false;
  144. }
  145. else
  146. {
  147. Particles[i].Stop(false);
  148. Particles[i].Clear(false);
  149. }
  150. }
  151. }
  152. if (loopTime)
  153. {
  154. await TimerComponent.Instance.WaitAsync((int)(_loopDelayHide.delayTime * 1000));
  155. }
  156. }
  157. protected override void ProActiveObj()
  158. {
  159. //gameObject.SetActive(true);
  160. _loopDelayHide = own.GetComponent<LoopDelayHide>();
  161. _hideShowEndFx = own.GetComponent<HideShowEndFx>();
  162. for (int i = 0; i < Particles.Length; i++)
  163. {
  164. if (_lastLoopFx.Contains(Particles[i]))
  165. {
  166. ParticleSystem.MainModule main = Particles[i].main;
  167. main.loop = true;
  168. }
  169. Particles[i].Stop();
  170. Particles[i].Clear(true);
  171. Particles[i].Play(true);
  172. }
  173. if (DelayShows != null)
  174. {
  175. for (int i = 0; i < DelayShows.Length; i++)
  176. {
  177. DelayShows[i].Init();
  178. }
  179. }
  180. currDelayTime = (float)0;
  181. StaticUpdater.Instance.AddRenderUpdateCallBack(UpdatePool);
  182. isShow = true;
  183. }
  184. protected virtual void UpdatePool()
  185. {
  186. currDelayTime += Time.deltaTime;
  187. if (isUpdateTrail)
  188. {
  189. trailTime += Time.deltaTime;
  190. if (trailTime > 0.1f)
  191. {
  192. for (int i = 0; i < _trailRenderers.Length; i++)
  193. {
  194. _trailRenderers[i].Clear();
  195. _trailRenderers[i].gameObject.SetActive(true);
  196. }
  197. isUpdateTrail = false;
  198. }
  199. }
  200. if (IsFinish())
  201. {
  202. OnDurationUp();
  203. }
  204. }
  205. public bool IsFinish()
  206. {
  207. if (Particles.Length <= 0 && delayTime <= 0) //没有特效的表现
  208. {
  209. delayTime = 30;
  210. }
  211. if (currDelayTime < delayTime)
  212. {
  213. return false;
  214. }
  215. bool isFinish = true;
  216. for (int i = 0; i < Particles.Length; i++)
  217. {
  218. if (!Particles[i])
  219. continue;
  220. if (Particles[i].IsAlive())
  221. {
  222. isFinish = false;
  223. break;
  224. }
  225. }
  226. return isFinish;
  227. }
  228. protected virtual void OnDurationUp()
  229. {
  230. if (this == null ||
  231. gameObject == null)
  232. {
  233. return;
  234. }
  235. CloseData();
  236. GObjectPool.Instance.Recycle(this);
  237. }
  238. private void CloseData()
  239. {
  240. StaticUpdater.Instance.RemoveRenderUpdateCallBack(UpdatePool);
  241. }
  242. protected override void ProDormancyObj()
  243. {
  244. if (isShow)
  245. {
  246. if (!string.IsNullOrEmpty(endFx))
  247. {
  248. // LoadResourceTask fxTask = CombatController.Instance.LoadCombatFX(endFx,
  249. // null, delegate(IPoolObject pool)
  250. // {
  251. // if (pool != null)
  252. // {
  253. // pool.myTran.position = transform.position;
  254. // }
  255. // });
  256. }
  257. isShow = false;
  258. }
  259. CloseData();
  260. if (gameObject == null)
  261. {
  262. return;
  263. }
  264. for (int i = 0; i < Particles.Length; i++)
  265. {
  266. Particles[i].Stop();
  267. }
  268. gameObject.SetActive(false);
  269. OnEndFx?.Invoke(this);
  270. OnEndFx = null;
  271. }
  272. //private void PlayerEnd()
  273. //{
  274. // if (endPlayerPartic != null)
  275. // {
  276. // CombatController.Instance.LoadCombatFX(endPlayerPartic, delegate (IPoolObject pool)
  277. // {
  278. // }, transform.parent, null, null);
  279. // }
  280. //}
  281. protected override void ProOnDestroy()
  282. {
  283. if (!gameObject)
  284. {
  285. return;
  286. }
  287. base.ProOnDestroy();
  288. //if (endPlayerPartic != null)
  289. //{
  290. //}
  291. // Destroy(gameObject);
  292. }
  293. #endif
  294. }
  295. }