コード例 #1
0
ファイル: MoviePlayer.cs プロジェクト: yuka800/UnityLearning
        /// <summary>
        /// Plays a movie with the provided name; returns when the playback finishes.
        /// </summary>
        public async Task PlayAsync(string movieName, CancellationToken cancellationToken = default)
        {
            if (IsPlaying)
            {
                Stop();
            }

            playedMovieName = movieName;
            playCTS         = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

            OnMoviePlay?.Invoke();
            await waitForFade;

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            #if UNITY_WEBGL && !UNITY_EDITOR
            Player.url = PathUtils.Combine(Application.streamingAssetsPath, videoLoader.BuildFullPath(movieName)) + ".mp4";
            #else
            var videoClipResource = await videoLoader.LoadAsync(movieName);

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }
            if (!videoClipResource.IsValid)
            {
                Debug.LogError($"Failed to load `{movieName}` movie."); Stop(); return;
            }
            Player.clip = videoClipResource;
            videoClipResource.Hold(this);
            #endif

            Player.Prepare();
            while (!Player.isPrepared)
            {
                await waitForEndOfFrame;
            }
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }
            OnMovieTextureReady?.Invoke(Player.texture);

            Player.Play();
            while (IsPlaying)
            {
                await waitForEndOfFrame;
            }
        }
コード例 #2
0
        public virtual async UniTask PlayAsync(string movieName, CancellationToken cancellationToken = default)
        {
            if (Playing)
            {
                Stop();
            }

            playedMovieName = movieName;
            playCTS         = cancellationToken.CreateLinkedTokenSource();

            OnMoviePlay?.Invoke();
            await UniTask.Delay(TimeSpan.FromSeconds(Configuration.FadeDuration));

            if (cancellationToken.CancelASAP)
            {
                return;
            }

            #if UNITY_WEBGL && !UNITY_EDITOR
            Player.url = PathUtils.Combine(Application.streamingAssetsPath, $"{Configuration.Loader.PathPrefix}/{movieName}") + streamExtension;
            #else
            var videoClipResource = await videoLoader.LoadAndHoldAsync(movieName, this);

            if (cancellationToken.CancelASAP)
            {
                return;
            }
            if (!videoClipResource.Valid)
            {
                throw new Exception($"Failed to load `{movieName}` movie.");
            }
            Player.clip = videoClipResource;
            #endif

            Player.Prepare();
            while (!Player.isPrepared)
            {
                await AsyncUtils.WaitEndOfFrame;
            }
            if (cancellationToken.CancelASAP)
            {
                return;
            }
            OnMovieTextureReady?.Invoke(Player.texture);

            Player.Play();
            while (Playing)
            {
                await AsyncUtils.WaitEndOfFrame;
            }
        }