Unity遍历Hierarchy上的所有物体,包括隐藏物体

Unity遍历Hierarchy上的所有物体,包括隐藏物体
代码如下

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

public class MyEditorUtils 
{
    [MenuItem("GameTools/遍历Hierarchy")]
    static void GetAllSceneObjectsWithInactive()
    {
        var allGos = Resources.FindObjectsOfTypeAll(typeof(GameObject));
        var previousSelection = Selection.objects;
        Selection.objects = allGos;
        var selectedTransforms = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);
        Selection.objects = previousSelection;
        foreach(var trans in selectedTransforms)
        {
            Debug.Log(trans.name);
        }
    }
}

Unity遍历Hierarchy上的所有物体,包括隐藏物体_第1张图片
如果想把预设的资源路径打印出来,可以这样

var go = PrefabUtility.GetPrefabParent(trans.gameObject);
string path = AssetDatabase.GetAssetPath(go);
Debug.Log(path);

你可能感兴趣的:(unity3D)