Adventure Creator Wikia
Advertisement

Description: a WIP custom action to control the new Video Player Unity Component. It can Play/Stop or change the settings of a video player. Should be able to modify most settings at runtime (untested though), the only settings not in yet are settings related to the Material Override Render mode (will add them later).

To use this custom action you have to create a new text file and name it ActionManageVideoPlayer.cs then add it to your custom actions folder which can be setup in the Action manager (or you can drop it on the Actions folder inside the Adventure Creator folders).

using System;
using UnityEngine;
using UnityEngine.Video;
#if UNITY_EDITOR
using UnityEditor;
#endif
 
 namespace AC
 {
 
     [System.Serializable]
    public class ActionManageVideoPlayer : Action
     {
         public enum PlayerActions
        {
             Play,Stop,ChangeSettings
        }
 
         public ActionManageVideoPlayer()
        {
             this.isDisplayed = true;
            category = ActionCategory.Custom;
            title = "Manage Video Player";
            description = "Plays, stops, or changes a Video Player settings.";
        }
 
         public PlayerActions CurrentAction = PlayerActions.Play;


        public VideoSource VideoSource = VideoSource.VideoClip;
        public VideoClip Clip;
        public string Url = "";

        public VideoRenderMode RenderMode = VideoRenderMode.RenderTexture;
        public VideoAspectRatio AspectRatio = VideoAspectRatio.FitHorizontally;
        public VideoAudioOutputMode OutputMode = VideoAudioOutputMode.Direct;

        public bool PlayChangesSettings;
        public bool WaitFirstFrame = true;
        public bool Loop;
        public bool Mute;
        public float PlaybackSpeed = 1f;
        public float AudioVolume = 1f;

        public RenderTexture RenderTexture;
        public Camera NewCamera;
        public float Alpha = 1f;

        public GameObject Target;
        public int constantID;

        private VideoPlayer _videoPlayer;

        public override void AssignValues()
        {
             Target = AssignFile(constantID, Target);
        }
 
         public override float Run()
        {
             if (Target == null)
            {
                 Debug.LogWarning("Video Player object is not assigned!");
                return 0f;
            }
             _videoPlayer = Target.GetComponent<VideoPlayer>();

            if (_videoPlayer == null)
            {
                 Debug.LogWarning("Could not find a Video Player component!");
                return 0f;
            }
 
             if ((CurrentAction == PlayerActions.Play && PlayChangesSettings) ||
                CurrentAction == PlayerActions.ChangeSettings)
            {
                 switch (VideoSource)
                {
                    case VideoSource.VideoClip:
                        _videoPlayer.source = VideoSource.VideoClip;
                        if (Clip != null) _videoPlayer.clip = Clip;
                        break;
                    case VideoSource.Url:
                        _videoPlayer.source = VideoSource.Url;
                        if (!Url.Equals(""))
                        {
                            _videoPlayer.url = Url;
                        }
                        else
                        {
                            Debug.LogWarning("You forgot to type an URL...");
                        }
                        break;
                }

                _videoPlayer.waitForFirstFrame = WaitFirstFrame;

                _videoPlayer.renderMode = RenderMode;
                switch (RenderMode)
                {
                    case VideoRenderMode.CameraBackPlane:
                    case VideoRenderMode.CameraFrontPlane:
                        if (NewCamera != null) _videoPlayer.targetCamera = NewCamera;
                        _videoPlayer.targetCameraAlpha = Alpha;
                        break;
                    case VideoRenderMode.RenderTexture:
                        if(RenderTexture!=null) _videoPlayer.targetTexture = RenderTexture;
                        break;
                }

                _videoPlayer.aspectRatio = AspectRatio;
                _videoPlayer.audioOutputMode = OutputMode;
                _videoPlayer.isLooping = Loop;
                _videoPlayer.SetDirectAudioMute(0,Mute);
                _videoPlayer.playbackSpeed = PlaybackSpeed;
                _videoPlayer.SetDirectAudioVolume(0, AudioVolume);
            }
 
             switch (CurrentAction)
            {
                 case PlayerActions.Play:
                    _videoPlayer.Play();
                    break;
                case PlayerActions.Stop:
                    _videoPlayer.Stop();
                    break;
            }
 
             return 0f;

        }
 
         void GetSettingsFromPlayer()
        {
             if (Target == null)
            {
                 Debug.LogWarning("Video Player object is not assigned!");
                return;
            }
             //get current settings from the Video Player
            _videoPlayer = Target.GetComponent<VideoPlayer>();
            if (_videoPlayer == null)
            {
                 Debug.LogWarning("Could not find a Video Player component!");
                return;
            }
             RenderTexture = _videoPlayer.targetTexture;
            VideoSource = _videoPlayer.source;
            Clip = _videoPlayer.clip;
            Url = _videoPlayer.url;
            WaitFirstFrame = _videoPlayer.waitForFirstFrame;
            Loop= _videoPlayer.isLooping;
            PlaybackSpeed = _videoPlayer.playbackSpeed;
            RenderMode = _videoPlayer.renderMode;
            NewCamera = _videoPlayer.targetCamera;
            Alpha = _videoPlayer.targetCameraAlpha;
            AspectRatio = _videoPlayer.aspectRatio;
            OutputMode = _videoPlayer.audioOutputMode;
            Mute = _videoPlayer.GetDirectAudioMute(0);
            AudioVolume = _videoPlayer.GetDirectAudioVolume(0);
        }
 
 #if UNITY_EDITOR

        override public void ShowGUI()
        {
             EditorGUILayout.HelpBox("Only 'Change Settings' and 'Play' actions are allowed to change a Video Player's settings.", MessageType.Info);

            EditorGUILayout.Separator();

            Target = (GameObject)EditorGUILayout.ObjectField("Video player object:", Target, typeof(GameObject), true);
            constantID = FieldToID(Target, constantID);
            Target = IDToField(Target, constantID, true);

            EditorGUILayout.Separator();
            CurrentAction = (PlayerActions)EditorGUILayout.EnumPopup("Action:", CurrentAction);

            if (CurrentAction == PlayerActions.Play)
                PlayChangesSettings = EditorGUILayout.ToggleLeft("Change player settings?", PlayChangesSettings);

            if ((CurrentAction == PlayerActions.Play && PlayChangesSettings) ||
                CurrentAction == PlayerActions.ChangeSettings)
            {
                 VideoSource = (VideoSource)EditorGUILayout.EnumPopup("Video Source:",VideoSource);
                switch (VideoSource)
                {
                    case VideoSource.VideoClip:
                        Clip = (VideoClip)EditorGUILayout.ObjectField("Video Clip", Clip, typeof(VideoClip), false);
                        break;
                    case VideoSource.Url:
                        Url = EditorGUILayout.TextArea("URL:", Url);
                        break;
                }

                WaitFirstFrame = EditorGUILayout.Toggle("Wait for first frame:", WaitFirstFrame);
                Loop = EditorGUILayout.Toggle("Loop:", Loop);
                PlaybackSpeed = EditorGUILayout.Slider("Playback Speed:", PlaybackSpeed, 0f, 10f);
                EditorGUILayout.Separator();
                RenderMode = (VideoRenderMode)EditorGUILayout.EnumPopup("Render Mode:",RenderMode);
                switch (RenderMode)
                {
                    case VideoRenderMode.CameraBackPlane:
                    case VideoRenderMode.CameraFrontPlane:
                        NewCamera = (Camera)EditorGUILayout.ObjectField("New Camera:", NewCamera, typeof(Camera), true);
                        Alpha = EditorGUILayout.Slider("Alpha:", Alpha, 0f, 1f);
                        break;
                        case VideoRenderMode.APIOnly:
                        case VideoRenderMode.MaterialOverride:
                            EditorGUILayout.HelpBox("Not currently supported. Please change settings related to this mode directly in the video player.", MessageType.Info);
                        break;
                    case VideoRenderMode.RenderTexture:
                        RenderTexture = (RenderTexture)EditorGUILayout.ObjectField("Render Texture:", RenderTexture, typeof(RenderTexture), true);
                        break;

                }

                EditorGUILayout.Separator();

                AspectRatio = (VideoAspectRatio)EditorGUILayout.EnumPopup("Aspect Ratio:",AspectRatio);
                OutputMode = (VideoAudioOutputMode)EditorGUILayout.EnumPopup("Output Mode:",OutputMode);
                if (OutputMode == VideoAudioOutputMode.Direct)
                {
                    Mute = EditorGUILayout.Toggle("Mute", Mute);
                    AudioVolume = EditorGUILayout.Slider("Audio Volume:", AudioVolume, 0f, 1f);
                }
            }
 
             EditorGUILayout.Separator();
            if (CurrentAction != PlayerActions.Stop)
                if (GUILayout.Button("Catch current Video Player settings"))
            {
                 GetSettingsFromPlayer();
            }
 
             EditorGUILayout.Separator();
            if (OutputMode == VideoAudioOutputMode.Direct && CurrentAction != PlayerActions.Stop)
                EditorGUILayout.HelpBox("Currently Mute and Audio volume only affect track 0.", MessageType.Info);

            AfterRunningOption();
        }
 
 
         public override string SetLabel()
        {
             // Return a string used to describe the specific action's job.

            string labelAdd = "Plays, stops, or changes a Video Player settings.";
            return labelAdd;
        }
 
 #endif
     }
 }
Advertisement