Trying to figure out a simple way to know if a UI button is currently highlighted or not, this is for using a gamepad or keyboard, so I can’t use OnPointerEnter or OnMouseOver methods or anything that exclusively uses a mouse.
I’m not proficient in C#.
I’ve been googling around but nothing seems to work so far -
This one only works once the button is selected, not highlighted.
This seems to be the closest thing to what I want, but I can’t seem to make this work, I get the error “No overload method for “IsHighlighted” takes 1 arguments” and I have no idea how to fix it.
did you guys figure it out? I also want to check if it’s highlighted. I’m using unity 2021.2, but my error says The name ‘isHighlighted’ does not exist in the current context.
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class IsSelected : Selectable
{
BaseEventData BaseEvent;
public GameObject dot;
void Update()
{
if (isHighlighted(BaseEvent) == true)
{
dot.SetActive(true);
}
else
{
dot.SetActive(false);
}
}
}
nevermind I used chatgpt cause I wanted to make this work quickly and got it working.
If anyone has this problem imma leave my script.
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class IsSelected : MonoBehaviour, ISelectHandler, IDeselectHandler
{
private bool isHighlighted = false;
public GameObject dot;
public void OnSelect(BaseEventData eventData)
{
isHighlighted = true;
dot.SetActive(true);
Debug.Log("Button Highlighted");
}
public void OnDeselect(BaseEventData eventData)
{
isHighlighted = false;
dot.SetActive(false);
Debug.Log("Button Unhighlighted");
}
// You can use isHighlighted in other parts of your script as needed.
}
Umm just to clear this up, you would use IsHighlighted like a bool, all you had to do was put the selectabke Before it and take the BaseEvent out of the parenthesis for it to return correctly. I dont recommend anyone use chatgpt ever. It’s only worthwhile to write boilerplate if u akready know what ur doing, otherwise you wont learn anything.
using System;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace SomethingSomething.UI
{
public class BaseButton : Button
{
public static event Action<BaseButton> OnSelectButton = delegate { };
public override void OnSelect(BaseEventData eventData)
{
base.OnSelect(eventData);
OnSelectButton(this);
}
}
}