- Home /
How to load all my sprites in my folder and put them in an array.
Hi guys, how can i load all my sprites in a folder and put all of them in an array? For example: i have a folder called "Icons" and i can see it in my project panel in Unity. In this folder there are many sprites. I'd like to store all of them in an array variable called "Icons".
Thanks, Penca.
Answer by Bunny83 · Oct 07, 2017 at 06:11 PM
There are several ways how to solve this problem. However the recommended one would be to just assign the sprites to the array in the inspector.
Here's an example how you can assign several textures at once. Imagine a script like this:
using UnityEngine;
public class SomeScript : MonoBehaviour
{
public Texture2D[] textures;
}
You just would select the object where your script is attached to. You would lock the inspector on that object, so you can select your images and the inspector still shows the object with the script. Then just select all the images at once (using SHIFT or CTRL like usual) and drag them onto the array. The items will be added at the end of the array.
See this animated GIF:
Don't forget to unlock the inspector when done.
This is the preferred way. Unity does only include assets in a build if they are referenced from a scene (directly or indirectly). However there's one alternative if you have many images which all need to be part of the game and you may add / remove assets frequently:
You have to create a folder inside your Assets folder named "Resources". Inside that Resources folder you can create subfolders if you like. Put all assets which should always be included in your game in that folder or a subfolder.
Now you can use Resources.Load to load an image by name or use Resources.LoadAll to load all assets in the specified subfolder.
Note that the path name you have to specify in Load or LoadAll is always relative to the Resources folder. Also you always have to use forward slashes when you have subfolders. Finally you have to omit the file extension. So just use the name. Something like:
Texture2D tex1 = Resources.Load<Texture2D>("Image1");
Texture2D tex2 = Resources.Load<Texture2D>("SubFolder1/SubFolder1/Image1");
Thank you so much! I didn't know that manual procedure... i'll use it!
Answer by YoucefB · Oct 07, 2017 at 06:24 PM
Move your folder (Icons) inside a folder named Resources , then you can call Resources.LoadAll
to load your icons, then fill the array.
Example
void Start(){
LoadIcons();
}
public Sprite[] Icons; // icons array
void LoadIcons(){
object[] loadedIcons = Resources.LoadAll ("Icons",typeof(Sprite)) ;
Icons = new Sprite[loadedIcons.Length];
//this
for(int x = 0; x< loadedIcons.Length;x++){
Icons [x] = (Sprite)loadedIcons [x];
}
//or this
//loadedIcons.CopyTo (Icons,0);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612140505im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to add a Sprite Rollover Image using a Button Array 0 Answers
How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer
How can I put the single pieces of a sliced Sprite into an array? 0 Answers
Uploaded sprite can't set bones and poses 1 Answer
Convert String to Sprite 1 Answer