using System.Collections; using System.Collections.Generic; using UnityEngine; public class AvProMovieState : MonoBehaviour { [System.Serializable] public class MovieState { public string StateName; public bool StateMovieIsloop; public int StartFrame; public int EndFrame; public delegate void StateBehaiver(); public StateBehaiver Doing; } [SerializeField] protected List StateList; [SerializeField] protected MovieState currentMovieSate; protected DShowMoviePlayer movie; protected uint MovieFrame { set { movie.Frame = value; } get { return movie.Frame; } } protected Coroutine DoCurrentFramesCor = null; // Update is called once per frame protected void Update () { if (currentMovieSate.Doing != null) currentMovieSate.Doing(); DoCurrentMovieFrame(); } protected void SetState(MovieState movieState) { if (currentMovieSate == movieState) return; if (DoCurrentFramesCor != null) StopCoroutine(DoCurrentFramesCor); currentMovieSate = movieState; MovieFrame = (uint)currentMovieSate.StartFrame; if (MovieFrame != currentMovieSate.StartFrame) { DoCurrentFramesCor = StartCoroutine(DoCurrentStartFrame(currentMovieSate.StartFrame, MovieFrame > currentMovieSate.StartFrame)); } movie.Play(); } protected void DoCurrentMovieFrame() { if (currentMovieSate == null) return; if (MovieFrame >= currentMovieSate.EndFrame) { if (currentMovieSate.StateMovieIsloop) { MovieFrame = (uint)currentMovieSate.StartFrame; if (MovieFrame != currentMovieSate.StartFrame) { if (DoCurrentFramesCor != null) StopCoroutine(DoCurrentFramesCor); DoCurrentFramesCor = StartCoroutine(DoCurrentStartFrame(currentMovieSate.StartFrame, MovieFrame > currentMovieSate.StartFrame)); } } else { movie.Pause(); MovieFrame = (uint)currentMovieSate.EndFrame; } } if (MovieFrame < currentMovieSate.StartFrame) { MovieFrame = (uint)currentMovieSate.StartFrame; if (MovieFrame != currentMovieSate.StartFrame) { if (DoCurrentFramesCor != null) StopCoroutine(DoCurrentFramesCor); DoCurrentFramesCor = StartCoroutine(DoCurrentStartFrame(currentMovieSate.StartFrame, MovieFrame > currentMovieSate.StartFrame)); } } if (!currentMovieSate.StateMovieIsloop) return; if (!movie.IsPlaying) { movie.Play(); } } IEnumerator DoCurrentStartFrame(int currentFrame, bool isBigger) { float adjust_frame = 0; if (isBigger) { while (MovieFrame > (uint)(currentFrame + adjust_frame)) { adjust_frame++; MovieFrame = (uint)(currentFrame + adjust_frame); if ((currentFrame + adjust_frame) > currentMovieSate.EndFrame) { yield break; } yield return new WaitForSeconds(3.0f * movie.GetDuration / movie.TotalNumFrames); } } else { while (MovieFrame < (uint)(currentFrame + adjust_frame)) { adjust_frame++; MovieFrame = (uint)(currentFrame + adjust_frame); if ((currentFrame + adjust_frame) > currentMovieSate.EndFrame) { yield break; } yield return new WaitForSeconds(3.0f * movie.GetDuration / movie.TotalNumFrames); } } yield return null; } }