Search Unity

Resource.Load Sprite from script problem?

Discussion in 'Scripting' started by Lars-Kristian, Nov 21, 2014.

  1. Lars-Kristian

    Lars-Kristian

    Joined:
    Jun 26, 2013
    Posts:
    64
    Hi dear forum users. I am trying to load a sprite from script using Resource.Load, but it does not work.

    I have used this function before on other assets but I am not able to make it work with sprites. Does anyone know what I am doing wrong? I am using 4.6 RC3 btw.

    Here are some of my attempts.
    Code (CSharp):
    1. Sprite[] tmp = Resources.LoadAll("Icons/Icons", typeof(Sprite)) as Sprite[];
    2. Sprite starOutline = tmp[0];
    Code (CSharp):
    1. Sprite starSolid = Resources.Load("Icons/Icons/Star_Solid", typeof(Sprite)) as Sprite;
    This is my folder structure.
    Icons.png
     
  2. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    I'm not sure, but could it be that you have to do it in this way:
    Code (CSharp):
    1. Sprite starSolid = Instantiate(Resources.Load("Icons/Icons/Star_Solid", typeof(Sprite))) as Sprite;
     
  3. Lars-Kristian

    Lars-Kristian

    Joined:
    Jun 26, 2013
    Posts:
    64
    I managed to solve the problem this way, but I am not completely happy with it. :/

    Code (CSharp):
    1. Object[] tmp = Resources.LoadAll("Icons/Icons", typeof(Sprite));
    2. for(int i = 0; i < tmp.Length; i++)
    3. {
    4.     if(tmp[i].name == "Star_Solid")
    5.     {
    6.         starSolid = tmp[i] as Sprite;
    7.     }
    8.     else if(tmp[i].name == "Star_Hollow")
    9.     {
    10.         starOutline = tmp[i] as Sprite;
    11.     }
    12. }
    13.  
     
  4. RSG

    RSG

    Joined:
    Feb 20, 2013
    Posts:
    93
    Check the path that you are using to make sure that your icons are in the correct place, also if you only need access to a couple of sprites, then store them as references in your script:

    Code (CSharp):
    1.  
    2. [SerializeField]
    3. public Sprite Star_Solid;
    4. [SerializeField]
    5. public Sprite Star_Hollow;
     
  5. Lars-Kristian

    Lars-Kristian

    Joined:
    Jun 26, 2013
    Posts:
    64
    Thanks for the info, but I already do this. The behavior I want, is to load the sprites every time I initiate this script so if I want to change the sprites I only need to replace them in the folder.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It would be better just to use a public variable and link them in the inspector. Sprites in Resources will not be auto-atlased if you have Unity Pro.

    --Eric
     
  7. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Also, Unity is unable to strip unused assets when building out projects if everything is in your Resources folder. Resources is not a good solution for your use-case - particularly given that if you just replace the sprites outside the unity folder (just overwrite them with same name), Unity will automagically refresh and reload.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    True, plus I would be leery of using LoadAll since there's no defined order in the returned array that I'm aware of.

    --Eric
     
  9. Lars-Kristian

    Lars-Kristian

    Joined:
    Jun 26, 2013
    Posts:
    64
    This is what I am trying to avoid, because I find it time consuming if you change out every texture/sprite used by the UI system.

    Can I read more about this in the manual somewhere?


    Yeah I agree but I do not always remembering doing this.


    I actually might end up replacing every sprite and texture with meshes. Reason: Lower file size and sharper edges.
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Not sure what you mean; if you replace a sprite's texture you don't have to do anything, since Unity picks up the change automatically.

    I'm not sure it's really covered properly in the manual (which should be fixed). You can find various places where it's discussed such as here.

    --Eric
     
  11. Lars-Kristian

    Lars-Kristian

    Joined:
    Jun 26, 2013
    Posts:
    64
    Thanks that was very informative.

    I am currently working on personal project where one of my main goals is to optimize workflow. In the past I have made asset references by using public variables and setting them via the inspector. This works fine most of the time, but when I move around on prefabs and perhaps doing things I should not do(I am not perfect) asset references get lost. A lot of time is used to set all the references again, specialty if this happens multiple times.

    I believe setting references via script would solve this problem. I made this thread, because loading sprites did not behave the way I expected.

    Are there other ways to load assets from scripts?
     
  12. mohsalim

    mohsalim

    Joined:
    Jan 4, 2015
    Posts:
    2
    Was this ever resolved? I had to make a SpriteCache for now, but would be nice to just directly access a sprite resource with just the name.