public static ToolPrintData PrintData(object obj) { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); // 性能监测开始 if (obj == null) { return(null); } if (hashList == null) { hashList = new List <int>(); } ToolPrintData baseData = new ToolPrintData(); RecursionPrintData(baseData, obj); hashList.Clear(); stopwatch.Stop(); return(baseData); Debug.Log("print data success time is " + stopwatch.Elapsed.TotalMilliseconds + "ms"); }
private static TreeViewItem GetTreeViewItem(ToolPrintData baseData) { TreeViewItem item = new TreeViewItem(0, -1, "root"); List <TreeViewItem> itemList = GetChildItems(1, baseData.childDataList); if (itemList != null) { itemList.ForEach(x => item.AddChild(x)); } return(item); }
public static void SetData(ToolPrintData data) { if (treeViewState == null) { treeViewState = new TreeViewState(); } if (data == null) { return; } IList <int> expandIdList = null; if (treeView != null) { expandIdList = treeView.GetExpanded(); } treeView = new ToolPrintDataTree(treeViewState); id = 0; treeView.root.AddChild(GetTreeViewItem(data)); treeView.Reload(); if (expandIdList != null) { if (needExpand && useOldExpand) { for (int i = 0; i < expandIdList.Count; i++) { treeView.SetExpanded(expandIdList[i], true); } } else { for (int i = 0; i < expandIdList.Count; i++) { treeView.SetExpanded(expandIdList[i], false); } } } _canShow = true; }
private static void RecursionPrintData(ToolPrintData parentData, object obj, string name = null) { if (obj != null) { int index = objList.FindIndex(x => x.Equals(obj)); if (index != -1) { ToolPrintData data = new ToolPrintData(); data.name = name == null?obj.GetType().Name : name; data.content = obj.ToString(); parentData.childDataList.Add(data); return; } objList.Add(obj); } ToolPrintData childData = new ToolPrintData(); childData.name = name == null?obj.GetType().ToString() : name; if (obj is string str) { childData.content = str; } else if (obj.GetType().IsArray) { Array array = obj as Array; for (int i = 0; i < array.Length; i++) { RecursionPrintData(childData, array.GetValue(i)); } } else if (obj is IList iList) { for (int i = 0; i < iList.Count; i++) { RecursionPrintData(childData, iList[i]); } } else if (obj.GetType().IsEnum) { childData.content = obj.ToString(); } else if (obj is Vector3 || obj is Vector4 || obj is Vector2 || obj is Quaternion) { childData.content = obj.ToString(); } else if (obj is IDictionary iDic) { ToolPrintData keyData = new ToolPrintData(); keyData.name = "keys"; foreach (object key in iDic.Keys) { RecursionPrintData(keyData, key, key.GetType().ToString()); } ToolPrintData valueData = new ToolPrintData(); valueData.name = "value"; foreach (object value in iDic.Values) { RecursionPrintData(valueData, value, value.GetType().ToString()); } childData.childDataList.Add(keyData); childData.childDataList.Add(valueData); } else { // obj.GetType().get List <FieldInfo> infos = obj.GetType().GetRuntimeFields().ToList(); infos.Sort((a, b) => String.Compare(a.Name, b.Name, StringComparison.Ordinal)); if (infos.Count == 0) { childData.content = obj.ToString(); } else { for (int i = 0; i < infos.Count; i++) { // if (infos[i].PropertyType.ToString() != "UnityEngine.GameObject" && infos[i].PropertyType.ToString() != "UnityEngine.Transform") { RecursionPropertyInfo(infos[i], obj, childData); } } } MonoBehaviour[] components = null; if (obj is Transform childTran) { components = childTran.GetComponents <MonoBehaviour>(); } else if (obj is GameObject gameObject) { components = gameObject.GetComponents <MonoBehaviour>(); } if (components != null) { for (int i = 0; i < components.Length; i++) { RecursionPrintData(childData, components[i], "Mono_" + components[i].GetType()); } } if (obj is MonoBehaviour) { FieldInfo[] fileInfos = obj.GetType().GetFields(); for (int i = 0; i < fileInfos.Length; i++) { try { object o = fileInfos[i].GetValue(obj); if (o == null || (o is GameObject gameObj && gameObj.GetInstanceID() == 0)) { ToolPrintData data = new ToolPrintData(); data.name = fileInfos[i].Name; data.content = "Null"; childData.childDataList.Add(data); } else { RecursionPrintData(childData, o, fileInfos[i].Name); } }
private static void RecursionPrintData(ToolPrintData parentData, object obj) { try { if (obj != null) { int hash = obj.GetHashCode(); if (hash != 0 && hashList.Contains(hash)) { return; } if (hash != 0) { hashList.Add(hash); } } ToolPrintData childData = new ToolPrintData(); if (obj != null) { childData.name = obj.GetType().ToString(); } if (obj == null) { childData.content = "null"; } else if (obj is string str) { childData.content = str; } else if (obj.GetType().IsArray) { Array array = obj as Array; for (int i = 0; i < array.Length; i++) { RecursionPrintData(childData, array.GetValue(i)); } } else if (obj.GetType().IsGenericType&& obj.GetType().GetGenericTypeDefinition() == typeof(List <>)) { IList iList = obj as IList; for (int i = 0; i < iList.Count; i++) { RecursionPrintData(childData, iList[i]); } } else { PropertyInfo[] infos = obj.GetType().GetProperties(); if (infos.Length == 0) { childData.content = obj.ToString(); } else { for (int i = 0; i < infos.Length; i++) { RecursionPrintData(childData, infos[i], obj); } } } parentData.childDataList.Add(childData); } catch (Exception e) { Console.WriteLine(e); throw; } }
private static void RecursionPrintData(ToolPrintData parentData, PropertyInfo info, object obj) { try { ToolPrintData childData = new ToolPrintData(); childData.name = info.Name; bool hasAdd = false; //处理unity类型 if (!(obj is string) && !obj.GetType().IsArray&& (!obj.GetType().IsGenericType || obj.GetType().GetGenericTypeDefinition() != typeof(List <>))) { if (info.PropertyType.BaseType.FullName == "System.ValueType") { childData.content = info.GetValue(obj).ToString(); hasAdd = true; } else if (info.PropertyType.FullName == "UnityEngine.Component") { childData.content = info.GetType().ToString(); hasAdd = true; } else { object o = info.GetValue(obj); if (o != null) { int hash = o.GetHashCode(); if (hashList.Contains(hash)) { return; } hashList.Add(hash); } } } if (hasAdd) { } else if (obj == null || info == null) { childData.content = "null"; } else if (obj is string str) { childData.content = str; } else if (obj.GetType().IsArray) { Array array = obj as Array; for (int i = 0; i < array.Length; i++) { RecursionPrintData(childData, array.GetValue(i)); } } else if (obj.GetType().IsGenericType&& obj.GetType().GetGenericTypeDefinition() == typeof(List <>)) { IList iList = obj as IList; for (int i = 0; i < iList.Count; i++) { RecursionPrintData(childData, iList[i]); } } else { object valueObj = info.GetValue(obj, null); if (valueObj == null) { childData.content = "null"; } else { PropertyInfo[] infos = valueObj.GetType().GetProperties(); if (infos.Length == 0) { childData.content = valueObj.ToString(); } else { for (int i = 0; i < infos.Length; i++) { RecursionPrintData(childData, infos[i], valueObj); } } } } parentData.childDataList.Add(childData); } catch (Exception e) { Console.WriteLine(e); throw; } }