-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHierarchyAnalys.cs
222 lines (160 loc) · 6.87 KB
/
HierarchyAnalys.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class HierarchyAnalys : EditorWindow
{
const string stringCountText = "GameObjects Count : ";
const string stringVertCountText = "Vertex Count : ";
StringBuilder countText = new StringBuilder(stringCountText);
StringBuilder vertCountText = new StringBuilder(stringVertCountText);
Label ObjCount;
Label ObjVertCount;
ListView leftPane;
List<string> rows = new();
List<GameObject> objs = new();
DataSet dataSet = new DataSet("dataSet");
DataTable data = new DataTable("Data");
[MenuItem("Window/Analysis/Hierarchy Analysator")]
public static void ShowMyEditor()
{
EditorWindow wnd = GetWindow<HierarchyAnalys>();
wnd.titleContent = new GUIContent("Hierarchy Analysator");
wnd.maxSize = new Vector2(600, 11080);
wnd.minSize = new Vector2(450, 150);
}
public void CreateGUI()
{
leftPane = new ListView();
Label InfoH = new Label("Base Info");
ObjCount = new Label(countText.ToString());
ObjVertCount = new Label(vertCountText.ToString());
ObjVertCount.style.top = 20;
ObjVertCount.style.left = 10;
ObjCount.style.top = 15;
ObjCount.style.left = 10;
InfoH.style.top = 5;
InfoH.style.left = 5;
InfoH.style.unityFontStyleAndWeight = FontStyle.Bold;
Button Update = new Button(onStat);
Update.text = "Update Data";
Update.style.top = 30;
leftPane.style.top = 40;
leftPane.style.alignContent = Align.Stretch;
leftPane.style.alignItems = Align.Stretch;
leftPane.style.unityTextAlign = TextAnchor.MiddleCenter;
rootVisualElement.Add(InfoH);
rootVisualElement.Add(ObjCount);
rootVisualElement.Add(ObjVertCount);
rootVisualElement.Add(Update);
rootVisualElement.Add(leftPane);
leftPane.onSelectionChange += OnSelect;
}
private void OnGUI() {
leftPane.style.left = 10;
leftPane.style.height = new StyleLength(position.height - 110);
leftPane.style.width = new StyleLength(position.width - 15);
}
void OnSelect(IEnumerable<object> a) {
string sel = a.ToArray()[0].ToString();
string stringId = sel.Substring(0, sel.IndexOf(' '));
int id = int.TryParse(stringId, out int sid) ? sid : -1;
if (id == -1) return;
if (id > objs.Count - 1 || id < 0) {
Debug.LogWarning("Data is not actual, will be updated!");
onStat();
return;
}
Selection.activeGameObject = objs[id];
}
void onStat() {
dataSet = new DataSet("BookStore");
data = new DataTable("Books");
dataSet.Tables.Add(data);
DataColumn key = new DataColumn("Key", System.Type.GetType("System.Int32"));
key.Unique = true; // столбец будет иметь уникальное значение
key.AllowDBNull = false; // не может принимать null
key.AutoIncrement = true; // будет автоинкрементироваться
key.AutoIncrementSeed = 1; // начальное значение
key.AutoIncrementStep = 1; // приращении при добавлении новой строки
DataColumn idColumn = new DataColumn("Id", System.Type.GetType("System.String"));
DataColumn nameColumn = new DataColumn("Name", System.Type.GetType("System.String"));
DataColumn vertCount = new DataColumn("VertCount", System.Type.GetType("System.String"));
data.Columns.Add(idColumn);
data.Columns.Add(nameColumn);
data.Columns.Add(vertCount);
data.PrimaryKey = new DataColumn[] { data.Columns["Key"] };
objs = GetAllObjectsOnlyInScene();
GameObject objI;
StringBuilder stringItem = new();
int[] counts = new int[objs.Count];
List<GameObjectType> objects = new();
int vertSum = 0;
for (int i = 0; i < objs.Count; i++)
{
objI = objs[i];
if (objI.TryGetComponent(out MeshFilter meshF)) {
GameObjectType e = new();
e.id = i;
e.name = objI.name;
e.vertCount = meshF.sharedMesh.vertexCount;
vertSum += meshF.sharedMesh.vertexCount;
objects.Add(e);
}
}
objects.Sort((x, y) => x.vertCount.CompareTo(y.vertCount) );
DataRow aa= data.NewRow();
aa.ItemArray = new object[] { "ID", "Name", "Vert Count"};
data.Rows.Add(aa);
for (int i = objects.Count - 1; i > 0; i--)
{
// stringItem.AppendFormat("{0,-35} {1,-20} {2, -10} \n", objects[i].id, objects[i].name, objects[i].vertCount);
DataRow row = data.NewRow();
row.ItemArray = new object[] { objects[i].id.ToString(), objects[i].name, objects[i].vertCount.ToString()};
data.Rows.Add(row);
// a.Add(stringItem.ToString());
// stringItem.Clear();
// rootVisualElement.Add(new Label(meshF.sharedMesh.vertexCount.ToString()));
}
rows.Clear();
foreach (DataRow r in data.Rows)
{
object alt = null;
foreach (var cell in r.ItemArray)
{
stringItem.Append(cell.ToString().PadLeft(alt != null ? 50 - alt.ToString().Length : 0));
alt = cell;
}
rows.Add(stringItem.ToString());
stringItem.Clear();
}
leftPane.itemsSource = rows;
countText.Clear();
vertCountText.Clear();
countText.Append(stringCountText);
vertCountText.Append(stringVertCountText);
countText.Append(GetAllObjectsOnlyInScene().Count.ToString());
vertCountText.Append(vertSum);
ObjCount.text = countText.ToString();
ObjVertCount.text = vertCountText.ToString();
}
List<GameObject> GetAllObjectsOnlyInScene()
{
List<GameObject> objectsInScene = new List<GameObject>();
foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
{
if (!EditorUtility.IsPersistent(go.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave))
objectsInScene.Add(go);
}
return objectsInScene;
}
}
class GameObjectType
{
public int id;
public string name;
public int vertCount;
}