|   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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
 | using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using System.Linq;
public class CustomProfilerWindow : EditorWindow
{
    protected DatabaseDisplayer databaseDisplay;
    private const string DataPath = "Assets/CustomProfilerData.asset";
    [MenuItem("Tools/Profiler Window")]
    static void Open()
    {
        GetWindow<CustomProfilerWindow>().SetData();
    }
    [MenuItem("Tools/Profiler Create Data")]
    static void CreateData()
    {
        var data = CreateInstance<ProfilerData>();
        AssetDatabase.CreateAsset(data, DataPath);
        for (int i = 0; i < 10; i++)
        {
            var pData = CreateInstance<ProfilerDataElement>();
            pData.Name = "name_" + i;
            pData.Calls = Random.Range(0, 50);
            pData.Time = (float)System.Math.Round(Random.Range(0, 100f), 2);
            pData.name = i.ToString();
            data.dataList.Add(pData);
            AssetDatabase.AddObjectToAsset(pData, data);
        }
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
    void SetData()
    {
        var data = AssetDatabase.LoadAssetAtPath<ProfilerData>(DataPath);
        if (data == null || data.dataList.Count == 0)
        {
            Debug.LogError("no correct data");
            return;
        }
        SerializedObject tempObj = new SerializedObject(data.dataList[0]);
        SerializedProperty prop = tempObj.GetIterator();
        int propCount = prop.CountRemaining();
        prop.Reset();
        prop.Next(true);
        //do it once to "jump over" the script file, we don't want that property.
        prop.NextVisible(false);
        MultiColumnHeaderState.Column[] columns = new MultiColumnHeaderState.Column[propCount - 1];
        for (int i = 0; i < propCount - 1; ++i)
        {
            prop.NextVisible(false);
            columns[i] = new MultiColumnHeaderState.Column();
            columns[i].allowToggleVisibility = false;
            columns[i].headerContent = new GUIContent(prop.displayName);
            columns[i].minWidth = GetPropertyWidthFromType(prop.propertyType);
            columns[i].width = columns[i].minWidth;
            columns[i].canSort = CanSort(prop.propertyType);
        }
        MultiColumnHeaderState headerstate = new MultiColumnHeaderState(columns);
        MultiColumnHeader header = new MultiColumnHeader(headerstate);
        TreeViewState state = new TreeViewState();
        databaseDisplay = new DatabaseDisplayer(state, header, data);
        databaseDisplay.Reload();
    }
    bool CanSort(SerializedPropertyType type)
    {
        switch (type)
        {
            case SerializedPropertyType.AnimationCurve:
            case SerializedPropertyType.Bounds:
            case SerializedPropertyType.BoundsInt:
            case SerializedPropertyType.Character:
            case SerializedPropertyType.Color:
            case SerializedPropertyType.ExposedReference:
            case SerializedPropertyType.FixedBufferSize:
            case SerializedPropertyType.Generic:
            case SerializedPropertyType.Gradient:
            case SerializedPropertyType.ObjectReference:
            case SerializedPropertyType.Quaternion:
            case SerializedPropertyType.Rect:
            case SerializedPropertyType.RectInt:
            case SerializedPropertyType.Vector2:
            case SerializedPropertyType.Vector2Int:
            case SerializedPropertyType.Vector3:
            case SerializedPropertyType.Vector3Int:
            case SerializedPropertyType.Vector4:
                return false;
            default:
                break;
        }
        return true;
    }
    private void OnGUI()
    {
        if (databaseDisplay != null)
        {
            float startY = 0;
            Rect r = new Rect(0, startY, position.width, position.height - startY);
            databaseDisplay.OnGUI(r);
        }
    }
    private float GetPropertyWidthFromType(SerializedPropertyType type)
    {
        float newSize = 32;
        switch (type)
        {
            case SerializedPropertyType.AnimationCurve:
                newSize = 128;
                break;
            case SerializedPropertyType.Vector2:
            case SerializedPropertyType.Vector2Int:
                newSize = 64 * 2;
                break;
            case SerializedPropertyType.Vector3:
            case SerializedPropertyType.Vector3Int:
                newSize = 64 * 3;
                break;
            case SerializedPropertyType.Vector4:
                newSize = 64 * 4;
                break;
            case SerializedPropertyType.Float:
            case SerializedPropertyType.Integer:
                newSize = 64;
                break;
            case SerializedPropertyType.ObjectReference:
                newSize = 128;
                break;
            default:
                break;
        }
        return newSize;
    }
}
public class DatabaseDisplayer : TreeView
{
    protected int _freeID = 0;
    protected ProfilerData data;
    public DatabaseDisplayer(TreeViewState state, MultiColumnHeader header, ProfilerData pData) : base(state, header)
    {
        _freeID = 0;
        data = pData;
        showAlternatingRowBackgrounds = true;
        showBorder = true;
        cellMargin = 6;
        multiColumnHeader.sortingChanged += OnSortingChanged;
        multiColumnHeader.ResizeToFit();
    }
    void OnSortingChanged(MultiColumnHeader multiColumnHeader)
    {
        Sort(GetRows());
        Repaint();
    }
    public int GetNewID()
    {
        int id = _freeID;
        _freeID += 1;
        return id;
    }
    void Sort(IList<TreeViewItem> rows)
    {
        if (multiColumnHeader.sortedColumnIndex == -1)
            return;
        if (rows.Count == 0)
            return;
        int sortedColumn = multiColumnHeader.sortedColumnIndex;
        var childrens = rootItem.children.Cast<DatabaseViewerItem>();
        var comparer = new SerializePropertyComparer();
        var ordered = multiColumnHeader.IsSortedAscending(sortedColumn) ? 
            childrens.OrderBy(k => k.properties[sortedColumn], comparer) : 
            childrens.OrderByDescending(k => k.properties[sortedColumn], comparer);
        rows.Clear();
        foreach (var v in ordered) rows.Add(v);
    }
    protected override void RowGUI(RowGUIArgs args)
    {
        var item = (DatabaseViewerItem)args.item;
        //GUI.enabled = false;
        for (int i = 0; i < args.GetNumVisibleColumns(); ++i)
        {
            Rect r = args.GetCellRect(i);
            int col = args.GetColumn(i);
            //EditorGUI.PropertyField(r, item.properties[col], GUIContent.none, false);
            var value = GetPropertyValue(item.properties[col]);
            EditorGUI.LabelField(r, value.ToString());
        }
        //GUI.enabled = true;
    }
    protected override TreeViewItem BuildRoot()
    {
        TreeViewItem root = new TreeViewItem();
        root.depth = -1;
        root.id = -1;
        root.parent = null;
        root.children = new List<TreeViewItem>();
        if (data.dataList != null)
        {
            for (int i = 0; i < data.dataList.Count; ++i)
            {
                var child = DatabaseViewerItem.CreateFromUnityObject(data.dataList[i], this);
                root.AddChild(child);
            }
        }
        return root;
    }
    public static object GetPropertyValue(SerializedProperty prop)
    {
        if (prop == null) throw new System.ArgumentNullException("prop");
        switch (prop.propertyType)
        {
            case SerializedPropertyType.Integer:
                return prop.intValue;
            case SerializedPropertyType.Boolean:
                return prop.boolValue;
            case SerializedPropertyType.Float:
                return prop.floatValue;
            case SerializedPropertyType.String:
                return prop.stringValue;
            case SerializedPropertyType.Color:
                return prop.colorValue;
            case SerializedPropertyType.ObjectReference:
                return prop.objectReferenceValue;
            case SerializedPropertyType.LayerMask:
                return (LayerMask)prop.intValue;
            case SerializedPropertyType.Enum:
                return prop.enumValueIndex;
            case SerializedPropertyType.Vector2:
                return prop.vector2Value;
            case SerializedPropertyType.Vector3:
                return prop.vector3Value;
            case SerializedPropertyType.Vector4:
                return prop.vector4Value;
            case SerializedPropertyType.Rect:
                return prop.rectValue;
            case SerializedPropertyType.ArraySize:
                return prop.arraySize;
            case SerializedPropertyType.Character:
                return (char)prop.intValue;
            case SerializedPropertyType.AnimationCurve:
                return prop.animationCurveValue;
            case SerializedPropertyType.Bounds:
                return prop.boundsValue;
            case SerializedPropertyType.Gradient:
                throw new System.InvalidOperationException("Can not handle Gradient types.");
        }
        return null;
    }
}
public class DatabaseViewerItem : TreeViewItem
{
    public SerializedObject obj;
    public SerializedProperty[] properties;
    public static DatabaseViewerItem CreateFromUnityObject(UnityEngine.Object unityObject, DatabaseDisplayer treeView)
    {
        SerializedObject so = new SerializedObject(unityObject);
        DatabaseViewerItem newItem = new DatabaseViewerItem();
        newItem.children = new List<TreeViewItem>();
        newItem.depth = 0;
        newItem.id = treeView.GetNewID();
        newItem.obj = so;
        SerializedProperty prop = so.GetIterator();
        prop.Next(true);
        prop.NextVisible(false);
        newItem.properties = new SerializedProperty[treeView.multiColumnHeader.state.columns.Length];
        for (int k = 0; k < newItem.properties.Length; ++k)
        {
            prop.NextVisible(false);
            newItem.properties[k] = prop.Copy();
        }
        return newItem;
    }
}
public class SerializePropertyComparer : IComparer<SerializedProperty>
{
    public int Compare(SerializedProperty x, SerializedProperty y)
    {
        return GenericCompare(x, y);
    }
    // TODO : look for a betetr way this probably generate ton of garbage + need to be extended manually
    int GenericCompare(SerializedProperty a, SerializedProperty b)
    {
        if (a.propertyType != b.propertyType)
        {
            Debug.LogError("Couldn't compare 2 SerializedProeprty of different type");
            return 0;
        }
        switch (a.propertyType)
        {
            case SerializedPropertyType.AnimationCurve:
            case SerializedPropertyType.Bounds:
            case SerializedPropertyType.BoundsInt:
            case SerializedPropertyType.Character:
            case SerializedPropertyType.Color:
            case SerializedPropertyType.ExposedReference:
            case SerializedPropertyType.FixedBufferSize:
            case SerializedPropertyType.Generic:
            case SerializedPropertyType.Gradient:
            case SerializedPropertyType.ObjectReference:
            case SerializedPropertyType.Quaternion:
            case SerializedPropertyType.Rect:
            case SerializedPropertyType.RectInt:
            case SerializedPropertyType.Vector2:
            case SerializedPropertyType.Vector2Int:
            case SerializedPropertyType.Vector3:
            case SerializedPropertyType.Vector3Int:
            case SerializedPropertyType.Vector4:
                return 0;
            case SerializedPropertyType.Boolean:
                return a.boolValue.CompareTo(b.boolValue);
            case SerializedPropertyType.Enum:
                return a.enumValueIndex.CompareTo(b.enumValueIndex);
            case SerializedPropertyType.Float:
                return a.floatValue.CompareTo(b.floatValue);
            case SerializedPropertyType.Integer:
                return a.intValue.CompareTo(b.intValue);
            case SerializedPropertyType.LayerMask:
                return a.intValue.CompareTo(b.intValue);
            case SerializedPropertyType.String:
                return a.stringValue.CompareTo(b.stringValue);
        }
        return 0;
    }
}
 |