Search Unity

Change UI Image source with script

Discussion in 'UGUI & TextMesh Pro' started by Dinrae, Mar 12, 2015.

  1. Dinrae

    Dinrae

    Joined:
    Dec 19, 2012
    Posts:
    29
    Hello,

    How do you change image source with script?
    Read a lot about it, with zillions of users solutions which never work (SpriteRenderer, Sprite, ...).
    What i want to do is Change Image Source, this Image Source is 1024x1024 texture in Resource folder, containing multiple Sprite defined with Unity Editor.
    I can easily change this sprite using Unity Editor (Like everyone), but i don't get how i can change this sprite at runtime ...

    This script, return errors
    Code (CSharp):
    1.     public Image CharacterPortrait;
    2.  
    3.     void Start ()
    4.     {
    5.         CharacterPortrait = GetComponent<Image>();
    6.     }
    7.  
    8.     // Use this for initialization
    9.     void Update ()
    10.     {
    11.         CharacterPortrait.sprite = Resources.Load<Sprite>("Yolo") as Sprite;
    12.     }
    13.  
    This script return zero errors but dont work ...
    Code (CSharp):
    1.     public Image CharacterPortrait;
    2.  
    3.     void Start ()
    4.     {
    5.         CharacterPortrait = GetComponent<Image>();
    6.     }
    7.  
    8.     // Use this for initialization
    9.     void Update ()
    10.     {
    11.         CharacterPortrait.sprite = Resources.Load<UnityEngine.Sprite>("Yolo");
    12. }
    13.  
    Thanks in advance :p
     
  2. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,688
    If you check in the MenuOptions script for the UI source, they do this practice in several places. The code does seem very similar to what you are using above:
    https://bitbucket.org/Unity-Technol...7095b/UnityEditor.UI/UI/MenuOptions.cs?at=4.6
    You should be able to assign the sprite in Start, however using update might cause issue. Another option is to try setting it in LateUpdate instead of Update as the UI doesn't get updated till then.
     
  4. Dinrae

    Dinrae

    Joined:
    Dec 19, 2012
    Posts:
    29
    Thanks for your answers.

    This is the function i use to load Sprite from Resources folder.
    Code (CSharp):
    1.     void LoadSprite (string GetTextureName, string GetSpriteName)
    2.     {
    3.         // Load Texture containing sprites
    4.         Sprite[] textures = Resources.LoadAll<Sprite>(GetTextureName);
    5.         string[] names = new string[textures.Length];
    6.        
    7.         for(int ii=0; ii< names.Length; ii++) {
    8.             names[ii] = textures[ii].name;
    9.         }
    10.        
    11.         // Load sprite from previously loaded texture
    12.         Sprite sprite = textures[Array.IndexOf(names, GetSpriteName)];
    13.        
    14.         CharacterPortrait.sprite = sprite;
    15.     }
    Once i figured Sprite in Unity are like tile in a tilesheet, but not really Image, it was pretty easy to figure it out.
    I'm sure there are more elegant ways to do it, but with my few months experience of coding, it's the only solution i can think of.
     
  5. danielruizvega896

    danielruizvega896

    Joined:
    Jun 15, 2018
    Posts:
    1
    and how do i reference a multiple sprite?
    i have no idea...