• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by eclaire211 · Apr 16, 2016 at 07:40 PM · load asset

Is it possible to use Resources.Load() to load an entire subfolder?

I'm a unity newbie, so I'm sorry if this really easy. I'm just having trouble finding examples online of this.

I have a subfolder within the Resources folder. This subfolder contains several images that I would like to load. The number of images within this folder will not always be the same, so I don't just want to hard code loading in each image separately, because I don't know how many there will be. Is it possible to load the entire subfolder at once and save it to an array? I tried the below code and it throws errors. Any suggestions?

The images are in Assets/Resources/Images/

Texture[] images = Resources.Load("Images/", typeof(Texture)) as Texture;

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TBruce · Apr 16, 2016 at 08:00 PM

@ eclaire211

Try

 Texture[] images = Resources.LoadAll("Images", typeof(Texture));

or if you would rather use a List

 using System.Collections.Generic;
 List<Texture> images = new List<Texture>(Resources.LoadAll("Images", typeof(Texture)));

Note, depending on your needs, you may want to use Texture2D instead of Texture.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eclaire211 · Apr 16, 2016 at 08:23 PM 0
Share

@$$anonymous$$avina Thank you for your answer! However, I'm still having an issue. I just tried:

Texture[] images = Resources.LoadAll("Images", typeof(Texture)) as Texture[]; (I added in the "as Texture[]" because it was throwing errors without it)

When I try to access the images array, I get a NullReferenceException. Do you know why my array is null?

avatar image TBruce eclaire211 · Apr 16, 2016 at 09:02 PM 0
Share

@eclaire211

$$anonymous$$y bad, do this ins$$anonymous$$d

 Object[] objects = Resources.LoadAll("Textures", typeof(Texture));
 Texture[] images;
 if (objects != null)
 {
     images = new Texture[objects.Length];
     for (int i = 0; i < objects.Length; i++)
     {
         images[i] = (Texture)objects[i];
     }
     Debug.Log("images.Length " + images.Length);
 }

or if using a List

 using System.Collections.Generic;
 
 Object[] objects = Resources.LoadAll("Textures", typeof(Texture));
 List<Texture> images = new List<Texture>();
 if (objects != null)
 {
     images = new Texture[objects.Length];
     for (int i = 0; i < objects.Length; i++)
     {
         images.Add((Texture)objects[i]);
     }
     Debug.Log("images.Length " + images.Length);
 }
 
avatar image eclaire211 TBruce · Apr 16, 2016 at 10:46 PM 0
Share

@$$anonymous$$avina Thank you so much! This fixed the problem! :)

Show more comments
avatar image RodrigoSeVeN eclaire211 · Mar 13, 2018 at 04:19 PM 0
Share

You can also do it this way (I haven't tried with lists or Texture specifically):

 using System.Linq;
 Texture[] images = Resources.LoadAll("Images", typeof(Texture)).Cast<Texture>().ToArray();

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get Texture By Opening File Browser Runtime 0 Answers

Is there any way to create and load a shader source file at runtime? 1 Answer

Creating a very Long Level Select Scene 0 Answers

How to load an asset and change it's parameters. 0 Answers

how to change asset set to script ? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges