- Home /
How can I check if a UI button is selected?
How can I check in script if a Button bla = new Button(); is selected? Not clicked on but just selected.
Thanks in advance :)!
Answer by Estecka · Mar 12, 2018 at 05:59 PM
Compare your button's gameObject with EventSystem.current.currentSelectedGameObject
. EventSystem
requires the namespace UnityEngine.EventSystems
Answer by k0fe · Feb 22, 2020 at 11:44 AM
There are a few ways you can do this.
1) You can use EventSystems and subscribe to OnSelect event Documentation Reference
using UnityEngine.EventSystems; // Required when using Event data.
public class ExampleClass : MonoBehaviour, ISelectHandler // required interface for OnSelect
{
//Do this when the selectable UI object is selected.
public void OnSelect(BaseEventData eventData)
{
Debug.Log(this.gameObject.name + " was selected");
}
}
2) You can use EventSystem to compare a gameObject with selected game object
public GameObject ButtonGameObject;
public void Update()
{
// Compare selected gameObject with referenced Button gameObject
if(EventSystem.currentSelectedGameObject == ButtonGameObject)
{
Debug.Log(this.ButtonGameObject.name + " was selected");
}
}
Using gameobject comparison in Update is inefficient and kinda silly if your game is not built on selecting UI buttons only. So your best bet is to mix up first soultion with a comparison idea.
using UnityEngine.EventSystems;
public class ExampleClass : MonoBehaviour, ISelectHandler
{
public void OnSelect(BaseEventData eventData)
{
if(eventData.selectedObject == ButtonGameObject)
{
Debug.Log(this.ButtonGameObject.name + " was selected");
}
}
}
3) Last but not least. You simply listen to OnMouseEnter/OnMouseExit and do your stuff. It's not a 100% way to check if a button was selected, but in most cases it works.
public bool Selected;
void OnMouseEnter()
{
selected = true;
Debug.Log("Button was selected");
}
void OnMouseExit()
{
selected = false;
Debug.Log("Button was deselected");
}
Thank you so much. This worked perfectly. I also implemented the IDeselectHandler
interface to add a behaviour there too.
Answer by BMRX · Mar 11, 2015 at 11:28 PM
Wouldn't this work just fine?
void OnMouseOver(){
Debug.Log("Button Selected");
}
Could also;
void OnMouseEnter(){
Debug.Log("Button Selected");
}
void OnMouseExit(){
Debug.Log("Button Unselected");
}
Bit better than my last answer.
To add on to this, if you were trying to do something with it in game, you could add a bool to it also. so it could be like
public bool selected;
void On$$anonymous$$ouseEnter(){
selected=true;
Debug.Log("Button Selected");
}
void On$$anonymous$$ouseExit(){
selected=false;
Debug.Log("Button Unselected");
}
That way you can set off certain events if the bool is true or false
I must add, this does not answer the question properly. What if this is intended for controller support? or a mouse-free gameplay?
using UnityEngine.EventSystems;
public class ExampleClass : $$anonymous$$onoBehaviour, ISelectHandler// required interface when using the OnSelect method.
{
//Do this when the selectable UI object is selected.
public void OnSelect(BaseEventData eventData)
{
Debug.Log(this.gameObject.name + " was selected");
}
}
Answer by freakrho · Oct 16, 2019 at 02:33 PM
Don't know if this is exactly what you wanted but it's what I wanted when I came across your question.
The class Selectable has the property currentSelectionState that you can check to see what state it's on, so to check if it's selected you would check
currentSelectionState == SelectionState.Selected
(Button, Toggle, Slider, etc. all inherit from Selectable)
Are you using an older version of Unity? It looks like the latest version(s) no longer expose this property
I believe the property is protected, so you need to make a new class that inherits from what you need (ie: Button).
Your answer
