Resume.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #if UNITY_2017_1_OR_NEWER
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. namespace BehaviorDesigner.Runtime.Tasks.Unity.Timeline
  5. {
  6. [TaskCategory("Unity/Timeline")]
  7. [TaskDescription("Resume playing a paused playable.")]
  8. public class Resume : Action
  9. {
  10. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  11. public SharedGameObject targetGameObject;
  12. [Tooltip("Should the task be stopped when the timeline has stopped playing?")]
  13. public SharedBool stopWhenComplete;
  14. private PlayableDirector playableDirector;
  15. private GameObject prevGameObject;
  16. private bool playbackStarted;
  17. public override void OnStart()
  18. {
  19. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  20. if (currentGameObject != prevGameObject) {
  21. playableDirector = currentGameObject.GetComponent<PlayableDirector>();
  22. prevGameObject = currentGameObject;
  23. }
  24. playbackStarted = false;
  25. }
  26. public override TaskStatus OnUpdate()
  27. {
  28. if (playableDirector == null) {
  29. Debug.LogWarning("PlayableDirector is null");
  30. return TaskStatus.Failure;
  31. }
  32. if (playbackStarted) {
  33. if (stopWhenComplete.Value && playableDirector.state == PlayState.Playing) {
  34. return TaskStatus.Running;
  35. }
  36. return TaskStatus.Success;
  37. }
  38. playableDirector.Resume();
  39. playbackStarted = true;
  40. return stopWhenComplete.Value ? TaskStatus.Running : TaskStatus.Success;
  41. }
  42. public override void OnReset()
  43. {
  44. targetGameObject = null;
  45. stopWhenComplete = false;
  46. }
  47. }
  48. }
  49. #endif