// Call this from Editor's OnInspectorGUI public void OnInspectorGUI() { m_ScratchComponentList.Clear(); int numNullComponents = GetComponent(m_Stage, m_ScratchComponentList); // Have the edited components changed? int numComponents = m_ScratchComponentList.Count; bool dirty = numComponents != m_EditedComponents.Count; for (int i = 0; !dirty && i < numComponents; ++i) { dirty = m_ScratchComponentList[i] != m_EditedComponents[i]; } if (dirty) { if (m_ComponentEditor != null) { ActiveEditorRegistry.SetActiveEditor(m_ComponentEditor, false); m_ComponentEditor.ResetTarget(); UnityEngine.Object.DestroyImmediate(m_ComponentEditor); } m_ComponentEditor = null; m_ComponentEditorOnSceneGUI = null; m_EditedComponents.Clear(); m_EditedComponents.AddRange(m_ScratchComponentList); m_IsMixedType = false; for (int i = 1; !m_IsMixedType && i < numComponents; ++i) { m_IsMixedType = m_EditedComponents[i].GetType() != m_EditedComponents[i - 1].GetType(); } } if (numNullComponents > 0 && numComponents > 0) { m_IsMixedType = true; } if (numComponents > 0 && m_ComponentEditor == null && !m_IsMixedType) { UnityEditor.Editor.CreateCachedEditor(m_EditedComponents.ToArray(), null, ref m_ComponentEditor); if (m_ComponentEditor != null) { m_ComponentEditorOnSceneGUI = m_ComponentEditor.GetType().GetMethod("OnSceneGUI", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); } } m_StageSelection = GetPopupIndexForComponent(numComponents == 0 ? null : m_EditedComponents[0]); m_StageError = false; for (int i = 0; !m_StageError && i < numComponents; ++i) { m_StageError = !m_EditedComponents[i].IsValid; } DrawComponentInspector(); }
// Call this from OnDisable() public void Shutdown() { ActiveEditorRegistry.SetActiveEditor(m_ComponentEditor, false); if (m_ComponentEditor != null) { UnityEngine.Object.DestroyImmediate(m_ComponentEditor); } m_ComponentEditor = null; m_EditedComponents.Clear(); m_ScratchComponentList.Clear(); }
private void DrawComponentInspector() { const float indentSize = 15; // GML wtf get rid of this int index = (int)m_Stage; Rect rect = EditorGUILayout.GetControlRect(true); // Don't use PrefixLabel() because it will link the enabled status of field and label GUIContent label = new GUIContent(InspectorUtility.NicifyClassName(m_Stage.ToString())); if (m_StageError) { label.image = EditorGUIUtility.IconContent("console.warnicon.sml").image; } float labelWidth = EditorGUIUtility.labelWidth - EditorGUI.indentLevel * indentSize; Rect r = rect; r.width = labelWidth; EditorGUI.LabelField(r, label); r = rect; r.width -= labelWidth; r.x += labelWidth; EditorGUI.BeginChangeCheck(); bool wasEnabled = GUI.enabled; if (TypeIsLocked) { GUI.enabled = false; } EditorGUI.showMixedValue = m_IsMixedType; m_StageSelection = EditorGUI.Popup(r, m_StageSelection, sStageData[index].PopupOptions); EditorGUI.showMixedValue = false; GUI.enabled = wasEnabled; Type type = sStageData[index].types[m_StageSelection]; if (EditorGUI.EndChangeCheck()) { SetComponent(m_Stage, type); if (m_StageSelection != 0) { sStageData[index].IsExpanded = true; } Shutdown(); GUIUtility.ExitGUI(); return; // let the component editor be recreated } // Draw the embedded editor if (type != null) { r = new Rect(rect.x, rect.y, labelWidth, rect.height); var isExpanded = m_IsMixedType ? false : EditorGUI.Foldout( r, sStageData[index].IsExpanded, GUIContent.none, true); if (isExpanded || isExpanded != sStageData[index].IsExpanded) { // Make the editor for that stage ActiveEditorRegistry.SetActiveEditor(m_ComponentEditor, isExpanded); if (isExpanded && m_ComponentEditor != null) { ++EditorGUI.indentLevel; m_ComponentEditor.OnInspectorGUI(); --EditorGUI.indentLevel; } } sStageData[index].IsExpanded = isExpanded; } }