- Home /
Get the sprite name attached to Source Image of UI Image
I have only one small simple question, for which I didn't find answer while searching Google. How do I get the name of the sprite which I have added to a Source Image of UI Image component? and also get the name of the sprite added to the 2d sprite game object in a 2d game.? Please someone help me..
Answer by Mako-Infused · Jul 30, 2015 at 06:31 PM
That should be simple, unless I'm missing something:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SomeScript : MonoBehaviour
{
public Image image;
void Awake()
{
string name = image.sprite.name;
// DO SOMETHING
}
}
Answer by tkaravou · Aug 30, 2018 at 07:19 PM
Putting this here in case someone is wondering. Here's the actual way.
GetComponent<SpriteRender>().sprite.name;
Little different for me. Running 2017.
GetComponent<UnityEngine.UI.Image> ().sprite.name;
Answer by Ristovski11 · Jul 15, 2016 at 12:34 PM
I dont think that is possible. I had similar problem when I wanted to compare 2 sprites. The closest you can define a sprite is by texture.name and textureRect.