SetScheduledEndTime.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityAudioSource
  3. {
  4. [TaskCategory("Unity/AudioSource")]
  5. [TaskDescription("Changes the time at which a sound that has already been scheduled to play will end. Notice that depending on the " +
  6. "timing not all rescheduling requests can be fulfilled. Returns Success.")]
  7. public class SetScheduledEndTime : Action
  8. {
  9. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  10. public SharedGameObject targetGameObject;
  11. [Tooltip("Time in seconds")]
  12. public SharedFloat time = 0;
  13. private AudioSource audioSource;
  14. private GameObject prevGameObject;
  15. public override void OnStart()
  16. {
  17. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  18. if (currentGameObject != prevGameObject) {
  19. audioSource = currentGameObject.GetComponent<AudioSource>();
  20. prevGameObject = currentGameObject;
  21. }
  22. }
  23. public override TaskStatus OnUpdate()
  24. {
  25. if (audioSource == null) {
  26. Debug.LogWarning("AudioSource is null");
  27. return TaskStatus.Failure;
  28. }
  29. audioSource.SetScheduledEndTime(time.Value);
  30. return TaskStatus.Success;
  31. }
  32. public override void OnReset()
  33. {
  34. targetGameObject = null;
  35. time = 0;
  36. }
  37. }
  38. }