private void CreateComponents()
    {
        switch (_sourceType)
        {
        case SourceType.Scene:
            _captureScene = (AVProMovieCaptureFromScene)GameObject.FindObjectOfType(typeof(AVProMovieCaptureFromScene));
            if (_captureScene == null)
            {
                GameObject go = new GameObject(TempGameObjectName);
                _captureScene = go.AddComponent <AVProMovieCaptureFromScene>();
            }
            _capture = _captureScene;
            break;

        case SourceType.Camera:
            _captureCamera = _cameraNode.gameObject.GetComponent <AVProMovieCaptureFromCamera>();
            if (_captureCamera == null)
            {
                _captureCamera = _cameraNode.gameObject.AddComponent <AVProMovieCaptureFromCamera>();
            }
            _captureCamera._useFastPixelFormat = _cameraFastPixel;
            _capture = _captureCamera;
            break;
        }

        _audioCapture = null;
        if (_captureAudio && _audioDeviceIndex == 0)
        {
            _audioCapture = (AVProUnityAudioCapture)GameObject.FindObjectOfType(typeof(AVProUnityAudioCapture));
            if (_audioCapture == null && Camera.main != null)
            {
                _audioCapture = Camera.main.gameObject.AddComponent <AVProUnityAudioCapture>();
            }
        }
    }
 private void StopCapture()
 {
     if (_capture != null)
     {
         if (_capture.IsCapturing())
         {
             _capture.StopCapture();
             _lastCapturePath = _capture.LastFilePath;
         }
         _capture       = null;
         _captureScene  = null;
         _captureCamera = null;
     }
     _audioCapture = null;
 }
    private void Configure(AVProMovieCaptureBase capture)
    {
        capture._videoCodecPriority = null;
        capture._audioCodecPriority = null;

        capture._captureOnStart         = false;
        capture._listVideoCodecsOnStart = false;
        capture._frameRate = _frameRate;
        capture._downScale = GetDownScaleFromIndex(_downScaleIndex);
        if (capture._downScale == AVProMovieCaptureBase.DownScale.Specific)
        {
            capture._maxVideoSize.x = _downscaleX;
            capture._maxVideoSize.y = _downscaleY;
        }

        capture._isRealTime            = (_captureModeIndex == 0);
        capture._autoGenerateFilename  = _appendTimestamp;
        capture._autoFilenamePrefix    = _autoFilenamePrefix;
        capture._autoFilenameExtension = _autoFilenameExtension;
        if (!capture._autoGenerateFilename)
        {
            capture._forceFilename = _autoFilenamePrefix + "." + _autoFilenameExtension;
        }

        capture._outputFolderType = AVProMovieCaptureBase.OutputPath.RelativeToProject;
        capture._outputFolderPath = _outputFolderRelative;
        if (_outputFolderIndex == 1)
        {
            capture._outputFolderType = AVProMovieCaptureBase.OutputPath.Absolute;
            capture._outputFolderPath = _outputFolderAbsolute;
        }

        capture._forceVideoCodecIndex = capture._codecIndex = Mathf.Max(-1, (_videoCodecIndex - 2));
        capture._noAudio = !_captureAudio;
        capture._forceAudioCodecIndex  = capture._audioCodecIndex = Mathf.Max(-1, (_audioCodecIndex - 2));
        capture._forceAudioDeviceIndex = capture._audioDeviceIndex = Mathf.Max(-1, (_audioDeviceIndex - 2));
    }
 private void StopCapture()
 {
     if (_capture != null)
     {
         if (_capture.IsCapturing())
         {
             _capture.StopCapture();
             _lastCapturePath = _capture.LastFilePath;
         }
         _capture = null;
         _captureScene = null;
         _captureCamera = null;
     }
     _audioCapture = null;
 }
    private void CreateComponents()
    {
        switch (_sourceType)
        {
        case SourceType.Scene:
            _captureScene = (AVProMovieCaptureFromScene)GameObject.FindObjectOfType(typeof(AVProMovieCaptureFromScene));
            if (_captureScene == null)
            {
                GameObject go = new GameObject(TempGameObjectName);
                _captureScene = go.AddComponent<AVProMovieCaptureFromScene>();
            }
            _capture = _captureScene;
            break;
        case SourceType.Camera:
            _captureCamera = _cameraNode.gameObject.GetComponent<AVProMovieCaptureFromCamera>();
            if (_captureCamera == null)
            {
                _captureCamera = _cameraNode.gameObject.AddComponent<AVProMovieCaptureFromCamera>();
            }
            _captureCamera._useFastPixelFormat = _cameraFastPixel;
            _capture = _captureCamera;
            break;
        }

        _audioCapture = null;
        if (_captureAudio && _audioDeviceIndex == 0)
        {
            _audioCapture = (AVProUnityAudioCapture)GameObject.FindObjectOfType(typeof(AVProUnityAudioCapture));
            if (_audioCapture == null && Camera.main != null)
            {
                _audioCapture = Camera.main.gameObject.AddComponent<AVProUnityAudioCapture>();
            }
        }
    }
    private void Configure(AVProMovieCaptureBase capture)
    {
        capture._videoCodecPriority = null;
        capture._audioCodecPriority = null;

        capture._captureOnStart = false;
        capture._listVideoCodecsOnStart = false;
        capture._frameRate = _frameRate;
        capture._downScale = GetDownScaleFromIndex(_downScaleIndex);
        if (capture._downScale == AVProMovieCaptureBase.DownScale.Specific)
        {
            capture._maxVideoSize.x = _downscaleX;
            capture._maxVideoSize.y = _downscaleY;
        }

        capture._isRealTime = (_captureModeIndex == 0);
        capture._autoGenerateFilename = _appendTimestamp;
        capture._autoFilenamePrefix = _autoFilenamePrefix;
        capture._autoFilenameExtension = _autoFilenameExtension;
        if (!capture._autoGenerateFilename)
        {
            capture._forceFilename = _autoFilenamePrefix + "." + _autoFilenameExtension;
        }

        capture._outputFolderType = AVProMovieCaptureBase.OutputPath.RelativeToProject;
        capture._outputFolderPath = _outputFolderRelative;
        if (_outputFolderIndex == 1)
        {
            capture._outputFolderType = AVProMovieCaptureBase.OutputPath.Absolute;
            capture._outputFolderPath = _outputFolderAbsolute;
        }

        capture._forceVideoCodecIndex = capture._codecIndex = Mathf.Max(-1, (_videoCodecIndex - 2));
        capture._noAudio = !_captureAudio;
        capture._forceAudioCodecIndex = capture._audioCodecIndex = Mathf.Max(-1, (_audioCodecIndex - 2));
        capture._forceAudioDeviceIndex = capture._audioDeviceIndex = Mathf.Max(-1, (_audioDeviceIndex - 2));
    }
    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);
        }
    }