码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-04-27 09:51:33      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:log   table   eal   ble   deb   目标   format   public   one   

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

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

    List<T> FindSceneObject<T>(string _SceneName)where T:UnityEngine.Component {
        List<T> objectsInScene = new List<T>();
        foreach (var go in Resources.FindObjectsOfTypeAll<T>())
        {
            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

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

标签:log   table   eal   ble   deb   目标   format   public   one   

原文地址:https://www.cnblogs.com/dream-follower/p/10777532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!