/// <summary> /// Registers a set of rows for all components in a <see cref="SceneObject"/>, as well as its transform and /// child objects. /// </summary> /// <param name="parent">Row element under which to create the new rows.</param> private void AddSceneObjectRows(Element parent) { string soName = "!" + parent.so.Name; Component[] components = parent.so.GetComponents(); parent.children = new Element[components.Length + 2]; SpriteTexture soIcon = EditorBuiltin.GetEditorIcon(EditorIcon.SceneObject); SpriteTexture compIcon = EditorBuiltin.GetEditorIcon(EditorIcon.Component); // Add transform parent.children[0] = AddFoldoutRow(parent.childLayout, soIcon, "Transform", parent.so, null, parent.path + "/" + soName, ToggleTransformFoldout); // Add components for (int i = 0; i < components.Length; i++) { Component childComponent = components[i]; Action <Element, bool> toggleCallback = (toggleParent, expand) => { SerializableObject componentObject = new SerializableObject(childComponent.GetType(), childComponent); ToggleObjectFoldout(toggleParent, componentObject, expand); }; string name = childComponent.GetType().Name; string path = parent.path + "/" + soName + "/:" + name; parent.children[i + 1] = AddFoldoutRow(parent.childLayout, compIcon, name, parent.so, childComponent, path, toggleCallback); } // Add children if (parent.so.GetNumChildren() > 0) { parent.children[parent.children.Length - 1] = AddFoldoutRow(parent.childLayout, soIcon, "Children", parent.so, null, parent.path + "/" + soName, ToggleChildFoldout); } }
/// <summary> /// Expands or collapses the set of rows displaying all <see cref="SceneObject"/> children of a /// <see cref="SceneObject"/>. /// </summary> /// <param name="parent">Parent row element whose children to expand/collapse.</param> /// <param name="expand">True to expand, false to collapse.</param> private void ToggleChildFoldout(Element parent, bool expand) { parent.childLayout.Clear(); parent.children = null; parent.indentLayout.Active = expand; if (expand) { int numChildren = parent.so.GetNumChildren(); parent.children = new Element[numChildren]; for (int i = 0; i < numChildren; i++) { SceneObject child = parent.so.GetChild(i); SpriteTexture soIcon = EditorBuiltin.GetEditorIcon(EditorIcon.SceneObject); parent.children[i] = AddFoldoutRow(parent.childLayout, soIcon, child.Name, child, null, parent.path, ToggleSceneObjectFoldout); } } }
/// <summary> /// Sets a scene object whose GUI is to be displayed in the inspector. Clears any previous contents of the window. /// </summary> /// <param name="so">Scene object to inspect.</param> private void SetObjectToInspect(SceneObject so) { if (so == null) { return; } currentType = InspectorType.SceneObject; activeSO = so; inspectorScrollArea = new GUIScrollArea(ScrollBarType.ShowIfDoesntFit, ScrollBarType.NeverShow); scrollAreaHighlight = new GUITexture(Builtin.WhiteTexture); scrollAreaHighlight.SetTint(HIGHLIGHT_COLOR); scrollAreaHighlight.Active = false; GUI.AddElement(inspectorScrollArea); GUIPanel inspectorPanel = inspectorScrollArea.Layout.AddPanel(); inspectorLayout = inspectorPanel.AddLayoutY(); highlightPanel = inspectorPanel.AddPanel(-1); highlightPanel.AddElement(scrollAreaHighlight); // SceneObject fields CreateSceneObjectFields(); RefreshSceneObjectFields(true); // Components Component[] allComponents = so.GetComponents(); for (int i = 0; i < allComponents.Length; i++) { inspectorLayout.AddSpace(COMPONENT_SPACING); InspectorComponent data = new InspectorComponent(); data.instanceId = allComponents[i].InstanceId; data.folded = false; data.foldout = new GUIToggle(allComponents[i].GetType().Name, EditorStyles.Foldout); data.foldout.AcceptsKeyFocus = false; SpriteTexture xBtnIcon = EditorBuiltin.GetEditorIcon(EditorIcon.X); data.removeBtn = new GUIButton(new GUIContent(xBtnIcon), GUIOption.FixedWidth(30)); data.title = inspectorLayout.AddLayoutX(); data.title.AddElement(data.foldout); data.title.AddElement(data.removeBtn); data.panel = inspectorLayout.AddPanel(); var persistentProperties = persistentData.GetProperties(allComponents[i].InstanceId); data.inspector = InspectorUtility.GetInspector(allComponents[i].GetType()); data.inspector.Initialize(data.panel, allComponents[i], persistentProperties); bool isExpanded = data.inspector.Persistent.GetBool(data.instanceId + "_Expanded", true); data.foldout.Value = isExpanded; if (!isExpanded) { data.inspector.SetVisible(false); } Type curComponentType = allComponents[i].GetType(); data.foldout.OnToggled += (bool expanded) => OnComponentFoldoutToggled(data, expanded); data.removeBtn.OnClick += () => OnComponentRemoveClicked(curComponentType); inspectorComponents.Add(data); } inspectorLayout.AddFlexibleSpace(); UpdateDropAreas(); }