FANDOM


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). Also, NewCamera and RenderTexture fields don't currently use constantIDs right now (so they will be lost if the camera or Render texture is deleted from the scene).

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

Note that CameraFarPlane and CameraNearPlane should be renamed CameraBackPlane and CameraFrontPlane in older version of Unity 5.6

using System;
using UnityEngine;

#if UNITY_5_6_OR_NEWER
using UnityEngine.Video;
#endif
 
 #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;

#if UNITY_5_6_OR_NEWER
        public VideoSource VideoSource = VideoSource.VideoClip;
        public VideoClip Clip;
        public VideoRenderMode RenderMode = VideoRenderMode.RenderTexture;
        public VideoAspectRatio AspectRatio = VideoAspectRatio.FitHorizontally;
        public VideoAudioOutputMode OutputMode = VideoAudioOutputMode.Direct;
        private VideoPlayer _videoPlayer;
#endif
         public string Url = "";
        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 bool UseWaitTime = true;
        public bool UseCustomWaitTime;
        public float CustomWaitTime;

        public GameObject Target;
        public int constantID;



        public override void AssignValues()
        {
             Target = AssignFile(constantID, Target);
        }
 
         public override float Run()
        {
 #if UNITY_5_6_OR_NEWER

            if (!isRunning)
            {
                 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;
                }

                var playTime = 0f;

                if ((CurrentAction == PlayerActions.Play && PlayChangesSettings) ||
                    CurrentAction == PlayerActions.ChangeSettings)
                {
                    switch (VideoSource)
                    {
                        case VideoSource.VideoClip:
                            _videoPlayer.source = VideoSource.VideoClip;
                            if (Clip != null)
                            {
                                _videoPlayer.clip = Clip;
                                if(UseWaitTime) playTime = (float)_videoPlayer.clip.length;
                            }
                            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.CameraFarPlane:
                        case VideoRenderMode.CameraNearPlane:
                            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;
                }

                if (UseWaitTime && UseCustomWaitTime)
                {
                    playTime = CustomWaitTime;
                }

                isRunning = true;
                return playTime;
            }
             else
            {
                 isRunning = false;
                return 0f;
            }
 #endif
 
         }
 
         void GetSettingsFromPlayer()
        {
 #if UNITY_5_6_OR_NEWER
            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);
#endif
         }
 
 #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);

#if UNITY_5_6_OR_NEWER
            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);
                        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.CameraFarPlane:
                    case VideoRenderMode.CameraNearPlane:
                        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();
            UseWaitTime = EditorGUILayout.Toggle("Wait until finished?", UseWaitTime);
            UseCustomWaitTime = EditorGUILayout.Toggle("WaitTime not Clip length", UseCustomWaitTime);
            if (UseCustomWaitTime)
            {
                 CustomWaitTime = EditorGUILayout.FloatField("Wait time:", CustomWaitTime);
            }
 
             EditorGUILayout.Separator();
            if (OutputMode == VideoAudioOutputMode.Direct && CurrentAction != PlayerActions.Stop)
                EditorGUILayout.HelpBox("Currently Mute and Audio volume only affect track 0.", MessageType.Info);

#else
             EditorGUILayout.HelpBox("Warning. Unity version not supported!", MessageType.Warning);
#endif
 
             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
         }
     }

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.