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): using UnityEngine; using System.Collections; using UnityEngine.UI; public class UI_Planets_script : MonoBehaviour { public GameObject icona; // ... public void LookAt ( string name ){ switch (name) { case ("1_Mercury"): icona.GetComponent<Image> ().sprite = (Sprite) Resources.Load("Textures/Icon_2", typeof(Sprite)) ; break; } } } 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
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!!!!
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
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")
Oo ok. Got it! Code (CSharp): int n = 0; // change the 0 for the index of the sprite that you want in the spritesheet Sprite[] array = Resources.LoadAll<Sprite>("Textures/Icon_2") ; icona.GetComponent<Image>().sprite = array[n]; The problem is that you have multiple sprites so you have to load them all and then select what you want.
Working now, thanks man, i would have never solved this alone in a reasonable amount of time Final code : Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.UI; public class UI_Planets_script : MonoBehaviour { public GameObject icona; private Sprite[] SpriteArray; // Use this for initialization void Start () { SpriteArray = Resources.LoadAll<Sprite>("Textures/Planet_icon") ; } //... public void LookAt ( string name ){ switch (name) { case ("1_Mercury"): icona.GetComponent<Image>().sprite = SpriteArray[0]; break; //.. } }
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): Resources.Load<Sprite>(filepath relative to Resources folder) should be fine.
Wish I had seen this before I created a different mechanism for loading sprites. Edit... that didn't work.