Search Unity

Load Resource ( Sprite ) at Runtime

Discussion in '2D' started by okkappa, Jun 22, 2016.

  1. okkappa

    okkappa

    Joined:
    Apr 29, 2016
    Posts:
    5
    Hi everyone,

    This is my first post in this forum and english is not my native language, so don't be rude with me.
    Anyway, this is the code related to the issue :


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class UI_Planets_script : MonoBehaviour {
    6.  
    7.    
    8.     public GameObject icona;
    9.  
    10. // ...
    11.  
    12.     public void LookAt ( string name ){
    13.  
    14.         switch (name)
    15.             {
    16.             case ("1_Mercury"):
    17.            
    18.             icona.GetComponent<Image> ().sprite = (Sprite) Resources.Load("Textures/Icon_2", typeof(Sprite)) ;
    19.             break;
    20.  
    21.             }
    22.    
    23.     }
    24.  
    25. }
    26.  
    Im trying to load the Icon_2 into the " source image " of the Image component of the object linked to "icona" . But all i got at runtime is a white box.

    note. Icon_2 is the second element of a bigger sprite sliced
     
  2. Gousenfire

    Gousenfire

    Joined:
    Feb 18, 2014
    Posts:
    30
    icona.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Icon_2") ;

    try this out.

    And the image "Icon_2" must be in the folder "Assets/Resources/Textures" it's very important!!!!
     
    BinaryRavine and adzina like this.
  3. okkappa

    okkappa

    Joined:
    Apr 29, 2016
    Posts:
    5
    Still not working.
    NDR. I've already tried to set the path to both to "Textures/Icon_2" and " Resources/Textures/Icon_2", with an extra "/" at beginning. I've also double checked the syntax of the path and it should be fine
     
  4. Gousenfire

    Gousenfire

    Joined:
    Feb 18, 2014
    Posts:
    30
    Can you print your project tab in Unity pls
     
  5. okkappa

    okkappa

    Joined:
    Apr 29, 2016
    Posts:
    5
    upload_2016-6-22_20-49-19.png

    This is the path to my "Icon_2" ( aka AllPlanetesIcons_2) resource . ( I've posted a path example, my true one is "Textures/Planet_icon/AllPlanetsIcons_2")
     

    Attached Files:

  6. Gousenfire

    Gousenfire

    Joined:
    Feb 18, 2014
    Posts:
    30
    Oo ok. Got it!
    Code (CSharp):
    1. int n = 0; // change the 0 for the index of the sprite that you want in the spritesheet
    2. Sprite[] array = Resources.LoadAll<Sprite>("Textures/Icon_2") ;
    3. icona.GetComponent<Image>().sprite = array[n];
    4.            
    The problem is that you have multiple sprites so you have to load them all and then select what you want.
     
  7. okkappa

    okkappa

    Joined:
    Apr 29, 2016
    Posts:
    5
    Working now, thanks man, i would have never solved this alone in a reasonable amount of time :)

    Final code :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class UI_Planets_script : MonoBehaviour {
    6.  
    7.     public GameObject icona;
    8.  
    9.     private Sprite[] SpriteArray;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.         SpriteArray =  Resources.LoadAll<Sprite>("Textures/Planet_icon") ;
    15.    
    16.     }
    17.  
    18.    //...
    19.  
    20.     public void LookAt ( string name ){
    21.  
    22.         switch (name)
    23.             {
    24.             case ("1_Mercury"):
    25.             icona.GetComponent<Image>().sprite = SpriteArray[0];
    26.             break;
    27.    
    28.             //..
    29.             }
    30.  
    31.     }
     
    Last edited: Jun 24, 2016
  8. Gousenfire

    Gousenfire

    Joined:
    Feb 18, 2014
    Posts:
    30
    You are welcome
     
  9. Will_Croxford

    Will_Croxford

    Joined:
    Mar 7, 2018
    Posts:
    5
    Hi, I'm just looking at this, in case anyone else thinks useful, as beginning to mount up the total size of images in my scene, so I see you need to have folder exact name "Resources" under the Assets root for runtime loading to work. This is like "Streaming Assets" folder for text file data and so on. In this or similar cases, if you think too expensive (slow) to load all images in one folder at one time during runtime, I guess you could just split them into different folders under the Resources folder, only 1 sprite in each folder. Then
    Code (CSharp):
    1. Resources.Load<Sprite>(filepath relative to Resources folder)
    should be fine.
     
  10. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Wish I had seen this before I created a different mechanism for loading sprites.


    Edit... that didn't work. ;)