I'm new in Unity and creating a game. So I have a class PicManager in which I have to load my images(sprites) from assets into a List and then use them in my game. What is the proper way to load multiple images from a folder in Assets?

I know, that I can use Sprite[] sprites = Resources.LoadAll<Sprite>(texture.name); But this article of Unity says not to use Resource folder.

  • 1
    \$\begingroup\$ May I ask why you want to load all images at the same time? You can simply do "public Sprite mySprite;" for each image you want to use, and drag and drop from the inspector? If you want multiple images in an array (to iterate through them for example) you can use public Sprite[] mySprites; and drag and drop from the inspector. \$\endgroup\$ – TomTsagk Oct 8 '18 at 14:28
  • \$\begingroup\$ Yeap, I can do by your way, but: I have a class PicManager, which is not inherited from 'MonoBehaviour', it's just a general C# class (may be its very wrong, I suppose, but for me it's conveniently). For me it seems more flexible to load sprites dynamically. Idk yet if this approach is correct or not \$\endgroup\$ – Alexander Oct 8 '18 at 15:07
  • \$\begingroup\$ It's a nice approach if you are using a custom engine, or you are using no engine at all, when Unity can take care of loading the assets for you, why would you not take advantage of that? It's one of the reasons one would use a game engine: focus on the game itself. \$\endgroup\$ – TomTsagk Oct 8 '18 at 15:28

The reason why the article tells you to not use the Resources folder is exactly because it leads to the anti-pattern you want to create here: Loading multiple images from a folder in Assets before the engine decided that it acutally needs them. So if you insist on doing this, then all that advise is invalid and you can use the method described in the question.

An alternative method which might replicate your architecture in a more Unity-friendly way would be:

  • Turn your PicManager into a MonoBehaviour
  • Make Sprite[] sprites a public variable if it isn't already
  • Add a new game object with that behaviour
  • Add the sprites you need to the game object through the inspector.

But there is just one scenario I could think of where this would make sense: If you have game objects which frequently change their appearance during the game. In that case you could store all the available appearances in the pic manager. When the appearance of a type of game object does not change, then it would be better to assign the sprite directly to the game object and create a prefab for each type of game object which you then instantiate whenever you need one.

| improve this answer | |
  • \$\begingroup\$ +1, if you directly use the Sprite class to give a sprite to an object, Unity will decide when is the best way to load the asset for you, so it won't start loading assets that it's not going to use any time soon. \$\endgroup\$ – TomTsagk Oct 8 '18 at 15:49

If you must load from assets without using Resources.Load, I'd suggest checking out unity's scriptable objects [ScriptableObjects] you save them as assets and can create a list of those objects.

But I also agree with Tom, use the engine. You can always have another script that inherits from monobehavior and implements PicManager if abstraction is what you're going for.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.