protected virtual void AddSelectionButtons() { VRTK_SDKSetup loadedSetup = sdkManager.loadedSetup; if (loadedSetup != null) { GameObject chooseNoneButton = Instantiate(chooseButton.gameObject, chooseButton.transform.parent); chooseNoneButton.GetComponentInChildren <Text>().text = "None"; chooseNoneButton.name = "ChooseNoneButton"; chooseNoneButton.SetActive(true); chooseNoneButton.GetComponent <Button>().onClick.AddListener( () => sdkManager.UnloadSDKSetup(true) ); chooseButtonGameObjects.Add(chooseNoneButton); } VRTK_SDKSetup[] setups = sdkManager.setups; for (int index = 0; index < setups.Length; index++) { VRTK_SDKSetup setup = setups[index]; if (setup == null || setup == loadedSetup) { continue; } GameObject chooseButtonCopy = Instantiate(chooseButton.gameObject, chooseButton.transform.parent); chooseButtonCopy.GetComponentInChildren <Text>().text = setup.name; chooseButtonCopy.name = string.Format("Choose{0}Button", setup.name); chooseButtonCopy.SetActive(true); int indexCopy = index; Button button = chooseButtonCopy.GetComponent <Button>(); button.onClick.AddListener( () => sdkManager.TryLoadSDKSetup(indexCopy, true, setups) ); ColorBlock buttonColors = button.colors; buttonColors.colorMultiplier = setup.isValid ? 1.0f : 0.8f; button.colors = buttonColors; chooseButtonGameObjects.Add(chooseButtonCopy); } }
protected virtual void OnEnable() { VRTK_SDKManager sdkManager = (VRTK_SDKManager)target; setupsList = new ReorderableList(serializedObject, serializedObject.FindProperty("setups")) { headerHeight = 2, drawElementCallback = (rect, index, active, focused) => { SerializedProperty serializedProperty = setupsList.serializedProperty; if (serializedProperty.arraySize <= index) { return; } rect.y += 2; rect.height = EditorGUIUtility.singleLineHeight; Color previousColor = GUI.color; if (IsSDKSetupNeverUsed(index)) { GUI.color = new Color(previousColor.r, previousColor.g, previousColor.b, 0.5f); } GUIContent unloadButtonGUIContent = new GUIContent("Unload", "Unload this SDK Setup."); GUIContent loadButtonGUIContent = new GUIContent("Load", "Try to load this SDK Setup."); float buttonGUIContentWidth = Mathf.Max( GUI.skin.button.CalcSize(unloadButtonGUIContent).x, GUI.skin.button.CalcSize(loadButtonGUIContent).x ); VRTK_SDKSetup setup = (VRTK_SDKSetup)serializedProperty.GetArrayElementAtIndex(index).objectReferenceValue; if (EditorApplication.isPlaying && setup != null) { rect.width -= buttonGUIContentWidth + ReorderableList.Defaults.padding; } EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(rect, serializedProperty.GetArrayElementAtIndex(index), GUIContent.none); if (EditorGUI.EndChangeCheck()) { setup = (VRTK_SDKSetup)serializedProperty.GetArrayElementAtIndex(index).objectReferenceValue; if (setup != null) { int indexOfExistingDuplicateSetup = Enumerable .Range(0, serializedProperty.arraySize) .Except(new[] { index }) .Where(i => (VRTK_SDKSetup)serializedProperty.GetArrayElementAtIndex(i).objectReferenceValue == setup) .DefaultIfEmpty(-1) .First(); if (indexOfExistingDuplicateSetup != -1) { serializedProperty.GetArrayElementAtIndex(indexOfExistingDuplicateSetup).objectReferenceValue = null; setupsList.index = indexOfExistingDuplicateSetup; ReorderableList.defaultBehaviours.DoRemoveButton(setupsList); setupsList.index = index; } } sdkManager.ManageVRSettings(false); } GUI.color = previousColor; if (EditorApplication.isPlaying && setup != null) { rect.x += rect.width + ReorderableList.Defaults.padding; rect.width = buttonGUIContentWidth; if (sdkManager.loadedSetup == setup) { if (GUI.Button(rect, unloadButtonGUIContent)) { sdkManager.UnloadSDKSetup(); } } else { if (GUI.Button(rect, loadButtonGUIContent)) { sdkManager.TryLoadSDKSetup(index, true, sdkManager.setups); } } } }, onAddCallback = list => { SerializedProperty serializedProperty = list.serializedProperty; int index = serializedProperty.arraySize; serializedProperty.arraySize++; list.index = index; SerializedProperty element = serializedProperty.GetArrayElementAtIndex(index); element.objectReferenceValue = null; sdkManager.ManageVRSettings(false); }, onRemoveCallback = list => { int index = list.index; VRTK_SDKSetup sdkSetup = sdkManager.setups[index]; bool isLoaded = sdkManager.loadedSetup == sdkSetup; if (isLoaded) { sdkManager.UnloadSDKSetup(); } if (sdkSetup != null) { list.serializedProperty.GetArrayElementAtIndex(index).objectReferenceValue = null; } ReorderableList.defaultBehaviours.DoRemoveButton(list); sdkManager.ManageVRSettings(false); }, onReorderCallback = list => sdkManager.ManageVRSettings(false) }; Undo.undoRedoPerformed += UndoRedoPerformed; }