private void LoadSettings()
    {
        _expandConfig     = EditorPrefs.GetBool(SettingsPrefix + "ExpandConfig", true);
        _sourceType       = (SourceType)EditorPrefs.GetInt(SettingsPrefix + "SourceType", (int)_sourceType);
        _cameraName       = EditorPrefs.GetString(SettingsPrefix + "CameraName", string.Empty);
        _cameraFastPixel  = EditorPrefs.GetBool(SettingsPrefix + "CameraFastPixel", true);
        _captureModeIndex = EditorPrefs.GetInt(SettingsPrefix + "CaptureModeIndex", 0);

        _autoFilenamePrefixFromSceneName = EditorPrefs.GetBool(SettingsPrefix + "AutoFilenamePrefixFromScenename", _autoFilenamePrefixFromSceneName);
        _autoFilenamePrefix    = EditorPrefs.GetString(SettingsPrefix + "AutoFilenamePrefix", "capture");
        _autoFilenameExtension = EditorPrefs.GetString(SettingsPrefix + "AutoFilenameExtension", "avi");
        _appendTimestamp       = EditorPrefs.GetBool(SettingsPrefix + "AppendTimestamp", true);

        _outputFolderIndex    = EditorPrefs.GetInt(SettingsPrefix + "OutputFolderIndex", 0);
        _outputFolderRelative = EditorPrefs.GetString(SettingsPrefix + "OutputFolderRelative", string.Empty);
        _outputFolderAbsolute = EditorPrefs.GetString(SettingsPrefix + "OutputFolderAbsolute", string.Empty);

        _downScaleIndex  = EditorPrefs.GetInt(SettingsPrefix + "DownScaleIndex", 0);
        _downscaleX      = EditorPrefs.GetInt(SettingsPrefix + "DownScaleX", 1);
        _downscaleY      = EditorPrefs.GetInt(SettingsPrefix + "DownScaleY", 1);
        _frameRate       = (AVProMovieCaptureBase.FrameRate)System.Enum.Parse(typeof(AVProMovieCaptureBase.FrameRate), EditorPrefs.GetString(SettingsPrefix + "FrameRate", "Thirty"));
        _videoCodecIndex = EditorPrefs.GetInt(SettingsPrefix + "VideoCodecIndex", 0);

        _captureAudio     = EditorPrefs.GetBool(SettingsPrefix + "CaptureAudio", false);
        _audioCodecIndex  = EditorPrefs.GetInt(SettingsPrefix + "AudioCodecIndex", 0);
        _audioDeviceIndex = EditorPrefs.GetInt(SettingsPrefix + "AudioDeviceIndex", 0);

        if (!string.IsNullOrEmpty(_cameraName))
        {
            Camera[] cameras = (Camera[])GameObject.FindObjectsOfType(typeof(Camera));
            foreach (Camera cam in cameras)
            {
                if (cam.name == _cameraName)
                {
                    _cameraNode = cam;
                    break;
                }
            }
        }

        // Check ranges
        if (_videoCodecIndex >= _videoCodecNames.Length)
        {
            _videoCodecIndex = 0;
        }
        if (_audioDeviceIndex >= _audioDeviceNames.Length)
        {
            _audioDeviceIndex = 0;
            _captureAudio     = false;
        }
        if (_audioCodecIndex >= _audioCodecNames.Length)
        {
            _audioCodecIndex = 0;
            _captureAudio    = false;
        }
    }
 private void ResetSettings()
 {
     _expandConfig     = true;
     _sourceType       = SourceType.Scene;
     _cameraNode       = null;
     _cameraName       = string.Empty;
     _cameraFastPixel  = true;
     _captureModeIndex = 0;
     _autoFilenamePrefixFromSceneName = true;
     _autoFilenamePrefix    = "capture";
     _autoFilenameExtension = "avi";
     _outputFolderIndex     = 0;
     _outputFolderRelative  = string.Empty;
     _outputFolderAbsolute  = string.Empty;
     _appendTimestamp       = true;
     _downScaleIndex        = 0;
     _downscaleX            = 1;
     _downscaleY            = 1;
     _frameRate             = AVProMovieCaptureBase.FrameRate.Thirty;
     _videoCodecIndex       = 0;
     _captureAudio          = false;
     _audioCodecIndex       = 0;
     _audioDeviceIndex      = 0;
 }
 private void ResetSettings()
 {
     _expandConfig = true;
     _sourceType = SourceType.Scene;
     _cameraNode = null;
     _cameraName = string.Empty;
     _cameraFastPixel = true;
     _captureModeIndex = 0;
     _autoFilenamePrefixFromSceneName = true;
     _autoFilenamePrefix = "capture";
     _autoFilenameExtension = "avi";
     _outputFolderIndex = 0;
     _outputFolderRelative = string.Empty;
     _outputFolderAbsolute = string.Empty;
     _appendTimestamp = true;
     _downScaleIndex = 0;
     _downscaleX = 1;
     _downscaleY = 1;
     _frameRate = AVProMovieCaptureBase.FrameRate.Thirty;
     _videoCodecIndex = 0;
     _captureAudio = false;
     _audioCodecIndex = 0;
     _audioDeviceIndex = 0;
 }
    private void LoadSettings()
    {
        _expandConfig = EditorPrefs.GetBool(SettingsPrefix + "ExpandConfig", true);
        _sourceType = (SourceType)EditorPrefs.GetInt(SettingsPrefix + "SourceType", (int)_sourceType);
        _cameraName = EditorPrefs.GetString(SettingsPrefix + "CameraName", string.Empty);
        _cameraFastPixel = EditorPrefs.GetBool(SettingsPrefix + "CameraFastPixel", true);
        _captureModeIndex = EditorPrefs.GetInt(SettingsPrefix + "CaptureModeIndex", 0);

        _autoFilenamePrefixFromSceneName = EditorPrefs.GetBool(SettingsPrefix + "AutoFilenamePrefixFromScenename", _autoFilenamePrefixFromSceneName);
        _autoFilenamePrefix = EditorPrefs.GetString(SettingsPrefix + "AutoFilenamePrefix", "capture");
        _autoFilenameExtension = EditorPrefs.GetString(SettingsPrefix + "AutoFilenameExtension", "avi");
        _appendTimestamp = EditorPrefs.GetBool(SettingsPrefix + "AppendTimestamp", true);

        _outputFolderIndex = EditorPrefs.GetInt(SettingsPrefix + "OutputFolderIndex", 0);
        _outputFolderRelative = EditorPrefs.GetString(SettingsPrefix + "OutputFolderRelative", string.Empty);
        _outputFolderAbsolute = EditorPrefs.GetString(SettingsPrefix + "OutputFolderAbsolute", string.Empty);

        _downScaleIndex = EditorPrefs.GetInt(SettingsPrefix + "DownScaleIndex", 0);
        _downscaleX = EditorPrefs.GetInt(SettingsPrefix + "DownScaleX", 1);
        _downscaleY = EditorPrefs.GetInt(SettingsPrefix + "DownScaleY", 1);
        _frameRate = (AVProMovieCaptureBase.FrameRate)System.Enum.Parse(typeof(AVProMovieCaptureBase.FrameRate), EditorPrefs.GetString(SettingsPrefix + "FrameRate", "Thirty"));
        _videoCodecIndex = EditorPrefs.GetInt(SettingsPrefix + "VideoCodecIndex", 0);

        _captureAudio = EditorPrefs.GetBool(SettingsPrefix + "CaptureAudio", false);
        _audioCodecIndex = EditorPrefs.GetInt(SettingsPrefix + "AudioCodecIndex", 0);
        _audioDeviceIndex = EditorPrefs.GetInt(SettingsPrefix + "AudioDeviceIndex", 0);

        if (!string.IsNullOrEmpty(_cameraName))
        {
            Camera[] cameras = (Camera[])GameObject.FindObjectsOfType(typeof(Camera));
            foreach (Camera cam in cameras)
            {
                if (cam.name == _cameraName)
                {
                    _cameraNode = cam;
                    break;
                }
            }
        }

        // Check ranges
        if (_videoCodecIndex >= _videoCodecNames.Length)
        {
            _videoCodecIndex = 0;
        }
        if (_audioDeviceIndex >= _audioDeviceNames.Length)
        {
            _audioDeviceIndex = 0;
            _captureAudio = false;
        }
        if (_audioCodecIndex >= _audioCodecNames.Length)
        {
            _audioCodecIndex = 0;
            _captureAudio = false;
        }
    }
    private void DrawConfigGUI()
    {
        EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(_lastCapturePath));
        if (GUILayout.Button("Open Last Video"))
        {
            if (!ShowInExplorer(_lastCapturePath))
            {
                _lastCapturePath = string.Empty;
            }
        }
        EditorGUI.EndDisabledGroup();
        _expandConfig = EditorGUILayout.Foldout(_expandConfig, "Configure");
        if (_expandConfig)
        {
            GUILayout.Label("General", EditorStyles.boldLabel);
            EditorGUILayout.BeginVertical("box");
            EditorGUI.indentLevel++;

            _captureModeIndex = EditorGUILayout.Popup("Mode", _captureModeIndex, _captureModes);

            // Source
            EditorGUILayout.LabelField("Source", EditorStyles.boldLabel);
            EditorGUI.indentLevel++;
            _sourceType = (SourceType)EditorGUILayout.EnumPopup("Type", _sourceType);
            if (_sourceType == SourceType.Camera)
            {
                if (_cameraNode == null && Camera.main != null)
                {
                    _cameraNode = Camera.main;
                }
                _cameraNode = (Camera)EditorGUILayout.ObjectField("Camera", _cameraNode, typeof(Camera), true);

                _cameraFastPixel = EditorGUILayout.Toggle("Fast Pixel Format", _cameraFastPixel);
            }
            EditorGUI.indentLevel--;

            // File name
            EditorGUILayout.LabelField("File Name", EditorStyles.boldLabel);
            EditorGUI.indentLevel++;
            _autoFilenamePrefixFromSceneName = EditorGUILayout.Toggle("From Scene Name", _autoFilenamePrefixFromSceneName);
            if (_autoFilenamePrefixFromSceneName)
            {
                _autoFilenamePrefix = System.IO.Path.GetFileNameWithoutExtension(EditorApplication.currentScene);
                if (string.IsNullOrEmpty(_autoFilenamePrefix))
                {
                    _autoFilenamePrefix = "capture";
                }
            }
            EditorGUI.BeginDisabledGroup(_autoFilenamePrefixFromSceneName);
            _autoFilenamePrefix = EditorGUILayout.TextField("Prefix", _autoFilenamePrefix);
            EditorGUI.EndDisabledGroup();
            _autoFilenameExtension = EditorGUILayout.TextField("Extension", _autoFilenameExtension);
            _appendTimestamp = EditorGUILayout.Toggle("Append Timestamp", _appendTimestamp);
            EditorGUI.indentLevel--;

            // File path
            EditorGUILayout.LabelField("File Path", EditorStyles.boldLabel);
            EditorGUI.indentLevel++;
            _outputFolderIndex = EditorGUILayout.Popup("Relative to", _outputFolderIndex, _outputFolders);
            if (_outputFolderIndex == 0)
            {
                _outputFolderRelative = EditorGUILayout.TextField("SubFolder(s)", _outputFolderRelative);
            }
            else
            {
                EditorGUILayout.BeginHorizontal();
                _outputFolderAbsolute = EditorGUILayout.TextField("Path", _outputFolderAbsolute);
                if (GUILayout.Button(">", GUILayout.Width(22)))
                {
                    _outputFolderAbsolute = EditorUtility.SaveFolderPanel("Select Folder To Store Video Captures", System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, "../")), "");
                    EditorUtility.SetDirty(this);
                }
                EditorGUILayout.EndHorizontal();
            }
            EditorGUI.indentLevel--;

            EditorGUI.indentLevel--;
            EditorGUILayout.EndVertical();

            GUILayout.Label("Video", EditorStyles.boldLabel);
            EditorGUILayout.BeginVertical("box");
            EditorGUI.indentLevel++;

            if (_sourceType == 0)
            {
                // We can't just use Screen.width and Screen.height because Unity returns the size of this window
                // So instead we look for a camera with no texture target and a valid viewport
                int inWidth = 1;
                int inHeight = 1;
                foreach (Camera cam in Camera.allCameras)
                {
                    if (cam.targetTexture == null)
                    {
                        float rectWidth = Mathf.Clamp01(cam.rect.width + cam.rect.x) - Mathf.Clamp01(cam.rect.x);
                        float rectHeight = Mathf.Clamp01(cam.rect.height + cam.rect.y) - Mathf.Clamp01(cam.rect.y);
                        if (rectWidth > 0.0f && rectHeight > 0.0f)
                        {
                            inWidth = Mathf.FloorToInt(cam.pixelWidth / rectWidth);
                            inHeight = Mathf.FloorToInt(cam.pixelHeight / rectHeight);
                            //Debug.Log (rectWidth + "    " + (cam.rect.height - cam.rect.y) + " " + cam.pixelHeight + " = " + inWidth + "x" + inHeight);
                        }
                        break;
                    }
                }
                Vector2 outSize = AVProMovieCaptureBase.GetRecordingResolution(inWidth, inHeight, GetDownScaleFromIndex(_downScaleIndex), new Vector2(_downscaleX, _downscaleY));
                EditorGUILayout.LabelField("Output", (int)outSize.x + " x " + (int)outSize.y + " @ " + (int)_frameRate, EditorStyles.boldLabel);
            }
            else
            {
                if (_cameraNode)
                {
                    int inWidth = Mathf.FloorToInt(_cameraNode.pixelRect.width);
                    int inHeight = Mathf.FloorToInt(_cameraNode.pixelRect.height);
                    Vector2 outSize = AVProMovieCaptureBase.GetRecordingResolution(inWidth, inHeight, GetDownScaleFromIndex(_downScaleIndex), new Vector2(_downscaleX, _downscaleY));
                    EditorGUILayout.LabelField("Output", (int)outSize.x + " x " + (int)outSize.y + " @ " + (int)_frameRate, EditorStyles.boldLabel);
                }
            }

            _downScaleIndex = EditorGUILayout.Popup("Down Scale", _downScaleIndex, _downScales);
            if (_downScaleIndex == 5)
            {
                Vector2 maxVideoSize = new Vector2(_downscaleX, _downscaleY);
                maxVideoSize = EditorGUILayout.Vector2Field("Size", maxVideoSize);
                _downscaleX = Mathf.Clamp((int)maxVideoSize.x, 1, 4096);
                _downscaleY = Mathf.Clamp((int)maxVideoSize.y, 1, 4096);
            }

            _frameRate = (AVProMovieCaptureBase.FrameRate)EditorGUILayout.EnumPopup("Frame Rate", _frameRate);

            if (ShowConfigList("Codec", _videoCodecNames, _videoCodecConfigurable, ref _videoCodecIndex))
            {
                _queueConfigureVideoCodec = Mathf.Max(-1, _videoCodecIndex - 2);
            }

            EditorGUI.indentLevel--;
            EditorGUILayout.EndVertical();

            EditorGUI.BeginDisabledGroup(_captureModeIndex != 0);
            GUILayout.Label("Audio", EditorStyles.boldLabel);
            EditorGUILayout.BeginVertical("box");
            EditorGUI.indentLevel++;

            _captureAudio = EditorGUILayout.Toggle("Capture Audio", _captureAudio);
            EditorGUI.BeginDisabledGroup(!_captureAudio);
            if (ShowConfigList("Source", _audioDeviceNames, null, ref _audioDeviceIndex, false))
            {
            }
            if (ShowConfigList("Codec", _audioCodecNames, _audioCodecConfigurable, ref _audioCodecIndex))
            {
                _queueConfigureAudioCodec = Mathf.Max(-1, _audioCodecIndex - 2);
            }
            EditorGUI.EndDisabledGroup();

            EditorGUI.indentLevel--;
            EditorGUILayout.EndVertical();
            EditorGUI.EndDisabledGroup();

            GUILayout.Space(16f);
            if (GUILayout.Button("Reset Settings"))
            {
                ResetSettings();
            }
            GUILayout.Space(4f);
        }
    }
    private void DrawConfigGUI()
    {
        EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(_lastCapturePath));
        if (GUILayout.Button("Open Last Video"))
        {
            if (!ShowInExplorer(_lastCapturePath))
            {
                _lastCapturePath = string.Empty;
            }
        }
        EditorGUI.EndDisabledGroup();
        _expandConfig = EditorGUILayout.Foldout(_expandConfig, "Configure");
        if (_expandConfig)
        {
            GUILayout.Label("General", EditorStyles.boldLabel);
            EditorGUILayout.BeginVertical("box");
            EditorGUI.indentLevel++;

            _captureModeIndex = EditorGUILayout.Popup("Mode", _captureModeIndex, _captureModes);

            // Source
            EditorGUILayout.LabelField("Source", EditorStyles.boldLabel);
            EditorGUI.indentLevel++;
            _sourceType = (SourceType)EditorGUILayout.EnumPopup("Type", _sourceType);
            if (_sourceType == SourceType.Camera)
            {
                if (_cameraNode == null && Camera.main != null)
                {
                    _cameraNode = Camera.main;
                }
                _cameraNode = (Camera)EditorGUILayout.ObjectField("Camera", _cameraNode, typeof(Camera), true);

                _cameraFastPixel = EditorGUILayout.Toggle("Fast Pixel Format", _cameraFastPixel);
            }
            EditorGUI.indentLevel--;


            // File name
            EditorGUILayout.LabelField("File Name", EditorStyles.boldLabel);
            EditorGUI.indentLevel++;
            _autoFilenamePrefixFromSceneName = EditorGUILayout.Toggle("From Scene Name", _autoFilenamePrefixFromSceneName);
            if (_autoFilenamePrefixFromSceneName)
            {
                _autoFilenamePrefix = System.IO.Path.GetFileNameWithoutExtension(EditorApplication.currentScene);
                if (string.IsNullOrEmpty(_autoFilenamePrefix))
                {
                    _autoFilenamePrefix = "capture";
                }
            }
            EditorGUI.BeginDisabledGroup(_autoFilenamePrefixFromSceneName);
            _autoFilenamePrefix = EditorGUILayout.TextField("Prefix", _autoFilenamePrefix);
            EditorGUI.EndDisabledGroup();
            _autoFilenameExtension = EditorGUILayout.TextField("Extension", _autoFilenameExtension);
            _appendTimestamp       = EditorGUILayout.Toggle("Append Timestamp", _appendTimestamp);
            EditorGUI.indentLevel--;

            // File path
            EditorGUILayout.LabelField("File Path", EditorStyles.boldLabel);
            EditorGUI.indentLevel++;
            _outputFolderIndex = EditorGUILayout.Popup("Relative to", _outputFolderIndex, _outputFolders);
            if (_outputFolderIndex == 0)
            {
                _outputFolderRelative = EditorGUILayout.TextField("SubFolder(s)", _outputFolderRelative);
            }
            else
            {
                EditorGUILayout.BeginHorizontal();
                _outputFolderAbsolute = EditorGUILayout.TextField("Path", _outputFolderAbsolute);
                if (GUILayout.Button(">", GUILayout.Width(22)))
                {
                    _outputFolderAbsolute = EditorUtility.SaveFolderPanel("Select Folder To Store Video Captures", System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, "../")), "");
                    EditorUtility.SetDirty(this);
                }
                EditorGUILayout.EndHorizontal();
            }
            EditorGUI.indentLevel--;


            EditorGUI.indentLevel--;
            EditorGUILayout.EndVertical();


            GUILayout.Label("Video", EditorStyles.boldLabel);
            EditorGUILayout.BeginVertical("box");
            EditorGUI.indentLevel++;

            if (_sourceType == 0)
            {
                // We can't just use Screen.width and Screen.height because Unity returns the size of this window
                // So instead we look for a camera with no texture target and a valid viewport
                int inWidth  = 1;
                int inHeight = 1;
                foreach (Camera cam in Camera.allCameras)
                {
                    if (cam.targetTexture == null)
                    {
                        float rectWidth  = Mathf.Clamp01(cam.rect.width + cam.rect.x) - Mathf.Clamp01(cam.rect.x);
                        float rectHeight = Mathf.Clamp01(cam.rect.height + cam.rect.y) - Mathf.Clamp01(cam.rect.y);
                        if (rectWidth > 0.0f && rectHeight > 0.0f)
                        {
                            inWidth  = Mathf.FloorToInt(cam.pixelWidth / rectWidth);
                            inHeight = Mathf.FloorToInt(cam.pixelHeight / rectHeight);
                            //Debug.Log (rectWidth + "    " + (cam.rect.height - cam.rect.y) + " " + cam.pixelHeight + " = " + inWidth + "x" + inHeight);
                        }
                        break;
                    }
                }
                Vector2 outSize = AVProMovieCaptureBase.GetRecordingResolution(inWidth, inHeight, GetDownScaleFromIndex(_downScaleIndex), new Vector2(_downscaleX, _downscaleY));
                EditorGUILayout.LabelField("Output", (int)outSize.x + " x " + (int)outSize.y + " @ " + (int)_frameRate, EditorStyles.boldLabel);
            }
            else
            {
                if (_cameraNode)
                {
                    int     inWidth  = Mathf.FloorToInt(_cameraNode.pixelRect.width);
                    int     inHeight = Mathf.FloorToInt(_cameraNode.pixelRect.height);
                    Vector2 outSize  = AVProMovieCaptureBase.GetRecordingResolution(inWidth, inHeight, GetDownScaleFromIndex(_downScaleIndex), new Vector2(_downscaleX, _downscaleY));
                    EditorGUILayout.LabelField("Output", (int)outSize.x + " x " + (int)outSize.y + " @ " + (int)_frameRate, EditorStyles.boldLabel);
                }
            }

            _downScaleIndex = EditorGUILayout.Popup("Down Scale", _downScaleIndex, _downScales);
            if (_downScaleIndex == 5)
            {
                Vector2 maxVideoSize = new Vector2(_downscaleX, _downscaleY);
                maxVideoSize = EditorGUILayout.Vector2Field("Size", maxVideoSize);
                _downscaleX  = Mathf.Clamp((int)maxVideoSize.x, 1, 4096);
                _downscaleY  = Mathf.Clamp((int)maxVideoSize.y, 1, 4096);
            }

            _frameRate = (AVProMovieCaptureBase.FrameRate)EditorGUILayout.EnumPopup("Frame Rate", _frameRate);

            if (ShowConfigList("Codec", _videoCodecNames, _videoCodecConfigurable, ref _videoCodecIndex))
            {
                _queueConfigureVideoCodec = Mathf.Max(-1, _videoCodecIndex - 2);
            }

            EditorGUI.indentLevel--;
            EditorGUILayout.EndVertical();


            EditorGUI.BeginDisabledGroup(_captureModeIndex != 0);
            GUILayout.Label("Audio", EditorStyles.boldLabel);
            EditorGUILayout.BeginVertical("box");
            EditorGUI.indentLevel++;

            _captureAudio = EditorGUILayout.Toggle("Capture Audio", _captureAudio);
            EditorGUI.BeginDisabledGroup(!_captureAudio);
            if (ShowConfigList("Source", _audioDeviceNames, null, ref _audioDeviceIndex, false))
            {
            }
            if (ShowConfigList("Codec", _audioCodecNames, _audioCodecConfigurable, ref _audioCodecIndex))
            {
                _queueConfigureAudioCodec = Mathf.Max(-1, _audioCodecIndex - 2);
            }
            EditorGUI.EndDisabledGroup();

            EditorGUI.indentLevel--;
            EditorGUILayout.EndVertical();
            EditorGUI.EndDisabledGroup();

            GUILayout.Space(16f);
            if (GUILayout.Button("Reset Settings"))
            {
                ResetSettings();
            }
            GUILayout.Space(4f);
        }
    }