如题。
借大腿宝地,发一点东西。 感谢!
- 新建Editor文件夹,把脚本放进去,
- 拖选你要找的物体上去。
- 选择你从那些物体里查找,简单点就ctrl+a 全选得了。点击按钮,
- 如果有会打印在console面板。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
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"); } } } |
可能会有一些问题,有问题大家提,一起修改哈。
- 本文固定链接: https://www.shijunzh.com/archives/227
- 转载请注明: 大腿Plus 于 大腿Plus 发表