How to Keep a UI Button stay highlighted (or at least showing highlighted color) after clicking somewhere else?
I know this question has been asked many times, but the answers are mostly using toggles.
In my case, I have to use "Button". It has to be button.
Is there any way to keep the button highlighted (or at least showing a highlighted color) after I click elsewhere except for the button itself. And it only change back to normal unless I click on another button.
Yes, it looks like 99% the same as a tab menu. But I have to achieve this using "button element".
I hope someone could help me with this. Thanks!!
p.s. btw, I found similar question but I couldn't get his solution to work: http://answers.unity3d.com/questions/1155697/how-to-keep-your-button-selected-after-clicking-aw.html
Answer by DavidJayIndie · Jun 21, 2020 at 08:06 PM
It's an old question but in case someone is looking for the answer, I wrote this simple script to keep the last selected game object selected at all times. Just add this to your EventSystem object.
using UnityEngine;
using UnityEngine.EventSystems;
public class EventSystemKeepSelected : MonoBehaviour
{
private EventSystem eventSystem;
private GameObject lastSelected = null;
void Start()
{
eventSystem = GetComponent<EventSystem>();
}
void Update()
{
if (eventSystem != null)
{
if (eventSystem.currentSelectedGameObject != null)
{
lastSelected = eventSystem.currentSelectedGameObject;
}
else
{
eventSystem.SetSelectedGameObject(lastSelected);
}
}
}
}
Answer by ismaelnascimento01 · Jun 07, 2017 at 05:22 PM
Check link : https://docs.unity3d.com/Manual/script-Button.html
Your answer
Follow this Question
Related Questions
Reset Text 1 Answer
How to make ui only activate once 1 Answer
Button OnClick Event won't set Animator Trigger 0 Answers
Cursor dissapearing in my menu 0 Answers