-->

分類

2018年1月12日 星期五

[Unity] use script to modify the value of SteamVR_AutoEnableVR and Virtual Reality Supported in PlayerSettings


reference: modify project settings via script
reference: YAML syntax

If somehow you need to build a non-VR version executable (e.g. for testing purpose) for your VR game, you need to turn off the VR support. Normally you can turn off VR support via the settings windows as the following:

normal way to set SteamVR Automatically Enable VR: Edit->Preferences->SteamVR



normal way to set Virtual Reality Supported: File->Build Settings->Player Settings->Other Settings



SteamVR Automatically Enable VR is a bool value of EditorPref, you can find its key in the Windows registry(if you are using Windows). The registry path is
[HKEY_CURRENT_USER\Software\Unity Technologies\Unity Editor 5.x]
if you are using Unity 5.x


Virtual Reality Supported is a bool value which is serialized by YAML format in YourProject/ProjectSettings/ProjectSettings.asset



If you need to change these settings values frequently, you can try to use script to do the switch for convenience.

To use script to modify the settings, first make sure Asset Serialization(Edit->Project Settings->Editor->Asset Serialization) is set as Force Text

Then use the following script to create an EditorWindow that can modify the value of SteamVR Automatically Enable VR(if you installed SteamVR plugin) and the value of Virtual Reality Supported in PlayerSettings

Note: different Unity versions may have different data structures for storing the ProjectSettings, so make sure you checked the content of ProjectSettings.asset and modify the script in order to access the values correctly 

public class EditorSettingsTestEditorWindow : EditorWindow
{

    [MenuItem("Window/EditorSettingsTestEditorWindow")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        EditorSettingsTestEditorWindow window = (EditorSettingsTestEditorWindow)EditorWindow.GetWindow(typeof(EditorSettingsTestEditorWindow));
        window.Show();
    }
    const string ProjectSettingsAssetPath = "ProjectSettings/ProjectSettings.asset";

    private bool bSteamVRAutoEnable = false;
    private bool bVRSupported = false;
    private SerializedProperty BuildTargetVRSettings;
    private SerializedObject projectSettingsManager;
    private void OnEnable()
    {
        bSteamVRAutoEnable = EditorPrefs.GetBool("SteamVR_AutoEnableVR");
        Debug.Log("bSteamVRAutoEnable: " + bSteamVRAutoEnable);

        projectSettingsManager = new SerializedObject(UnityEditor.AssetDatabase.LoadAllAssetsAtPath(ProjectSettingsAssetPath)[0]);
        BuildTargetVRSettings = projectSettingsManager.FindProperty("m_BuildTargetVRSettings");
        int arrSize = BuildTargetVRSettings.arraySize;
        for(int i = 0; i < arrSize; i++)
        {
            SerializedProperty element = BuildTargetVRSettings.GetArrayElementAtIndex(i);

            SerializedProperty buildTarget = element.FindPropertyRelative("m_BuildTarget");
            string buildTargetValue = buildTarget.stringValue;
            if(buildTargetValue == "Standalone")
            {
                SerializedProperty enabled = element.FindPropertyRelative("m_Enabled");
                bVRSupported = enabled.boolValue;
                Debug.Log("got vr supported: " + bVRSupported);
            }
        }
    }

    void OnGUI()
    {
        bool tempSteamVRAutoEnable = bSteamVRAutoEnable;
        tempSteamVRAutoEnable = EditorGUILayout.ToggleLeft("SteamVR_AutoEnableVR", bSteamVRAutoEnable);
        if(tempSteamVRAutoEnable != bSteamVRAutoEnable)
        {
            bSteamVRAutoEnable = tempSteamVRAutoEnable;
            EditorPrefs.SetBool("SteamVR_AutoEnableVR", bSteamVRAutoEnable);
        }

        bool tempVRSupported = bVRSupported;

        tempVRSupported = EditorGUILayout.ToggleLeft("vr supported", bVRSupported);
        if(tempVRSupported != bVRSupported)
        {
            bVRSupported = tempVRSupported;
            int arrSize = BuildTargetVRSettings.arraySize;
            for (int i = 0; i < arrSize; i++)
            {
                SerializedProperty element = BuildTargetVRSettings.GetArrayElementAtIndex(i);

                SerializedProperty buildTarget = element.FindPropertyRelative("m_BuildTarget");
                string buildTargetValue = buildTarget.stringValue;
                if (buildTargetValue == "Standalone")
                {
                    SerializedProperty enabled = element.FindPropertyRelative("m_Enabled");
                    enabled.boolValue = bVRSupported;
                    projectSettingsManager.ApplyModifiedProperties();
                }
            }
        }
    }
}