A Unity runtime inspection plugin for fun
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

124 строки
3.7KB

  1. using System;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace Dissonance.Inspector
  7. {
  8. public struct Instance
  9. {
  10. public string name;
  11. public UnityEngine.Object obj;
  12. public Type type;
  13. public MethodBase[] methods;
  14. public Dictionary<string, object> properties;
  15. public UnityEngine.HideFlags hideFlags;
  16. public Instance(UnityEngine.Object obj)
  17. {
  18. this.name = obj.name;
  19. this.obj = obj;
  20. this.type = obj.GetType();
  21. this.methods = this.type.GetMethods();
  22. this.properties = new Dictionary<string, object>();
  23. foreach (PropertyInfo propInf in this.type.GetProperties())
  24. {
  25. if (propInf.CanRead && propInf.GetGetMethod().IsPublic)
  26. {
  27. this.properties.Add(propInf.Name, propInf.GetValue(obj));
  28. }
  29. }
  30. this.hideFlags = obj.hideFlags;
  31. }
  32. public override string ToString()
  33. {
  34. StringBuilder sb = new StringBuilder();
  35. sb.Append("{\n");
  36. if (type != null && type.FullName != null)
  37. {
  38. sb.Append($"\"type\": \"{type.FullName}\",\n");
  39. }
  40. if (name != null)
  41. {
  42. sb.Append($"\"name\": \"{name}\",\n");
  43. }
  44. if (obj != null)
  45. {
  46. sb.Append($"\"object\": \"{obj.ToString()}\",\n");
  47. } else
  48. {
  49. sb.Append($"\"object\": null,\n");
  50. }
  51. sb.Append($"\"hideFlags\": \"{hideFlags}\",\n");
  52. sb.Append("\"properties\": {\n");
  53. int count = 1;
  54. foreach (string key in properties.Keys)
  55. {
  56. if (properties[key] != null)
  57. {
  58. sb.Append($" \"{key}\": \"{properties[key].ToString()}\"");
  59. } else
  60. {
  61. sb.Append($" \"{key}\": null");
  62. }
  63. if (count != properties.Keys.Count)
  64. {
  65. sb.Append(",\n");
  66. }
  67. count++;
  68. }
  69. sb.Append("\n},\n");
  70. sb.Append("\"methods\": [\n");
  71. count = 1;
  72. foreach (MethodBase m in methods)
  73. {
  74. sb.Append($" \"{m.Name}");
  75. if (m.ContainsGenericParameters)
  76. {
  77. sb.Append("<");
  78. Type[] genArgs = m.GetGenericArguments();
  79. int argCount = 1;
  80. foreach (Type t in genArgs)
  81. {
  82. sb.Append(t.Name);
  83. if (argCount != genArgs.Length)
  84. {
  85. sb.Append(",");
  86. }
  87. argCount++;
  88. }
  89. sb.Append(">");
  90. }
  91. sb.Append("(");
  92. ParameterInfo[] parameters = m.GetParameters();
  93. int paramCount = 1;
  94. foreach(ParameterInfo p in parameters)
  95. {
  96. sb.Append($"{p.ParameterType.FullName} {p.Name}");
  97. if (paramCount != parameters.Length)
  98. {
  99. sb.Append(", ");
  100. }
  101. paramCount++;
  102. }
  103. sb.Append(")\"");
  104. if (count != methods.Length)
  105. {
  106. sb.Append(",\n");
  107. }
  108. count++;
  109. }
  110. sb.Append("\n]\n");
  111. sb.Append("}");
  112. return sb.ToString();
  113. }
  114. }
  115. }