Tanoda
pb_Inspector.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3using System.Reflection;
4using UnityEngine.UI;
6using System.Linq;
7using System.Collections.Generic;
9
10namespace GILES.Interface
11{
15 public class pb_Inspector : pb_MonoBehaviourSingleton<pb_Inspector>
16 {
19 public GameObject inspectorScrollPanel;
20
22 private GameObject currentSelection;
23
25 public bool showUnityComponents = false;
26
28 private List<pb_ComponentEditor> componentEditors = new List<pb_ComponentEditor>();
29
31 private static HashSet<System.Type> userIgnoredTypes = new HashSet<System.Type>();
32
36 public static void AddIgnoredType(System.Type type)
37 {
38 userIgnoredTypes.Add(type);
39 }
40
44 public static void RemoveIgnoredType(System.Type type)
45 {
46 userIgnoredTypes.Remove(type);
47 }
48
49 void Start()
50 {
52 Undo.AddUndoPerformedListener( UndoRedoPerformed );
53 Undo.AddRedoPerformedListener( UndoRedoPerformed );
55 }
56
57 void UndoRedoPerformed()
58 {
59 foreach(pb_ComponentEditor editor in componentEditors)
60 {
61 editor.UpdateGUI();
62 }
63 }
64
65 void OnSelectionChange(IEnumerable<GameObject> selection)
66 {
67 // build inspector queue
68 if(currentSelection != pb_Selection.activeGameObject)
69 {
70 RebuildInspector( pb_Selection.activeGameObject );
71 currentSelection = pb_Selection.activeGameObject;
72 }
73 }
74
75 public void RebuildInspector(GameObject go)
76 {
77 ClearInspector();
78
79 if(go == null)
80 return;
81
82 var rename = go.GetComponent<RenameObject>();
83 var goName = go.name;
84 if (rename && !string.IsNullOrEmpty(rename.NewName))
85 {
86 goName = rename.NewName;
87 }
88 GameObject panel1 = pb_GUIUtility.CreateLabeledVerticalPanel(goName);
89 panel1.transform.SetParent(inspectorScrollPanel.transform);
90
91 foreach (Component component in go.GetComponents<Component>())
92 {
93 if( component == null ||
94 userIgnoredTypes.Contains(component.GetType()) ||
95 pb_Reflection.HasIgnoredAttribute(component.GetType()) ||
96 System.Attribute.GetCustomAttribute(component.GetType(), typeof(pb_InspectorIgnoreAttribute)) != null ||
97 (!showUnityComponents && pb_Config.IgnoredComponentsInInspector.Contains(component.GetType())))
98 continue;
99
100 string panelLabelTitleString = component.GetType().ToString();
101
102 //check if the component has the custom inspector name attribute
103 if (component.GetType().GetCustomAttributes(typeof(pb_InspectorNameAttribute), true).Length > 0)
104 {
105 //get first instance of the attribute.
106 pb_InspectorNameAttribute attr = (pb_InspectorNameAttribute)component.GetType().GetCustomAttributes(typeof(pb_InspectorNameAttribute), true)[0];
107
108 if (attr.name != null && attr.name.Length > 0)
109 {
110 panelLabelTitleString = attr.name; //set the title to the name set on the attribute
111 }
112
113 }
114
115 GameObject panel = pb_GUIUtility.CreateLabeledVerticalPanel(panelLabelTitleString);
116 panel.transform.SetParent(inspectorScrollPanel.transform);
117
118 pb_ComponentEditor inspector = null;
119
120 if( typeof(pb_ICustomEditor).IsAssignableFrom(component.GetType()) )
121 inspector = ((pb_ICustomEditor)component).InstantiateInspector(component);
122 else
123 inspector = pb_ComponentEditorResolver.GetEditor(component);
124
125 inspector.transform.SetParent(panel.transform);
126
127 componentEditors.Add(inspector);
128 }
129 }
130
131 void ClearInspector()
132 {
133 foreach(Transform go in inspectorScrollPanel.transform)
134 pb_ObjectUtility.Destroy(go.gameObject);
135
136 componentEditors.Clear();
137 }
138
142 public void ToggleInspector(bool show)
143 {
144 // GetComponent<RectTransform>().bottom = show ? 0f : 200f;
145 }
146 }
147}
UnityEngine.Component Component
static void AddIgnoredType(System.Type type)
Definition: pb_Inspector.cs:36
void ToggleInspector(bool show)
void RebuildInspector(GameObject go)
Definition: pb_Inspector.cs:75
static void RemoveIgnoredType(System.Type type)
Definition: pb_Inspector.cs:44
bool showUnityComponents
By default don't show the Unity components.
Definition: pb_Inspector.cs:25
static void AddRedoPerformedListener(Callback callback)
Definition: Undo.cs:77
static void AddUndoPerformedListener(Callback callback)
Definition: Undo.cs:66
static void AddOnSelectionChangeListener(Callback< IEnumerable< GameObject > > del)
Definition: pb_Selection.cs:34
string NewName
Definition: RenameObject.cs:7