- Home /
UI 4.6 - Disable Mouse From Stealing Focus
Hi!
I'm developing a game where I'm using the new (and sweet) Unity UI system. However my main idea is wanting to navigate through the menu, using keyboard or an Xbox controller.
I've got the keyboard and Xbox controller code down and everything is basically working as intended.
However when I left click anywhere in my UI the mouse takes away focus from the currently selected button. To regain focus i have to re-click one of the UI buttons in the scene.
I really have no use for mouse input at all with my UI. So is there a way to completely disable mouse input with the 4.6 UI or stop mouse clicks from removing focus from the buttons?
All help is appreciated and i don't expect anyone to make code for me, i just need to be pointed in the right direction.
Thank you for your time!
Interesting question. This is probably not something Unity ever expected anyone to want to do.
Not sure whether creating a dummy script implementing all the mouse event interfaces with empty bodies would actually prevent focus changes, but that's something you could try.
I'd be wary of disabling mouse interaction altogether; even in a text-based game. Unless players haven't touched a computer since 1985, they're going to expect the mouse to work.
Answer by SleepDOTexe · Jun 04, 2016 at 05:09 PM
To get focus to return to the previously selected object, you could try a script like this.
public class Initialise_Button : MonoBehaviour {
GameObject lastselect;
void Start()
{
lastselect = new GameObject();
}
// Update is called once per frame
void Update () {
if (EventSystem.current.currentSelectedGameObject == null)
{
EventSystem.current.SetSelectedGameObject(lastselect);
}
else
{
lastselect = EventSystem.current.currentSelectedGameObject;
}
}
}
Just wanted to say 2 years later this still is the solution. Thank you @SleepDOTexe !
Works for Unity 2018.1
Sharin' is carin' <3
Bit of an old post, but you don't need to create lastSelect as a new GameObject at all.
For some reason I had to change every instance of '''EventSystem''' to UnityEngine.EventSystems.EventSystem Even though I had all the defualt import statements. This is for Unity 2019.1. Other than that, works perfectly
Answer by DeeCeptor · Nov 20, 2015 at 09:41 PM
I wrote my own solution that simply sets the focus back to the first selected item in the Event System. Simply attach this script to any game object in the scene.
using UnityEngine;
using UnityEngine.EventSystems;
// If there is no selected item, set the selected item to the event system's first selected item
public class ControllerRefocus : MonoBehaviour
{
void Update()
{
if (EventSystem.current.currentSelectedGameObject == null)
{
Debug.Log("Reselecting first input");
EventSystem.current.SetSelectedGameObject(EventSystem.current.firstSelectedGameObject);
}
}
}
The next step would be to save the last selected element, which is what I'll be doing in my game, so ins$$anonymous$$d, I set it to the last element selected and not the first one.
Add a variable to the mix, save the current game object into it, update whenever it changes and isn't null and finally use the "if" above to set it to the variable whenever it's null.
Works perfectly and you don't even need to disable mouse even if it's keyboard/gamepad only.
Answer by pfreese · Apr 22, 2015 at 01:24 AM
I don't think this is possible without modifying the UI source code ( https://bitbucket.org/Unity-Technologies/ui ).
If you decide to go that route, it should be fairly easy to accomplish: modify the Selectable class so that you remove the behavior in the various OnPointer methods: OnPointerDown(), OnPointerUp(), OnPointerEnter(), and OnPointerExit(). The downside of this is that you're basically forking the UI code and won't easily have access to new fixes and features.
Answer by carlos_cabral · May 28, 2015 at 07:00 AM
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
[System.Serializable]
public class Buttons
{
public string name;
public GameObject gameObject;
public Button button;
}
public class scrDeselectButtonNoMore : MonoBehaviour
{
public Buttons[] buttons;
public void Selected()
{
for (int i = 0; i < buttons.Length; i++)
{
if (EventSystem.current.lastSelectedGameObject == buttons[i].gameObject)
{
buttons[i].button.Select();
}
}
}
}