参考链接:
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 }
输出如下:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?