ParticleSystemPool.cs 9.4 KB

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