- Home /
Question by
Astha_Mishra · Jan 04, 2018 at 05:04 PM ·
unity 5uigameobjectspritesspriterenderer
how to check presence of sprite on a UI button?
I want to check , if sprite is present on the clicked button or not , and if present then take that sprite in a gameobject for accessing it.
Comment
Answer by Hellium · Jan 04, 2018 at 05:09 PM
I haven't tested the following code, but give it a try :
First of all, put this at the top of your file :
using UnityEngine.EventSystems;
Then, in the function called by the onClick
callback of your button :
public void OnButtonClicked()
{
GameObject clickedButtonGameObject = EventSystem.current.currentSelectedGameObject ;
Button button = clickedButtonGameObject.GetComponent<Button>();
Image image = button.targetGraphic as Image;
if( image != null )
{
Sprite sprite = image.sprite ;
Debug.Log( sprite ) ;
}
}