返回介绍

Resources.FindObjectsOfTypeAll 查找该类型的所有对象

发布于 2019-12-18 15:38:27 字数 10974 浏览 995 评论 0 收藏 0

JavaScript => public static function FindObjectsOfTypeAll(type: Type): Object[];
C# => public static Object[] FindObjectsOfTypeAll(Type type);

Parameters 参数

typeType of the class to match while searching.\\匹配搜索时类的类型

Returns 返回值

Object[] An array of objects whose class is type or is derived from type.

返回一个对象的类型或从type派生的数组。

Description 描述

Returns a list of all objects of Type type.

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

This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal stuff, therefore please be extra careful the way you handle the returned objects. Contrary to Object.FindObjectsOfType this function will also list disabled objects.

这个函数可以返回加载的Unity物体的任意类型,包含游戏对象、预制体、材质、网格、纹理等等。它也会列出内部的东西,因此,请格外小心处理返回的对象的方式。相反Object.FindObjectsOfType这个函数也会列出禁用的对象。

Please note that this function is very slow and is not recommended to be used every frame.

请注意这个函数非常慢,不推荐在每帧中使用。

JavaScript:

	// 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.
 
	function OnGUI () {
		GUILayout.Label("All " + Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object)).Length);
		GUILayout.Label("Textures " + Resources.FindObjectsOfTypeAll(typeof(Texture)).Length);
		GUILayout.Label("AudioClips " + Resources.FindObjectsOfTypeAll(typeof(AudioClip)).Length);
		GUILayout.Label("Meshes " + Resources.FindObjectsOfTypeAll(typeof(Mesh)).Length);
		GUILayout.Label("Materials " + Resources.FindObjectsOfTypeAll(typeof(Material)).Length);
		GUILayout.Label("GameObjects " + Resources.FindObjectsOfTypeAll(typeof(GameObject)).Length);
		GUILayout.Label("Components " + Resources.FindObjectsOfTypeAll(typeof(Component)).Length);
	}
using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void OnGUI() {
        GUILayout.Label("All " + Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object)).Length);
        GUILayout.Label("Textures " + Resources.FindObjectsOfTypeAll(typeof(Texture)).Length);
        GUILayout.Label("AudioClips " + Resources.FindObjectsOfTypeAll(typeof(AudioClip)).Length);
        GUILayout.Label("Meshes " + Resources.FindObjectsOfTypeAll(typeof(Mesh)).Length);
        GUILayout.Label("Materials " + Resources.FindObjectsOfTypeAll(typeof(Material)).Length);
        GUILayout.Label("GameObjects " + Resources.FindObjectsOfTypeAll(typeof(GameObject)).Length);
        GUILayout.Label("Components " + Resources.FindObjectsOfTypeAll(typeof(Component)).Length);
    }
}
import System.Collections.Generic;
 
// This script finds all the objects in scene, excluding prefabs:
function GetAllObjectsInScene(): List.<GameObject> {
    var objectsInScene: List.<GameObject> = new List.<GameObject>();
 
    for (var go: GameObject 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;
}

C#:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
 
public class ExampleClass  : MonoBehaviour {
 
 
	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;
	}
}

Resources.FindObjectsOfTypeAll 查找该类型的所有对象

JavaScript => public static function FindObjectsOfTypeAll(): T[];
C# => public static T[] FindObjectsOfTypeAll();

Description 描述

Returns a list of all objects of Type T.

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

This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal stuff, therefore please be extra careful the way you handle the returned objects. Contrary to Object.FindObjectsOfType this function will also list disabled objects.

这个函数可以返回加载的Unity物体的任意类型,包含游戏对象、预制体、材质、网格、纹理等等。它也会列出内部的东西,因此,请格外小心处理返回的对象的方式。相反Object.FindObjectsOfType这个函数也会列出禁用的对象。

Please note that this function is very slow and is not recommended to be used every frame.

请注意这个函数非常慢,不推荐在每帧中使用。

JavaScript:

	// 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.
 
	function OnGUI () {
		GUILayout.Label("All " + Resources.FindObjectsOfTypeAll.<UnityEngine.Object>().Length);
		GUILayout.Label("Textures " + Resources.FindObjectsOfTypeAll.<Texture>().Length);
		GUILayout.Label("AudioClips " + Resources.FindObjectsOfTypeAll.<AudioClip>().Length);
		GUILayout.Label("Meshes " + Resources.FindObjectsOfTypeAll.<Mesh>().Length);
		GUILayout.Label("Materials " + Resources.FindObjectsOfTypeAll.<Material>().Length);
		GUILayout.Label("GameObjects " + Resources.FindObjectsOfTypeAll.<GameObject>().Length);
		GUILayout.Label("Components " + Resources.FindObjectsOfTypeAll.<Component>().Length);
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void OnGUI() {
        GUILayout.Label("All " + Resources.FindObjectsOfTypeAll<Object>().Length);
        GUILayout.Label("Textures " + Resources.FindObjectsOfTypeAll<Texture>().Length);
        GUILayout.Label("AudioClips " + Resources.FindObjectsOfTypeAll<AudioClip>().Length);
        GUILayout.Label("Meshes " + Resources.FindObjectsOfTypeAll<Mesh>().Length);
        GUILayout.Label("Materials " + Resources.FindObjectsOfTypeAll<Material>().Length);
        GUILayout.Label("GameObjects " + Resources.FindObjectsOfTypeAll<GameObject>().Length);
        GUILayout.Label("Components " + Resources.FindObjectsOfTypeAll<Component>().Length);
    }
}

JavaScript:

import System.Collections.Generic;
 
// This script finds all the objects in scene, excluding prefabs:
function GetAllObjectsInScene(): List.<GameObject> {
    var objectsInScene: List.<GameObject> = new List.<GameObject>();
 
    for (var go: GameObject in Resources.FindObjectsOfTypeAll.<GameObject>()) {
        if (go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave)
            continue;
 
        if (!EditorUtility.IsPersistent(go.transform.root.gameObject))
            continue;
 
        objectsInScene.Add(go);
    }
 
    return objectsInScene;
}

C#:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
 
public class ExampleClass  : MonoBehaviour {
 
	List<GameObject> GetAllObjectsInScene()  {
		List<GameObject> objectsInScene = new List<GameObject>();
 
		foreach (GameObject go in Resources.FindObjectsOfTypeAll<GameObject>()) {
			if (go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave)
				continue;
 
			if (!EditorUtility.IsPersistent(go.transform.root.gameObject))
				continue;
 
			objectsInScene.Add(go);
		}
 
		return objectsInScene;
	}
}

Resources

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文