Advertisement
Guest User

Untitled

a guest
Jan 27th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using System;
  7.  
  8. public static class FindReferenceInScene
  9. {
  10. [MenuItem("GameObject/Find Reference in Scene", false, 45)]
  11. static void DoFindReferenceInScene()
  12. {
  13. if (Selection.activeGameObject == null)
  14. return;
  15.  
  16. bool found = false;
  17. var list = FindAllTransformInScene();
  18. foreach (var item in list)
  19. {
  20. var behaviours = item.GetComponents<MonoBehaviour>();
  21. //Debug.LogWarning(behaviours.Length + ":" + item.name, item);
  22.  
  23. foreach (var b in behaviours)
  24. {
  25. Type type;
  26. try
  27. {
  28. type = b.GetType();
  29. }
  30. catch (System.Exception e)
  31. {
  32. Debug.LogError(item);
  33. Debug.LogError(e.Message);
  34. continue;
  35. }
  36.  
  37. var fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
  38. foreach (var f in fields)
  39. {
  40. bool isArray;
  41. Type baseType;
  42. if (IsUnitySerializable(f, out isArray, out baseType))
  43. {
  44. //Debug.Log(f.ToString() + ":" + isArray + ":" + baseType.ToString());
  45. if (isArray)
  46. {
  47. foreach (var obj in (IEnumerable)f.GetValue(b))
  48. found = CheckSelection(b, type, f, obj) || found;
  49. }
  50. else
  51. {
  52. object obj = f.GetValue(b);
  53. found = CheckSelection(b, type, f, obj) || found;
  54. }
  55. }
  56. }
  57. }
  58. }
  59. if (!found)
  60. Debug.Log("no reference in current scene. <color=white>" + Selection.activeGameObject + "</color>");
  61. }
  62.  
  63. private static bool CheckSelection(MonoBehaviour b, Type type, FieldInfo f, object obj)
  64. {
  65. if (obj == null || obj.Equals(null))
  66.  
  67. return false;
  68.  
  69. if (obj is GameObject)
  70. {
  71. if ((obj as GameObject) == Selection.activeGameObject)
  72. {
  73. Debug.Log(string.Format("<color=green>{0}</color>.<color=white>{1}</color> ({2}), on <color=red>{3}</color>", type, f.Name, f.FieldType, b.gameObject.name), b.gameObject);
  74. return true;
  75. }
  76. }
  77. else if (obj is Component)
  78. {
  79. if ((obj as Component).gameObject == Selection.activeGameObject)
  80. {
  81. Debug.Log(string.Format("<color=green>{0}</color>.<color=white>{1}</color> ({2}), on <color=red>{3}</color>", type, f.Name, f.FieldType, b.gameObject.name), b.gameObject);
  82. return true;
  83. }
  84. }
  85. else
  86. {
  87. // we dont care about other types
  88. }
  89.  
  90. return false;
  91. }
  92.  
  93. private static List<Transform> FindAllTransformInScene()
  94. {
  95. var list = new List<Transform>();
  96. //var roots = new List<Transform>();
  97.  
  98. foreach (var item in GameObject.FindObjectsOfType<Transform>())
  99. {
  100. if (item == item.root) // 自己是root結點
  101. list.AddRange(item.GetComponentsInChildren<Transform>(true)); // dark magic here
  102. }
  103. return list;
  104. }
  105.  
  106. private static bool IsUnitySerializable(FieldInfo f, out bool isArray, out Type baseType)
  107. {
  108. var isPublicOrSerializeField = (f.IsPublic || f.GetCustomAttributes(typeof(SerializeField), true).Length > 0) &&
  109. (f.GetCustomAttributes(typeof(NonSerializedAttribute), true).Length == 0);
  110. if (!isPublicOrSerializeField)
  111. {
  112. isArray = false;
  113. baseType = null;
  114. return false;
  115. }
  116.  
  117. return IsSerializable(f, out isArray, out baseType);
  118. }
  119.  
  120. private static bool IsSerializable(FieldInfo f, out bool isArray, out Type baseType)
  121. {
  122. var fieldType = f.FieldType;
  123. if (fieldType.IsArray)
  124. {
  125. baseType = fieldType.GetElementType();
  126. isArray = true;
  127. }
  128. else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
  129. {
  130. baseType = fieldType.GetGenericArguments()[0];
  131. isArray = true;
  132. }
  133. else
  134. {
  135. baseType = fieldType;
  136. isArray = false;
  137. }
  138.  
  139. return typeof(UnityEngine.Object).IsAssignableFrom(baseType) || baseType.GetCustomAttributes(typeof(SerializableAttribute), true).Length > 0;
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement