Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Penca53 · Oct 07, 2017 at 03:55 PM · arrayspritefolder

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.

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

2 Replies

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

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:

Assign several images at once

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");


Comment
Add comment · Show 1 · 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 Penca53 · Oct 07, 2017 at 06:37 PM 0
Share

Thank you so much! I didn't know that manual procedure... i'll use it!

avatar image
2

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);
 
                 }

Comment
Add comment · Show 1 · 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 Penca53 · Oct 07, 2017 at 06:39 PM 0
Share

Thank you for your technical procedure!

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.

Follow this Question

Answers Answers and Comments

109 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 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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges