IsPlaying.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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("Is the timeline currently playing?")]
  8. public class IsPlaying : Conditional
  9. {
  10. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  11. public SharedGameObject targetGameObject;
  12. private PlayableDirector playableDirector;
  13. private GameObject prevGameObject;
  14. public override void OnStart()
  15. {
  16. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  17. if (currentGameObject != prevGameObject) {
  18. playableDirector = currentGameObject.GetComponent<PlayableDirector>();
  19. prevGameObject = currentGameObject;
  20. }
  21. }
  22. public override TaskStatus OnUpdate()
  23. {
  24. if (playableDirector == null) {
  25. Debug.LogWarning("PlayableDirector is null");
  26. return TaskStatus.Failure;
  27. }
  28. return playableDirector.state == PlayState.Playing ? TaskStatus.Success : TaskStatus.Failure;
  29. }
  30. public override void OnReset()
  31. {
  32. targetGameObject = null;
  33. }
  34. }
  35. }
  36. #endif