Unity3D编辑器:查找一个物体在哪些物体上引用

如题。文章源自大腿Plus-https://www.shijunzh.com/archives/227

借大腿宝地,发一点东西。 感谢!文章源自大腿Plus-https://www.shijunzh.com/archives/227

  1. 新建Editor文件夹,把脚本放进去,
  2. 拖选你要找的物体上去。
  3. 选择你从那些物体里查找,简单点就ctrl+a 全选得了。点击按钮,
  4. 如果有会打印在console面板。
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Reflection;
using System;
using System.Linq;
using System.Collections.Generic;
namespace ToolManage
{
    /// <summary>  查找一个物体拖拽到了哪些物体上。 </summary> 
    public class FindDragObject : ScriptableWizard
    {
        public GameObject targetObj;
        public void OnWizardUpdate()
        {
            helpString = "选择你要查看的物体";
            isValid = targetObj != null;
        }
        private void OnWizardCreate()
        {
            GameObject[] roots = Selection.gameObjects;
            Dictionary<string, GameObject> dic = new Dictionary<string, GameObject>();
            foreach (var item in roots)
            {
                if (!dic.ContainsKey(item.transform.root.name))
                    dic.Add(item.transform.root.name, item.transform.root.gameObject);
            }
            foreach (var item in dic) { GetCompent(item.Value); }
        }
        private void GetCompent(GameObject gameObject)
        {
            Component[] components = gameObject.GetComponentsInChildren<Component>();
            foreach (var item in components)
            {
                GetPropertiesInCompent(item);
            }
        }
        private void GetPropertiesInCompent(Component item)
        {
            if (item.gameObject == targetObj) { return; }
            List<PropertyInfo> properties = item.GetType().GetProperties().Where(p => p.PropertyType.IsPublic && !p.PropertyType.IsValueType && p.PropertyType != typeof(Component)).ToList<PropertyInfo>();
            foreach (var propertyItem in properties)
            {
                var value = propertyItem.GetValue(item, null);
                if (value != null)
                {
                    if (value == (object)targetObj)
                    {
                        Debug.Log("找到名称为下列的物体"); Debug.Log(item.gameObject.name);
                    }
                }
            }
            GetFieldsInCompent(item);
        }
        private void GetFieldsInCompent(Component item)
        {
            List<FieldInfo> fields = item.GetType().GetFields().Where(p => p.FieldType.IsPublic && !p.FieldType.IsValueType).ToList<FieldInfo>(); foreach (var fieldItem in fields)
            {
                var value = fieldItem.GetValue(item);
                if (value != null)
                {
                    if (value == (object)targetObj)
                    {
                        Debug.Log("找到名称为下列的物体");
                        Debug.Log(item.gameObject.name);
                    }
                }
            }
        }
        [MenuItem("Tools/开始查询")]
        public static void FindObjectNames()
        {
            ScriptableWizard.DisplayWizard<FindDragObject>("FindNames", "FindObjectNames");
        }
    }
}

可能会有一些问题,有问题大家提,一起修改哈。文章源自大腿Plus-https://www.shijunzh.com/archives/227

大腿Plus
  • 本文由 发表于 2017年2月15日16:36:13
  • 转载请务必保留本文链接:https://www.shijunzh.com/archives/227

发表评论