public static void Update()
        {
            if (!SteamVR_Settings.instance.autoEnableVR || Application.isPlaying)
            {
                End();
            }

            if (UnityEditor.PlayerSettings.virtualRealitySupported == false)
            {
                if (forceInstall == false)
                {
                    int shouldInstall = UnityEditor.EditorUtility.DisplayDialogComplex("SteamVR", "Would you like to enable Virtual Reality mode?\n\nThis will install the OpenVR for Desktop package and enable it in Player Settings.", "Yes", "No, and don't ask again", "No");

                    switch (shouldInstall)
                    {
                    case 0:     //yes
                        UnityEditor.PlayerSettings.virtualRealitySupported = true;
                        break;

                    case 1:     //no
                        End();
                        return;

                    case 2:     //no, don't ask
                        SteamVR_Settings.instance.autoEnableVR = false;
                        SteamVR_Settings.Save();
                        End();
                        return;
                    }
                }

                Debug.Log("<b>[SteamVR Setup]</b> Enabled virtual reality support in Player Settings. (you can disable this by unchecking Assets/SteamVR/SteamVR_Settings.autoEnableVR)");
            }

            switch (updateState)
            {
            case PackageStates.None:
                //see if we have the package
                listRequest = UnityEditor.PackageManager.Client.List(true);
                updateState = PackageStates.WaitingForList;
                break;

            case PackageStates.WaitingForList:
                if (listRequest == null)
                {
                    listRequest = UnityEditor.PackageManager.Client.List(true);
                    updateState = PackageStates.WaitingForList;
                }
                else if (listRequest.IsCompleted)
                {
                    if (listRequest.Error != null || listRequest.Status == UnityEditor.PackageManager.StatusCode.Failure)
                    {
                        updateState = PackageStates.Failed;
                        break;
                    }

                    string packageName = unityOpenVRPackageString;

                    bool hasPackage = listRequest.Result.Any(package => package.name == packageName);

                    if (hasPackage == false)
                    {
                        //if we don't have the package - then install it
                        addRequest  = UnityEditor.PackageManager.Client.Add(packageName);
                        updateState = PackageStates.WaitingForAdd;
                        addTryCount++;

                        Debug.Log("<b>[SteamVR Setup]</b> Installing OpenVR package...");
                        addingPackageTime.Start();
                        addingPackageTimeTotal.Start();
                    }
                    else
                    {
                        //if we do have the package, make sure it's enabled.
                        updateState = PackageStates.Installed;     //already installed
                    }
                }
                break;

            case PackageStates.WaitingForAdd:
                if (addRequest.IsCompleted)
                {
                    if (addRequest.Error != null || addRequest.Status == UnityEditor.PackageManager.StatusCode.Failure)
                    {
                        updateState = PackageStates.Failed;
                        break;
                    }
                    else
                    {
                        //if the package manager says we added it then confirm that with the list
                        listRequest = UnityEditor.PackageManager.Client.List(true);
                        updateState = PackageStates.WaitingForAddConfirm;
                    }
                }
                else
                {
                    if (addingPackageTimeTotal.Elapsed.TotalSeconds > estimatedTimeToInstall)
                    {
                        if (addTryCount == 1)
                        {
                            estimatedTimeToInstall *= 2;     //give us more time to retry
                        }
                        else
                        {
                            updateState = PackageStates.Failed;
                        }
                    }

                    string dialogText;
                    if (addTryCount == 1)
                    {
                        dialogText = "Installing OpenVR from Unity Package Manager...";
                    }
                    else
                    {
                        dialogText = "Retrying OpenVR install from Unity Package Manager...";
                    }

                    bool cancel = UnityEditor.EditorUtility.DisplayCancelableProgressBar("SteamVR", dialogText, (float)addingPackageTimeTotal.Elapsed.TotalSeconds / estimatedTimeToInstall);
                    if (cancel)
                    {
                        updateState = PackageStates.Failed;
                    }

                    if (addingPackageTime.Elapsed.TotalSeconds > 10)
                    {
                        Debug.Log("<b>[SteamVR Setup]</b> Waiting for package manager to install OpenVR package...");
                        addingPackageTime.Stop();
                        addingPackageTime.Reset();
                        addingPackageTime.Start();
                    }
                }
                break;

            case PackageStates.WaitingForAddConfirm:
                if (listRequest.IsCompleted)
                {
                    if (listRequest.Error != null)
                    {
                        updateState = PackageStates.Failed;
                        break;
                    }
                    string packageName = unityOpenVRPackageString;

                    bool hasPackage = listRequest.Result.Any(package => package.name == packageName);

                    if (hasPackage == false)
                    {
                        if (addTryCount == 1)
                        {
                            addRequest  = UnityEditor.PackageManager.Client.Add(packageName);
                            updateState = PackageStates.WaitingForAdd;
                            addTryCount++;

                            Debug.Log("<b>[SteamVR Setup]</b> Retrying OpenVR package install...");
                        }
                        else
                        {
                            updateState = PackageStates.Failed;
                        }
                    }
                    else
                    {
                        updateState = PackageStates.Installed;     //installed successfully

                        Debug.Log("<b>[SteamVR Setup]</b> Successfully installed OpenVR Desktop package.");
                    }
                }
                break;

            case PackageStates.Installed:
                UnityEditor.BuildTargetGroup currentTarget = UnityEditor.EditorUserBuildSettings.selectedBuildTargetGroup;

                string[] devices = UnityEditorInternal.VR.VREditor.GetVREnabledDevicesOnTargetGroup(currentTarget);

                bool hasOpenVR = false;
                bool isFirst   = false;

                if (devices.Length != 0)
                {
                    int index = Array.FindIndex(devices, device => string.Equals(device, openVRString, System.StringComparison.CurrentCultureIgnoreCase));
                    hasOpenVR = index != -1;
                    isFirst   = index == 0;
                }

                //list openvr as the first option if it was in the list already
                List <string> devicesList = new List <string>(devices);
                if (isFirst == false)
                {
                    if (hasOpenVR == true)
                    {
                        devicesList.Remove(openVRString);
                    }

                    devicesList.Insert(0, openVRString);
                    string[] newDevices = devicesList.ToArray();

                    UnityEditorInternal.VR.VREditor.SetVREnabledDevicesOnTargetGroup(currentTarget, newDevices);
                    Debug.Log("<b>[SteamVR Setup]</b> Added OpenVR to supported VR SDKs list.");
                }

                End();
                break;

            case PackageStates.Failed:
                End();

                string failtext = "The Unity Package Manager failed to automatically install the OpenVR Desktop package. Please open the Package Manager Window and try to install it manually.";
                UnityEditor.EditorUtility.DisplayDialog("SteamVR", failtext, "Ok");
                Debug.Log("<b>[SteamVR Setup]</b> " + failtext);
                break;
            }
        }
コード例 #2
0
        public static void Update()
        {
            if (!SteamVR_Settings.instance.autoEnableVR || Application.isPlaying)
            {
                End();
            }

            bool enabledVR = false;

            int shouldInstall = -1;

            if (UnityEditor.PlayerSettings.virtualRealitySupported == false)
            {
                shouldInstall = UnityEditor.EditorUtility.DisplayDialogComplex("SteamVR", "Would you like to enable Virtual Reality mode?\n\nThis will enable Virtual Reality in Player Settings and add OpenVR as a target.", "Yes", "No, and don't ask again", "No");

                switch (shouldInstall)
                {
                case 0:     //yes
                    UnityEditor.PlayerSettings.virtualRealitySupported = true;
                    break;

                case 1:     //no:
                    UnityEditor.EditorApplication.update -= Update;
                    return;

                case 2:     //no, don't ask
                    SteamVR_Settings.instance.autoEnableVR = false;
                    SteamVR_Settings.Save();
                    UnityEditor.EditorApplication.update -= Update;
                    return;
                }
            }

            UnityEditor.BuildTargetGroup currentTarget = UnityEditor.EditorUserBuildSettings.selectedBuildTargetGroup;

#if UNITY_5_6_OR_NEWER
            string[] devices = UnityEditorInternal.VR.VREditor.GetVREnabledDevicesOnTargetGroup(currentTarget);
#else
            string[] devices = UnityEditorInternal.VR.VREditor.GetVREnabledDevices(currentTarget);
#endif

            bool hasOpenVR = devices.Any(device => string.Equals(device, openVRString, System.StringComparison.CurrentCultureIgnoreCase));

            if (hasOpenVR == false || enabledVR)
            {
                string[] newDevices;
                if (enabledVR && hasOpenVR == false)
                {
                    newDevices = new string[] { openVRString }; //only list openvr if we enabled it
                }
                else
                {
                    List <string> devicesList = new List <string>(devices); //list openvr as the first option if it wasn't in the list.
                    if (hasOpenVR)
                    {
                        devicesList.Remove(openVRString);
                    }

                    devicesList.Insert(0, openVRString);
                    newDevices = devicesList.ToArray();
                }

                int shouldEnable = -1;
                if (shouldInstall == 0)
                {
                    shouldEnable = 0;
                }
                else
                {
                    shouldEnable = UnityEditor.EditorUtility.DisplayDialogComplex("SteamVR", "Would you like to enable OpenVR as a VR target?", "Yes", "No, and don't ask again", "No");
                }

                switch (shouldEnable)
                {
                case 0:     //yes
#if UNITY_5_6_OR_NEWER
                    UnityEditorInternal.VR.VREditor.SetVREnabledDevicesOnTargetGroup(currentTarget, newDevices);
#else
                    UnityEditorInternal.VR.VREditor.SetVREnabledDevices(currentTarget, newDevices);
#endif
                    Debug.Log("<b>[SteamVR Setup]</b> Added OpenVR to supported VR SDKs list.");
                    break;

                case 1:     //no:
                    break;

                case 2:     //no, don't ask
                    SteamVR_Settings.instance.autoEnableVR = false;
                    SteamVR_Settings.Save();
                    break;
                }
            }

            UnityEditor.EditorApplication.update -= Update;
        }