LoadAll
Loads all assets in a folder or file at path in a Resources folder.
Read time 3 minutesLast updated 4 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
LoadAll(string, Type)
Loads all assets in a folder or file at in a Resources folder.
pathpublic static Object[] LoadAll(string path, Type systemTypeInstance)
Parameters
Returns
Type | Description |
|---|---|
| Object[] | An array of all the assets found at the given path. |
Remarks
If refers to a folder, all assets in the folder will be returned. If refers to a file, only that asset will be returned. The is relative to any Resources folder inside the Assets folder of your project.
pathpathpathNote: All asset names and paths in Unity use forward slashes. Paths using backslashes will not work.
Examples
// 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; } }}
LoadAll(string)
Loads all assets in a folder or file at in a Resources folder.
pathpublic static Object[] LoadAll(string path)
Parameters
Pathname of the target folder. When using the empty string (i.e., ""), the function will load the entire contents of the Resources folder.
Returns
Type | Description |
|---|---|
| Object[] | An array of all the assets found at the given path. |
Remarks
If refers to a folder, all assets in the folder will be returned. If refers to a file, only that asset will be returned. The is relative to any Resources folder inside the Assets folder of your project.
pathpathpathNote: All asset names and paths in Unity use forward slashes. Paths using backslashes will not work.
Examples
// 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; } }}
LoadAll<T>(string)
Loads all assets in a folder or file at in a Resources folder.
pathpublic static T[] LoadAll<T>(string path) where T : Object
Parameters
Pathname of the target folder. When using the empty string (i.e., ""), the function will load the entire contents of the Resources folder.
Returns
Type | Description |
|---|---|
| <T>[] | An array of all the assets of type |
Remarks
If refers to a folder, all assets in the folder will be returned. If refers to a file, only that asset will be returned. Only objects of type will be returned. The is relative to any Resources folder inside the Assets folder of your project.
pathpathTpathThe script example below shows how Resources.LoadAll can be used with Linq.
Examples
// 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); }}