RewindableParticleSystemTimeline.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Chronos
  4. {
  5. // Currently bugged in Unity 5.5 due to a bug in the Simulate method at times higher than the system's duration
  6. // https://fogbugz.unity3d.com/default.asp?854431_5mmt5ltn2q6nuseh
  7. public class RewindableParticleSystemTimeline : ComponentTimeline<ParticleSystem>, IParticleSystemTimeline
  8. {
  9. #region Fields
  10. private float absoluteSimulationTime;
  11. private float loopedSimulationTime;
  12. private float relativeStartTime;
  13. #endregion
  14. #region Properties
  15. public float playbackSpeed { get; set; }
  16. public float time
  17. {
  18. get { return (loopedSimulationTime - relativeStartTime) % component.main.duration; }
  19. set { loopedSimulationTime = relativeStartTime + value; }
  20. }
  21. public bool isPlaying
  22. {
  23. get { return state == State.Playing || state == State.Stopping; }
  24. }
  25. public bool isPaused
  26. {
  27. get { return state == State.Paused; }
  28. }
  29. public bool isStopped
  30. {
  31. get { return state == State.Stopped; }
  32. }
  33. #endregion
  34. #region State and Emission
  35. private enum State
  36. {
  37. Playing,
  38. Paused,
  39. Stopping,
  40. Stopped
  41. }
  42. private enum EmissionAction
  43. {
  44. EnableEmission,
  45. DisableEmission,
  46. Play,
  47. Stop
  48. }
  49. private struct StateEvent
  50. {
  51. public State state;
  52. public float time;
  53. public StateEvent(State state, float time)
  54. {
  55. this.state = state;
  56. this.time = time;
  57. }
  58. }
  59. private struct EmissionEvent
  60. {
  61. public EmissionAction action;
  62. public float time;
  63. public EmissionEvent(EmissionAction action, float time)
  64. {
  65. this.action = action;
  66. this.time = time;
  67. }
  68. }
  69. private float stateEventsTime
  70. {
  71. get { return timeline.time; }
  72. }
  73. private float emissionEventsTime
  74. {
  75. get { return absoluteSimulationTime; }
  76. }
  77. private void RegisterState(State state)
  78. {
  79. stateEvents.Add(new StateEvent(state, stateEventsTime));
  80. }
  81. private void RegisterEmission(EmissionAction action)
  82. {
  83. emissionEvents.Add(new EmissionEvent(action, emissionEventsTime));
  84. }
  85. public RewindableParticleSystemTimeline(Timeline timeline, ParticleSystem component) : base(timeline, component)
  86. {
  87. emissionEvents = new List<EmissionEvent>();
  88. stateEvents = new List<StateEvent>();
  89. }
  90. private List<StateEvent> stateEvents;
  91. private State stateOnStart;
  92. private List<EmissionEvent> emissionEvents;
  93. private bool enableEmissionOnStart;
  94. private State _state;
  95. private State state
  96. {
  97. get { return _state; }
  98. set
  99. {
  100. if (!AssertForwardProperty("state", Severity.Error)) return;
  101. if (_state != value)
  102. {
  103. RegisterState(value);
  104. _state = value;
  105. }
  106. }
  107. }
  108. private bool _enableEmission;
  109. public bool enableEmission
  110. {
  111. get { return _enableEmission; }
  112. set
  113. {
  114. if (!AssertForwardProperty("enableEmission", Severity.Warn)) return;
  115. if (_enableEmission && !value)
  116. {
  117. RegisterEmission(EmissionAction.DisableEmission);
  118. }
  119. else if (!_enableEmission && value)
  120. {
  121. RegisterEmission(EmissionAction.EnableEmission);
  122. }
  123. _enableEmission = value;
  124. }
  125. }
  126. #endregion
  127. #region Timeline
  128. public override void CopyProperties(ParticleSystem source)
  129. {
  130. playbackSpeed = source.main.simulationSpeed;
  131. stateOnStart = state = source.main.playOnAwake ? State.Playing : State.Stopped;
  132. enableEmissionOnStart = _enableEmission = source.emission.enabled;
  133. time = 0;
  134. if (source.useAutoRandomSeed)
  135. {
  136. if (source.isPlaying)
  137. {
  138. source.Pause(true);
  139. }
  140. ////source.useAutoRandomSeed = false;
  141. ////source.randomSeed = (uint)Random.Range(1, int.MaxValue);
  142. }
  143. }
  144. public override void Update()
  145. {
  146. if (timeline.timeScale < 0)
  147. {
  148. // Determine state by consuming state events
  149. if (stateEvents.Count > 0)
  150. {
  151. StateEvent lastStateEvent = stateEvents[stateEvents.Count - 1];
  152. if (stateEventsTime <= lastStateEvent.time)
  153. {
  154. stateEvents.Remove(lastStateEvent);
  155. if (stateEvents.Count > 0)
  156. {
  157. _state = stateEvents[stateEvents.Count - 1].state;
  158. }
  159. else
  160. {
  161. _state = stateOnStart;
  162. }
  163. }
  164. }
  165. // Consume emission events
  166. for (int i = emissionEvents.Count - 1; i >= 0; i--)
  167. {
  168. if (emissionEvents[i].time > emissionEventsTime)
  169. {
  170. emissionEvents.RemoveAt(i);
  171. }
  172. }
  173. }
  174. // Known issue: low time scales / speed will cause stutter
  175. // Reported here: http://fogbugz.unity3d.com/default.asp?694191_dso514lin4rf5vbg
  176. component.Simulate(0, true, true);
  177. if (loopedSimulationTime > 0)
  178. {
  179. var emission = component.emission;
  180. emission.enabled = enableEmissionOnStart;
  181. float chunkStartTime = 0;
  182. for (int i = 0; i < emissionEvents.Count; i++)
  183. {
  184. EmissionEvent current = emissionEvents[i];
  185. component.Simulate(current.time - chunkStartTime, true, false);
  186. emission.enabled = current.action == EmissionAction.Play || current.action == EmissionAction.EnableEmission;
  187. chunkStartTime = current.time;
  188. }
  189. component.Simulate(loopedSimulationTime - chunkStartTime, true, false);
  190. if (state == State.Stopping && component.particleCount == 0 && timeline.timeScale > 0)
  191. {
  192. state = State.Stopped;
  193. }
  194. }
  195. if (state == State.Playing || state == State.Stopping)
  196. {
  197. absoluteSimulationTime += timeline.deltaTime * playbackSpeed;
  198. if (state == State.Playing && !component.main.loop && absoluteSimulationTime >= component.main.duration)
  199. {
  200. // A bit hacky to stop it here, as the real system just goes on playing,
  201. // just without emitting, but it shouldn't cause any problem. Unfortunately,
  202. // there is no check on Unity's side to see if it entered that final state.
  203. state = State.Stopping;
  204. }
  205. // Can be performance intensive at high times.
  206. // Limit it with a loop-multiple of its time (globally configurable)
  207. float maxLoops = Timekeeper.instance.maxParticleLoops;
  208. if (maxLoops > 0 && state != State.Stopping)
  209. {
  210. loopedSimulationTime = absoluteSimulationTime % (component.main.duration * maxLoops);
  211. }
  212. else
  213. {
  214. loopedSimulationTime = absoluteSimulationTime;
  215. }
  216. }
  217. }
  218. #endregion
  219. #region Methods
  220. public void Play(bool withChildren = true)
  221. {
  222. if (!AssertForwardMethod("Play", Severity.Warn)) return;
  223. if (state != State.Paused)
  224. {
  225. RegisterEmission(EmissionAction.Play);
  226. relativeStartTime = loopedSimulationTime;
  227. }
  228. state = State.Playing;
  229. if (withChildren)
  230. {
  231. ExecuteOnChildren(ps => ps.Play(false), ps => ps.Play(false));
  232. }
  233. }
  234. public void Pause(bool withChildren = true)
  235. {
  236. if (!AssertForwardMethod("Pause", Severity.Warn)) return;
  237. state = State.Paused;
  238. if (withChildren)
  239. {
  240. ExecuteOnChildren(ps => ps.Pause(false), ps => ps.Pause(false));
  241. }
  242. }
  243. public void Stop(bool withChildren = true)
  244. {
  245. if (!AssertForwardMethod("Stop", Severity.Warn)) return;
  246. state = State.Stopping;
  247. RegisterEmission(EmissionAction.Stop);
  248. if (withChildren)
  249. {
  250. ExecuteOnChildren(ps => ps.Stop(false), ps => ps.Stop(false));
  251. }
  252. }
  253. public bool IsAlive(bool withChildren = true)
  254. {
  255. if (state == State.Stopped)
  256. {
  257. return false;
  258. }
  259. if (withChildren)
  260. {
  261. return CheckOnChildren(ps => ps.IsAlive(false), ps => ps.IsAlive(false));
  262. }
  263. return true;
  264. }
  265. #endregion
  266. #region Hierarchy
  267. private delegate void ChildNativeAction(ParticleSystem target);
  268. private delegate void ChildChronosAction(IParticleSystemTimeline target);
  269. private delegate bool ChildNativeCheck(ParticleSystem target);
  270. private delegate bool ChildChronosCheck(IParticleSystemTimeline target);
  271. private void ExecuteOnChildren(ChildNativeAction native, ChildChronosAction chronos)
  272. {
  273. foreach (ParticleSystem childParticleSystem in timeline.GetComponentsInChildren<ParticleSystem>())
  274. {
  275. if (childParticleSystem == component)
  276. {
  277. continue;
  278. }
  279. Timeline childTimeline = childParticleSystem.GetComponent<Timeline>();
  280. if (childTimeline != null)
  281. {
  282. chronos(childTimeline.particleSystem);
  283. }
  284. else
  285. {
  286. native(childParticleSystem);
  287. }
  288. }
  289. }
  290. private bool CheckOnChildren(ChildNativeCheck native, ChildChronosCheck chronos)
  291. {
  292. foreach (ParticleSystem childParticleSystem in timeline.GetComponentsInChildren<ParticleSystem>())
  293. {
  294. if (childParticleSystem == component)
  295. {
  296. continue;
  297. }
  298. Timeline childTimeline = childParticleSystem.GetComponent<Timeline>();
  299. if (childTimeline != null)
  300. {
  301. if (!chronos(childTimeline.particleSystem))
  302. {
  303. return false;
  304. }
  305. }
  306. else
  307. {
  308. if (!native(childParticleSystem))
  309. {
  310. return false;
  311. }
  312. }
  313. }
  314. return true;
  315. }
  316. #endregion
  317. #region Utility
  318. private bool AssertForwardMethod(string method, Severity severity)
  319. {
  320. if (timeline.timeScale <= 0)
  321. {
  322. if (severity == Severity.Error)
  323. {
  324. throw new ChronosException("Cannot call " + method + " on the particle system while time is paused or rewinding.");
  325. }
  326. else if (severity == Severity.Warn)
  327. {
  328. Debug.LogWarning("Trying to call " + method +
  329. " on the particle system while time is paused or rewinding, ignoring.");
  330. }
  331. }
  332. return timeline.timeScale > 0;
  333. }
  334. private bool AssertForwardProperty(string property, Severity severity)
  335. {
  336. if (timeline.timeScale <= 0)
  337. {
  338. if (severity == Severity.Error)
  339. {
  340. throw new ChronosException("Cannot set " + property + " on the particle system while time is paused or rewinding.");
  341. }
  342. else if (severity == Severity.Warn)
  343. {
  344. Debug.LogWarning("Trying to set " + property +
  345. " on the particle system while time is paused or rewinding, ignoring.");
  346. }
  347. }
  348. return timeline.timeScale > 0;
  349. }
  350. #endregion
  351. }
  352. }