-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSerializedPropertyViewer.cs
185 lines (156 loc) · 3.93 KB
/
SerializedPropertyViewer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace cmdwtf.UnityTools.Editor
{
public class SerializedPropertyViewer : EditorWindow
{
public class SPData
{
public int depth;
public string info;
public string val;
public int oid;
public SPData(int d, string i, string v, int o)
{
if (d < 0)
{
d = 0;
}
depth = d;
info = i;
val = v;
oid = o;
}
}
[MenuItem("Window/SP Viewer")]
static void Init()
{
// Get existing open window or if none, make a new one:
SerializedPropertyViewer window =
(SerializedPropertyViewer)EditorWindow.GetWindow(typeof(SerializedPropertyViewer));
window.titleContent = new GUIContent("SP Viewer");
window.Show();
}
UnityEngine.Object obj;
Vector2 scrollPos;
List<SPData> data;
bool dirty = true;
string searchStr;
string searchStrRep;
public static GUIStyle richTextStyle;
void OnGUI()
{
if (richTextStyle == null)
{
//EditorStyles does not exist in Constructor??
richTextStyle = new GUIStyle(EditorStyles.label);
richTextStyle.richText = true;
}
UnityEngine.Object newObj = EditorGUILayout.ObjectField("Object:", obj, typeof(UnityEngine.Object), false);
string newSearchStr = EditorGUILayout.TextField("Search:", searchStr);
if (newSearchStr != searchStr)
{
searchStr = newSearchStr;
searchStrRep = "<color=green>" + searchStr + "</color>";
dirty = true;
}
if (obj != newObj)
{
obj = newObj;
dirty = true;
}
if (data == null)
{
dirty = true;
}
if (dirty == true)
{
dirty = false;
searchObject(obj);
}
scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
foreach (SPData line in data)
{
EditorGUI.indentLevel = line.depth;
if (line.oid > 0)
{
GUILayout.BeginHorizontal();
}
EditorGUILayout.LabelField(line.info, richTextStyle);
if (line.oid > 0)
{
if (GUILayout.Button(">>", GUILayout.Width(50)))
{
Selection.activeInstanceID = line.oid;
}
GUILayout.EndHorizontal();
}
}
EditorGUILayout.EndScrollView();
}
void searchObject(UnityEngine.Object obj)
{
data = new List<SPData>();
if (obj == null)
{
return;
}
SerializedObject so = new SerializedObject(obj);
SerializedProperty iterator = so.GetIterator();
search(iterator, 0);
}
void search(SerializedProperty prop, int depth)
{
logProperty(prop);
while (prop.Next(true))
{
logProperty(prop);
}
}
void logProperty(SerializedProperty prop)
{
string strVal = getStringValue(prop);
string propDesc = prop.propertyPath + " type:" + prop.type + " name:" + prop.name + " val:" + strVal;
if (searchStr.Length > 0)
{
propDesc = propDesc.Replace(searchStr, searchStrRep);
}
data.Add(new SPData(prop.depth, propDesc, strVal, getObjectID(prop)));
}
int getObjectID(SerializedProperty prop)
{
if (prop.propertyType == SerializedPropertyType.ObjectReference && prop.objectReferenceValue != null)
{
return prop.objectReferenceValue.GetInstanceID();
}
return 0;
}
string getStringValue(SerializedProperty prop)
{
switch (prop.propertyType)
{
case SerializedPropertyType.String:
return prop.stringValue;
case SerializedPropertyType.Character: //this isn't really a thing, chars are ints!
case SerializedPropertyType.Integer:
if (prop.type == "char")
{
return System.Convert.ToChar(prop.intValue).ToString();
}
return prop.intValue.ToString();
case SerializedPropertyType.ObjectReference:
if (prop.objectReferenceValue != null)
{
return prop.objectReferenceValue.ToString();
}
else
{
return "(null)";
}
default:
return "";
}
}
}
}