//============= 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(NamedVideoPlayer))] public class NamedVideoPlayerInspector : ComponentHandlerInspector { } #endif [System.Serializable] public class NamedVideoPlayer : ComponentHandler { public override string TypeLabel () { return "VideoPlayer"; } public override string ContentLabel() { UpdateComponent(); return videoPlayerComponent.clip.name; } public override void UpdateComponent() { base.UpdateComponent(); videoPlayerComponent = GetComponent(); } public VideoPlayer videoPlayerComponent; public override string titleText { get { return "Run Video, Show/Hide, FadeIn/FadeOut"; } } public override string[][] helpText { get { return new string[][] { new string[] {"Run", null, $"BTC.Run(\"{roomId}\", \"{gameObject.name}\")"}, new string[] {"Show", null, $"BTC.Show(\"{roomId}\", \"{gameObject.name}\")"}, new string[] {"Hide", null, $"BTC.Hide(\"{roomId}\", \"{gameObject.name}\")"}, new string[] {"FadeIn", null, $"BTC.FadeIn(\"{roomId}\", \"{gameObject.name}\")"}, new string[] {"FadeOut", null, $"BTC.FadeOut(\"{roomId}\", \"{gameObject.name}\")"}, }; } } public override void Run(MyBT.NodeState nodeState) { Task.log = $"isPrepared: {videoPlayerComponent.isPrepared} isPlaying:{videoPlayerComponent.isPlaying}"; // whan aborting if (nodeState == NodeState.Aborting) { videoPlayerComponent.Stop(); return; } // at start if (nodeState == NodeState.FirstRun) { // reset event trigger videoPlayerComponent.Play(); return; } // during runtime if (nodeState == NodeState.Running) { if (videoPlayerComponent.isPrepared) { if (!videoPlayerComponent.isPlaying) { Task.SetSucceeded(); return; } } } } // public override void Set(NodeState nodeState, string key, string value) { // if (nodeState == NodeState.FirstRun) { // if (key == "text") { // text.text = value; // Task.SetSucceeded(); // return; // } // } // Task.SetFailed(); // } #region both values setting public override void SetAlpha(float alpha) { if (videoPlayerComponent != null) { videoPlayerComponent.targetCameraAlpha = 1 - alpha; } } public override float GetAlpha() { if (videoPlayerComponent != null) { return 1-videoPlayerComponent.targetCameraAlpha; } return 0; } #endregion enum VideoState { Undefined, Initialize, FadeIn, Playing, FadeOut, Stopping } // public void PlayVideo(NodeState nodeState) { // , bool waitUntilFinished // switch (nodeState) { // case NodeState.FirstRun: // equals isStarting // // Debug.Log("PlayVideo: starting " + roomId + " " + videoName + " " + Time.frameCount); // // Task.data = VideoState.Initialize; // videoPlayerComponent.Play(); // break; // case NodeState.Running: // if (videoPlayerComponent.isPrepared) { // if (!videoPlayerComponent.isPlaying) { // Task.SetSucceeded(); // return; // } // } // break; // case NodeState.Aborting: // videoPlayerComponent.Stop(); // // Debug.Log("PlayVideo: aborting " + roomId + " " + videoName + " " + Time.frameCount); // // Task.data = VideoState.Stopping; // break; // } // if (Input.GetKeyDown(KeyCode.Backspace)) { // // Debug.Log("PlayVideo: skipping " + roomId + " " + videoName + " " + Time.frameCount); // Task.data = VideoState.Stopping; // } // Task.log = "" + Task.data.ToString(); // switch ((VideoState)Task.data) { // case VideoState.Initialize: // // Debug.Log("VideoState.Initialize " + Time.frameCount); // // HideNow(); // //Task.data = VideoState.FadeIn; // videoPlayerComponent.Prepare(); // videoPlayerComponent.StepForward(); // //player.video.frame = 1; // if (videoPlayerComponent.isPrepared) { // Debug.Log("isPrepared"); // Task.data = VideoState.FadeIn; // // FadeInStart(); // } // break; // case VideoState.FadeIn: // // Debug.Log("VideoState.FadeIn " + Time.frameCount); // if (isVisible) { // // Debug.Log("PlaybackStart " + Time.time); // ShowNow(); // videoPlayerComponent.Play(); // Task.data = VideoState.Playing; // } // break; // case VideoState.Playing: // // Debug.Log("VideoState.Playing " + Time.frameCount); // // video has stopped playing // if (!videoPlayerComponent.isPlaying && videoPlayerComponent.isPrepared) { // //player.video.Stop(); // ShowNow(); // FadeOutStart(); // // switch to fade out // Task.data = VideoState.FadeOut; // } // break; // case VideoState.FadeOut: // // Debug.Log("VideoState.FadeOut " + Time.frameCount); // if (isHidden) { // HideNow(); // Task.data = VideoState.Undefined; // // Debug.Log("PlayVideo: finished " + roomId + " " + videoName); // Task.SetSucceeded(); // } // break; // case VideoState.Stopping: // case VideoState.Undefined: // // Debug.Log("VideoState.Stopping/Undefined " + Time.frameCount); // HideNow(); // videoPlayerComponent.Stop(); // Task.data = VideoState.Undefined; // Task.SetSucceeded(); // break; // } // } // public void IsFinished(NodeState nodeState) { // // on start, play // Task.log = "playing=" + videoPlayerComponent.isPlaying + " isPrepared=" + videoPlayerComponent.isPrepared; // if (Task.isStarting) { // Debug.Log("WaitVideoFinished.isStarting : waiting for video to finish=" + name); // } // else { // //if (!player.video.isPrepared) { // // // not even prepared // // return; // //} // // if stopped playing // if (!videoPlayerComponent.isPlaying) { // FadeOutStart(); // Task.SetSucceeded(); // } // } // } // public override void Abort(NodeState nodeState) { // // on start, play // Task.log = "playing=" + videoPlayerComponent.isPlaying + " isPrepared=" + videoPlayerComponent.isPrepared; // if (nodeState == NodeState.FirstRun) { // Debug.Log("AbortVideo.isStarting : waiting for video to finish=" + name); // FadeOutStart(); // videoPlayerComponent.Stop(); // Task.SetSucceeded(); // } // } // public float sourceAlpha; // public float targetAlpha; // public float timeSum; // public float timeScale = 0.3f; // private float visibleAlpha = Color.white.a; // private float hiddenAlpha = Color.clear.a; // public override void Update() { // base.Update(); // if (timeSum < timeScale) { // timeSum += Time.deltaTime; // objectAlpha = Mathf.Lerp(sourceAlpha, targetAlpha, timeSum / timeScale); // }; // } // public void FadeInStart() { // sourceAlpha = objectAlpha; // targetAlpha = visibleAlpha; // // actually should be -1, but because of an error with the first frame, we need more time // timeSum = -3*Time.deltaTime; // } // public void FadeOutStart() { // sourceAlpha = objectAlpha; // targetAlpha = hiddenAlpha; // // actually should be -1, but because of an error with the first frame, we need more time // timeSum = -3*Time.deltaTime; // } // public void ShowNow() { // sourceAlpha = Color.white.a; // targetAlpha = Color.white.a; // timeSum = 1; // objectAlpha = targetAlpha; // } // public void HideNow() { // sourceAlpha = Color.clear.a; // targetAlpha = Color.clear.a; // timeSum = 1; // objectAlpha = targetAlpha; // } // public bool isHidden { // get { // return (objectAlpha == hiddenAlpha); // } // } // public bool isVisible { // get { // return (objectAlpha == visibleAlpha); // } // } // public override void Update() { // base.Update(); // } // public float objectAlpha { // get { // if (useRenderer) { // return renderer.sharedMaterial.color.a; // } // else if (useRawImage) { // return rawImage.color.a; // } // else { // return videoPlayerComponent.targetCameraAlpha; // } // } // set { // // Debug.Log("NamedVideoPlayer.objectAlpha: "+value); // if (useRenderer) { // renderer.sharedMaterial.color = new Color( // renderer.sharedMaterial.color.r, // renderer.sharedMaterial.color.g, // renderer.sharedMaterial.color.b, // value // ); // } // else if (useRawImage) { // // clear texture when full alpha because of first frame errors (doenst fix it tough) // if (value == 0) { // rawImage.texture = null; // } // else { // rawImage.texture = videoPlayerComponent.targetTexture; // } // rawImage.color = new Color( // rawImage.color.r, // rawImage.color.g, // rawImage.color.b, // value // ); // } // else { // videoPlayerComponent.targetCameraAlpha = value; // } // } // } }