lyh916

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

参考链接:

https://blog.csdn.net/hjzyzr/article/details/53316919?utm_source=blogxgwz4

https://blog.csdn.net/weixin_39706943/article/details/80507276

 

0.场景如下:

 

1.获取场景中所有的Go,包括嵌套的,隐藏的。需要注意的是要进行go.scene.name != null,这句判断,否则会包含很多来自其他地方的go(不只是来自Project视图)

 1 using UnityEngine;
 2 using UnityEditor;
 3 
 4 /// <summary>
 5 /// 查找GameObject在场景中所有被引用的地方
 6 /// </summary>
 7 public class FindGoInScene {
 8 
 9     [MenuItem("GameObject/FindGoInScene", priority = 0)]
10     static void Init()
11     {
12         GameObject[] goes = Resources.FindObjectsOfTypeAll<GameObject>();
13         for (int i = 0; i < goes.Length; i++)
14         {
15             GameObject go = goes[i];
16             if (go.scene.name != null)
17             {
18                 Debug.Log(go.name);
19             }
20         }
21     }
22 }

 

输出如下:

 

2.查找引用

TestFindGoInScene.cs

1 using UnityEngine;
2 
3 public class TestFindGoInScene : MonoBehaviour {
4 
5     public GameObject temp;
6 }

 

FindGoInScene.cs

 1 using UnityEngine;
 2 using UnityEditor;
 3 using System.Reflection;
 4 
 5 /// <summary>
 6 /// 查找GameObject在场景中所有被引用的地方
 7 /// </summary>
 8 public class FindGoInScene {
 9 
10     [MenuItem("GameObject/FindGoInScene", priority = 0)]
11     static void Init()
12     {
13         GameObject[] goes = Resources.FindObjectsOfTypeAll<GameObject>();
14         for (int i = 0; i < goes.Length; i++)
15         {
16             GameObject go = goes[i];
17             if (go.scene.name != null)
18             {
19                 if (go != Selection.activeGameObject)
20                 {
21                     //Debug.Log(go.name);
22                     Find(go);
23                 }
24             }
25         }
26     }
27 
28     static void Find(GameObject go)
29     {
30         Component[] components = go.GetComponents<Component>();
31         for (int i = 0; i < components.Length; i++)
32         {
33             Component component = components[i];
34             FieldInfo[] fields = component.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
35             for (int j = 0; j < fields.Length; j++)
36             {
37                 var value = fields[j].GetValue(component);
38                 if (value == (object)Selection.activeGameObject)
39                 {
40                     Debug.LogWarning(go.name);
41                 }
42             }
43         }
44     }
45 }

 

输出如下:

posted on 2019-02-13 23:21  艰苦奋斗中  阅读(1348)  评论(0编辑  收藏  举报