zoukankan      html  css  js  c++  java
  • [Unity工具]查找GameObject在场景中所有被引用的地方

    参考链接:

    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 }

    输出如下:

  • 相关阅读:
    重磅!容器集群监控利器 阿里云Prometheus 正式免费公测
    阿里开源 KT Connnect,轻量级云原生测试环境治理平台来啦!
    使用Velero Restic快速完成云原生应用迁移至ACK集群
    Kubernetes 弹性伸缩全场景解析 (一):概念延伸与组件布局
    北京DAY1下午
    洛谷 P3041 [USACO12JAN] Video Game Combos
    Tyvj 1729 文艺平衡树
    bzoj 3238: [AHOI2013]差异
    bzoj 2957: 楼房重建
    bzoj 2660: [Beijing wc2012]最多的方案
  • 原文地址:https://www.cnblogs.com/lyh916/p/10372366.html
Copyright © 2011-2022 走看看