拖动到父节点的gameobject,太多后不好找。添加打印,获取类名和字段名。
[MenuItem("GameObject/获取对象引用的类名和字段名", false, 32)]
public static void GetCurParentRefName()
{
Transform selectChild = Selection.activeTransform;
if (selectChild == null)
{
Debug.LogError("You must select Obj first!");
return;
}
if (selectChild != null)
{
int id = selectChild.GetInstanceID();
int parentCount = 0;
while (selectChild.parent != null)
{
parentCount++;
selectChild = selectChild.parent;
var coms = selectChild.GetComponents<Component>();
foreach (Component v in coms)
{
if (!(v is MonoBehaviour))
{
continue;
}
System.Type clsType = v.GetType();
var properties = clsType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (System.Reflection.FieldInfo info in properties)
{
var attributes = info.GetCustomAttributes(typeof(System.ObsoleteAttribute), true);
if (attributes.Length > 0)
{
continue;
}
var realValue = info.GetValue(v) as UnityEngine.Component;
if (realValue && realValue.transform)
{
if (realValue.transform.GetInstanceID() == id)
{
Debug.Log(string.Format("向上层数:{0}|节点名:{1}|类名:{2}|字段名:{3}", parentCount, selectChild.name, clsType.Name, info.Name));
return;
}
}
}
}
}
}
Debug.Log("not find !");
}