SetPan.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #if UNITY_4_6 || UNITY_4_7
  2. using UnityEngine;
  3. namespace BehaviorDesigner.Runtime.Tasks.Unity.UnityAudioSource
  4. {
  5. [TaskCategory("Unity/AudioSource")]
  6. [TaskDescription("Sets the pan value of the AudioSource. Returns Success.")]
  7. public class SetPan : Action
  8. {
  9. [Tooltip("The GameObject that the task operates on. If null the task GameObject is used.")]
  10. public SharedGameObject targetGameObject;
  11. [Tooltip("The pan value of the AudioSource")]
  12. public SharedFloat pan;
  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.pan = pan.Value;
  30. return TaskStatus.Success;
  31. }
  32. public override void OnReset()
  33. {
  34. targetGameObject = null;
  35. pan = 1;
  36. }
  37. }
  38. }
  39. #endif