//============= Copyright (c) Ludic GmbH, All rights reserved. ============== // // Purpose: Part of the My Behaviour Tree Controller Code // //============================================================================= using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Video; using UnityEngine.UI; using MyBT; #if UNITY_EDITOR using UnityEditor; [CustomEditor(typeof(NamedAudioSource))] public class NamedAudioSourceInspector : ComponentHandlerInspector { } #endif [System.Serializable] public class NamedAudioSource : ComponentHandler { public override string TypeLabel () { return "AudioSource"; } public AudioSource audioSourceComponent; public bool playInBackground = false; public override string ContentLabel() { UpdateComponent(); return audioSourceComponent.clip.name; } public override void UpdateComponent() { base.UpdateComponent(); audioSourceComponent = GetComponent(); } public override string titleText { get { return "Run: Play Audio Source"; } } public override string[][] helpText { get { return new string[][] { new string[] {"Play AudioSource", "Wait until sound finished\nor PlayInBackground!", $"BTC.Run(\"{roomId}\", \"{gameObject.name}\")"}, new string[] {"Abort Playing in Background", null, $"BTC.Abort(\"{roomId}\", \"{gameObject.name}\")"}, new string[] {"FadeIn", null, $"BTC.FadeIn(\"{roomId}\", \"{gameObject.name}\")"}, new string[] {"FadeOut", null, $"BTC.FadeOut(\"{roomId}\", \"{gameObject.name}\")"}, //new string[] {"Play in Background", null, $"BTC.Show(\"{roomId}\", \"{gameObject.name}\")"}, //new string[] {"Stop playing in Background", null, $"BTC.Hide(\"{roomId}\", \"{gameObject.name}\")"}, }; } } public override void Run(MyBT.NodeState nodeState) { // whan aborting if (nodeState == NodeState.Aborting) { audioSourceComponent.Stop(); return; } // at start if (nodeState == NodeState.FirstRun) { // reset event trigger audioSourceComponent.Play(); if (playInBackground) { Task.SetSucceeded(); return; } } // during runtime if (nodeState == NodeState.Running) { if (!audioSourceComponent.isPlaying) { // || runInBackground Task.SetSucceeded(); return; } } } public override void Abort(MyBT.NodeState nodeState) { // whan aborting if (nodeState == NodeState.Aborting) { audioSourceComponent.Stop(); return; } // at start if (nodeState == NodeState.FirstRun) { audioSourceComponent.Stop(); Task.SetSucceeded(); return; } // during runtime if (nodeState == NodeState.Running) { Task.SetSucceeded(); return; } } public float fadeToVolume = 1.0f; #region both values setting public override void SetAlpha(float alpha) { if (audioSourceComponent != null) { audioSourceComponent.volume = (1 - alpha) * fadeToVolume; } } public override float GetAlpha() { if (audioSourceComponent != null) { if (fadeToVolume == 0) return 0; return 1 - audioSourceComponent.volume / fadeToVolume; } return 0; } #endregion //public override void Show(MyBT.NodeState nodeState) { // switch (nodeState) { // case NodeState.FirstRun: // audioSourceComponent.Play(); // goto case NodeState.Running; // case NodeState.Running: // Task.SetSucceeded(); // break; // case NodeState.Aborting: // break; // } //} //public override void Hide(MyBT.NodeState nodeState) { // //Debug.Log($"NamedDepthkitPlayer.Hide {nodeState}"); // switch (nodeState) { // case NodeState.FirstRun: // audioSourceComponent.Stop(); // goto case NodeState.Running; // case NodeState.Running: // Task.SetSucceeded(); // break; // case NodeState.Aborting: // break; // case NodeState.NotRunning: // break; // } //} }