|
- using System;
- using System.Reflection;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
-
- namespace Dissonance.Inspector
- {
- public struct Instance
- {
- public string name;
-
- public UnityEngine.Object obj;
-
- public Type type;
-
- public MethodBase[] methods;
-
- public Dictionary<string, object> properties;
-
- public UnityEngine.HideFlags hideFlags;
-
- public Instance(UnityEngine.Object obj)
- {
- this.name = obj.name;
- this.obj = obj;
- this.type = obj.GetType();
- this.methods = this.type.GetMethods();
- this.properties = new Dictionary<string, object>();
- foreach (PropertyInfo propInf in this.type.GetProperties())
- {
- if (propInf.CanRead && propInf.GetGetMethod().IsPublic)
- {
- this.properties.Add(propInf.Name, propInf.GetValue(obj));
- }
- }
- this.hideFlags = obj.hideFlags;
- }
-
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("{\n");
- if (type != null && type.FullName != null)
- {
- sb.Append($"\"type\": \"{type.FullName}\",\n");
- }
- if (name != null)
- {
- sb.Append($"\"name\": \"{name}\",\n");
- }
- if (obj != null)
- {
- sb.Append($"\"object\": \"{obj.ToString()}\",\n");
- } else
- {
- sb.Append($"\"object\": null,\n");
- }
- sb.Append($"\"hideFlags\": \"{hideFlags}\",\n");
- sb.Append("\"properties\": {\n");
- int count = 1;
- foreach (string key in properties.Keys)
- {
- if (properties[key] != null)
- {
- sb.Append($" \"{key}\": \"{properties[key].ToString()}\"");
- } else
- {
- sb.Append($" \"{key}\": null");
- }
- if (count != properties.Keys.Count)
- {
- sb.Append(",\n");
- }
- count++;
- }
- sb.Append("\n},\n");
- sb.Append("\"methods\": [\n");
- count = 1;
- foreach (MethodBase m in methods)
- {
- sb.Append($" \"{m.Name}");
- if (m.ContainsGenericParameters)
- {
- sb.Append("<");
- Type[] genArgs = m.GetGenericArguments();
- int argCount = 1;
- foreach (Type t in genArgs)
- {
- sb.Append(t.Name);
- if (argCount != genArgs.Length)
- {
- sb.Append(",");
- }
- argCount++;
- }
- sb.Append(">");
- }
- sb.Append("(");
- ParameterInfo[] parameters = m.GetParameters();
- int paramCount = 1;
- foreach(ParameterInfo p in parameters)
- {
- sb.Append($"{p.ParameterType.FullName} {p.Name}");
- if (paramCount != parameters.Length)
- {
- sb.Append(", ");
- }
- paramCount++;
- }
- sb.Append(")\"");
- if (count != methods.Length)
- {
- sb.Append(",\n");
- }
- count++;
- }
- sb.Append("\n]\n");
- sb.Append("}");
- return sb.ToString();
- }
- }
- }
|