Unity 获取场景中所有目标对象(包括不激活的对象)

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class ExampleScript : MonoBehaviour
{
    // 获取场景中所有目标对象(包括不激活的对象)不包括Prefabs:

    List FindSceneObject(string _SceneName)where T:UnityEngine.Component {
        List objectsInScene = new List();
        foreach (var go in Resources.FindObjectsOfTypeAll())
        {
            if (go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave)
                continue;
            if (EditorUtility.IsPersistent(go.transform.root.gameObject))// 如果对象位于Scene中,则返回false
                continue;
            if (_SceneName != go.gameObject.scene.name)
                continue;
            Debug.LogFormat("gameObject:{0},scene:{1}", go.gameObject.name, go.gameObject.scene.name);
            objectsInScene.Add(go);
        }
        return objectsInScene;
    }
}

参考:Resources.FindObjectsOfTypeAll

转载于:https://www.cnblogs.com/dream-follower/p/10777532.html

你可能感兴趣的:(Unity 获取场景中所有目标对象(包括不激活的对象))