Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEditor;
- using System.Collections.Generic;
- using System.Reflection;
- using System;
- public static class FindReferenceInScene
- {
- [MenuItem("GameObject/Find Reference in Scene", false, 45)]
- static void DoFindReferenceInScene()
- {
- if (Selection.activeGameObject == null)
- return;
- bool found = false;
- var list = FindAllTransformInScene();
- foreach (var item in list)
- {
- var behaviours = item.GetComponents<MonoBehaviour>();
- //Debug.LogWarning(behaviours.Length + ":" + item.name, item);
- foreach (var b in behaviours)
- {
- Type type;
- try
- {
- type = b.GetType();
- }
- catch (System.Exception e)
- {
- Debug.LogError(item);
- Debug.LogError(e.Message);
- continue;
- }
- var fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
- foreach (var f in fields)
- {
- bool isArray;
- Type baseType;
- if (IsUnitySerializable(f, out isArray, out baseType))
- {
- //Debug.Log(f.ToString() + ":" + isArray + ":" + baseType.ToString());
- if (isArray)
- {
- foreach (var obj in (IEnumerable)f.GetValue(b))
- found = CheckSelection(b, type, f, obj) || found;
- }
- else
- {
- object obj = f.GetValue(b);
- found = CheckSelection(b, type, f, obj) || found;
- }
- }
- }
- }
- }
- if (!found)
- Debug.Log("no reference in current scene. <color=white>" + Selection.activeGameObject + "</color>");
- }
- private static bool CheckSelection(MonoBehaviour b, Type type, FieldInfo f, object obj)
- {
- if (obj == null || obj.Equals(null))
- return false;
- if (obj is GameObject)
- {
- if ((obj as GameObject) == Selection.activeGameObject)
- {
- 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);
- return true;
- }
- }
- else if (obj is Component)
- {
- if ((obj as Component).gameObject == Selection.activeGameObject)
- {
- 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);
- return true;
- }
- }
- else
- {
- // we dont care about other types
- }
- return false;
- }
- private static List<Transform> FindAllTransformInScene()
- {
- var list = new List<Transform>();
- //var roots = new List<Transform>();
- foreach (var item in GameObject.FindObjectsOfType<Transform>())
- {
- if (item == item.root) // 自己是root結點
- list.AddRange(item.GetComponentsInChildren<Transform>(true)); // dark magic here
- }
- return list;
- }
- private static bool IsUnitySerializable(FieldInfo f, out bool isArray, out Type baseType)
- {
- var isPublicOrSerializeField = (f.IsPublic || f.GetCustomAttributes(typeof(SerializeField), true).Length > 0) &&
- (f.GetCustomAttributes(typeof(NonSerializedAttribute), true).Length == 0);
- if (!isPublicOrSerializeField)
- {
- isArray = false;
- baseType = null;
- return false;
- }
- return IsSerializable(f, out isArray, out baseType);
- }
- private static bool IsSerializable(FieldInfo f, out bool isArray, out Type baseType)
- {
- var fieldType = f.FieldType;
- if (fieldType.IsArray)
- {
- baseType = fieldType.GetElementType();
- isArray = true;
- }
- else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
- {
- baseType = fieldType.GetGenericArguments()[0];
- isArray = true;
- }
- else
- {
- baseType = fieldType;
- isArray = false;
- }
- return typeof(UnityEngine.Object).IsAssignableFrom(baseType) || baseType.GetCustomAttributes(typeof(SerializableAttribute), true).Length > 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement