SetAudioClip.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityAudioSource
  3. {
  4. [TaskCategory("Unity/AudioSource")]
  5. [TaskDescription("Sets the clip value of the AudioSource. Returns Success.")]
  6. public class SetAudioClip : Action
  7. {
  8. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  9. public SharedGameObject targetGameObject;
  10. [Tooltip("The AudioSource clip")]
  11. public AudioClip audioClip;
  12. private AudioSource audioSource;
  13. private GameObject prevGameObject;
  14. public override void OnStart()
  15. {
  16. var currentGameObject = GetDefaultGameObject(targetGameObject.Value);
  17. if (currentGameObject != prevGameObject) {
  18. audioSource = currentGameObject.GetComponent<AudioSource>();
  19. prevGameObject = currentGameObject;
  20. }
  21. }
  22. public override TaskStatus OnUpdate()
  23. {
  24. if (audioSource == null) {
  25. Debug.LogWarning("AudioSource is null");
  26. return TaskStatus.Failure;
  27. }
  28. audioSource.clip = audioClip;
  29. return TaskStatus.Success;
  30. }
  31. public override void OnReset()
  32. {
  33. targetGameObject = null;
  34. audioClip = null;
  35. }
  36. }
  37. }