Resources.FindObjectsOfTypeAll

切换到手册
public static Object[] FindObjectsOfTypeAll (Type type);

参数

type搜索时要匹配的类的类型。

返回

Object[] 对象数组,其类为 type 或派生自 type

描述

返回所有类型为 type 的对象的列表。

此函数可以返回已加载的任何类型的 Unity 对象,包括游戏对象、预制件、材质、网格、纹理等。此函数还将列出内部对象。因此请小心处理返回的对象。 与 Object.FindObjectsOfType 不同的是,此函数还将列出禁用的对象。

注意:该函数的运行速度非常缓慢,不建议在每帧中使用。

using UnityEngine;
using System.Collections;

public class ExampleScript : MonoBehaviour { // This script displays the number of allocated Unity objects by type. // This is useful for finding leaks. Knowing the type of object // (mesh, texture, sound clip, game object) that is getting leaked is // the first step. You could then print the names of all leaked assets of that type.

void Start() { print("All " + Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object)).Length); print("Textures " + Resources.FindObjectsOfTypeAll(typeof(Texture)).Length); print("AudioClips " + Resources.FindObjectsOfTypeAll(typeof(AudioClip)).Length); print("Meshes " + Resources.FindObjectsOfTypeAll(typeof(Mesh)).Length); print("Materials " + Resources.FindObjectsOfTypeAll(typeof(Material)).Length); print("GameObjects " + Resources.FindObjectsOfTypeAll(typeof(GameObject)).Length); print("Components " + Resources.FindObjectsOfTypeAll(typeof(Component)).Length); } }
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class ExampleScript : MonoBehaviour { // This script finds all the objects in Scene, excluding Prefabs:

List<GameObject> GetAllObjectsInScene() { List<GameObject> objectsInScene = new List<GameObject>();

foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[]) { if (go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave) continue;

if (!EditorUtility.IsPersistent(go.transform.root.gameObject)) continue;

objectsInScene.Add(go); }

return objectsInScene; } }

public static T[] FindObjectsOfTypeAll ();

描述

返回所有类型为 T 的对象的列表。

该函数可以返回任何类型的已加载的 Unity 对象,包括游戏对象、预制件、材质、网格、纹理等。此外,它还会列出内部内容,因此在处理返回的对象时要格外 小心。 与 Object.FindObjectsOfType 相反,该函数还会列出禁用的对象。

请注意,该函数的运行速度非常缓慢,不建议在每帧中使用。

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

public class ExampleScript : MonoBehaviour { // This script displays the number of allocated Unity objects by type. // This is useful for finding leaks. Knowing the type of object // (mesh, texture, sound clip, game object) that is getting leaked is // the first step. You could then print the names of all leaked assets of that type.

void Start() { print("All " + Resources.FindObjectsOfTypeAll<UnityEngine.Object>().Length); print("Textures " + Resources.FindObjectsOfTypeAll<Texture>().Length); print("AudioClips " + Resources.FindObjectsOfTypeAll<AudioClip>().Length); print("Meshes " + Resources.FindObjectsOfTypeAll<Mesh>().Length); print("Materials " + Resources.FindObjectsOfTypeAll<Material>().Length); print("GameObjects " + Resources.FindObjectsOfTypeAll<GameObject>().Length); print("Components " + Resources.FindObjectsOfTypeAll<Component>().Length); } }
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class ExampleScript : MonoBehaviour { // This script finds all the objects in Scene, excluding Prefabs:

List<GameObject> GetAllObjectsInScene() { List<GameObject> objectsInScene = new List<GameObject>();

foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[]) { if (go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave) continue;

if (!EditorUtility.IsPersistent(go.transform.root.gameObject)) continue;

objectsInScene.Add(go); }

return objectsInScene; } }

显示存储的游戏对象名称:

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

public class ExampleScript : MonoBehaviour { // Find all of the GameObjects in the Resources folder // Add a GameObject called "Player" so at least 1 GameObject will be stored. // However, there are nearly twenty GameObjects.

void Start() { string s = ""; GameObject[] gameObjects; int count = 0;

GameObject player = new GameObject("Player"); gameObjects = Resources.FindObjectsOfTypeAll<GameObject>();

foreach (GameObject go in gameObjects) { s = s + go.name;

if ((count == 4)) { s = s + "\n"; count = 0; } else { s = s + ' '; count++; } }

Debug.Log(s);

// Prints: //Player SceneLight SceneLight SceneLight SceneCamera //GameObject InternalIdentityTransform torus cube cylinder //sphere PreviewMaterials Cylinder HandlesGO Cone //Cube Sphere Directional Light Main Camera } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961