两种寻找scene所有物体的方法

本文介绍两种在Unity中获取场景内所有游戏对象的方法,包括活动和非活动对象。第一种方法递归地遍历所有根对象及其子对象。第二种方法使用资源查找及选择工具,筛选出特定场景中的游戏对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 //1
    void GetRoot()
    {
        List<GameObject> allGOs = new List<GameObject>();
        GameObject[] rootGOs = gameObject.scene.GetRootGameObjects();

        for (int i = 0; i < rootGOs.Length; i++)
            GetAllChildren(rootGOs[i].transform, allGOs);
        all = allGOs;
    }
    void GetAllChildren(Transform current, List<GameObject> arrayToFill)
    {
        arrayToFill.Add(current.gameObject);

        for (int i = 0; i < current.childCount; i++)
            GetAllChildren(current.GetChild(i), arrayToFill);
    }
    //2
    private List<GameObject> GetAllSceneObjectsWithInactive()
    {
        var allTransforms = Resources.FindObjectsOfTypeAll(typeof(Transform));
        var previousSelection = Selection.objects;
        Selection.objects = allTransforms.Cast<Transform>()
            .Where(x => x != null)
            .Select(x => x.gameObject)
            .Where(x => x != null && !x.activeInHierarchy)
            .Cast<UnityEngine.Object>().ToArray();

        var selectedTransforms = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);
        Selection.objects = previousSelection;

        return selectedTransforms.Select(tr => tr.gameObject).ToList();
    }

写成了一个扩展方法

using UnityEngine;
using System.Collections;
using System.Linq;
using UnityEngine.SceneManagement;
using System.Collections.Generic;

public static class Extool
{
    //1
   public static void GetGO(this GameObject self,ref GameObject go, string name)
    {
        GameObject[] rootGOs = self.scene.GetRootGameObjects();

        for (int i = 0; i < rootGOs.Length; i++)
            GetAllChildren(rootGOs[i].transform, ref go, name);
    }
    public static void GetAllChildren(Transform current, ref GameObject go, string name)
    {
        if (current.name == name)
        {
            go = current.gameObject;
            return;
        }
        for (int i = 0; i < current.childCount; i++)
            GetAllChildren(current.GetChild(i), ref go, name);
    }
    //2
    public static void GetAllSceneObjectsWithInactive(this MonoBehaviour self,ref GameObject go,string name)
    {
        var allTransforms = Resources.FindObjectsOfTypeAll(typeof(Transform));
        List<GameObject> allGOs = new List<GameObject>();
        allGOs = allTransforms.Cast<Transform>()
            .Where(x => x != null)
            .Select(x => x.gameObject)
            //.Where(x => x != null && !x.activeInHierarchy)
            .Where(x => x != null && x.scene == SceneManager.GetActiveScene())
            .Cast<UnityEngine.GameObject>().ToList();

        for (int i = 0; i < allGOs.Count; i++)
        {
            if (allGOs[i].name==name)
            {
                go = allGOs[i];
                break;
            }
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值