Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Resources.LoadAll

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static method LoadAll(path: string): Object[];
public static Object[] LoadAll(string path);
public static method LoadAll(path: string, systemTypeInstance: Type): Object[];
public static Object[] LoadAll(string path, Type systemTypeInstance);

Parameters

path Pathname of the target folder. When using the empty string (i.e., ""), the function will load the entire contents of the Resources folder.
systemTypeInstance Type filter for objects returned.

Description

Loads all assets in a folder or file at path in a Resources folder.

If path refers to a folder, all assets in the folder will be returned. If path refers to a file, only that asset will be returned. The path is relative to any Resources folder inside the Assets folder of your project.

Note: All asset names and paths in Unity use forward slashes. Paths using backslashes will not work.

#pragma strict

// Loads all assets in the "Resources/Textures" folder // Then picks a random one from the list. // Note: Random.Range in this case returns [low,high) // range, i.e. the high value is not included in the range.

function Start() { var go: GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);

var textures: Object[] = Resources.LoadAll("Textures", Texture2D); var texture: Texture2D = textures[Random.Range(0, textures.Length)] as Texture2D;

go.GetComponent.<Renderer>().material.mainTexture = texture; }
// Loads all assets in the "Resources/Textures" folder
// Then picks a random one from the list.
// Note: Random.Range in this case returns [low,high)
// range, i.e. the high value is not included in the range.
using UnityEngine;

public class ExampleClass : MonoBehaviour { private Object[] textures; private GameObject go;

void Start() { textures = Resources.LoadAll("Textures", typeof(Texture2D));

foreach (var t in textures) { Debug.Log(t.name); }

go = GameObject.CreatePrimitive(PrimitiveType.Cube); }

void OnGUI() { if (GUI.Button(new Rect(10, 70, 150, 30), "Change texture")) { // change texture on cube Texture2D texture = (Texture2D)textures[Random.Range(0, textures.Length)]; go.GetComponent<Renderer>().material.mainTexture = texture; } } }

public static method LoadAll(path: string): T[];
public static T[] LoadAll(string path);

Parameters

path Pathname of the target folder. When using the empty string (i.e., ""), the function will load the entire contents of the Resources folder.

Description

Loads all assets in a folder or file at path in a Resources folder.

If path refers to a folder, all assets in the folder will be returned. If path refers to a file, only that asset will be returned. Only objects of type T will be returned. The path is relative to any Resources folder inside the Assets folder of your project.

The script example below shows how LoadAll can be used with Linq.

#pragma strict
// Loads all assets in the "Resources/Textures" folder
// using Linq.
function Start() {
	var textures: var = Resources.LoadAll("Textures", Texture2D).Cast.<Texture2D>().ToArray();
	for (var t: var in textures)
		Debug.Log(t.name);
}
// Loads all assets in the "Resources/Textures" folder
// using Linq.
using UnityEngine;
using System.Linq;

public class ExampleClass : MonoBehaviour { void Start() { var textures = Resources.LoadAll("Textures", typeof(Texture2D)).Cast<Texture2D>().ToArray(); foreach (var t in textures) Debug.Log(t.name); } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答