Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by UmbraFidelis · Apr 10, 2015 at 09:56 PM · unity 5ui

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlwaysSunny · Apr 10, 2015 at 09:56 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
11

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;
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image navratillukas · Sep 05, 2016 at 09:27 AM 0
Share

That's working exactly as expected. Thanks a lot

avatar image KarlKarl2000 · May 16, 2018 at 08:31 PM 0
Share

Just wanted to say 2 years later this still is the solution. Thank you @SleepDOTexe !

Works for Unity 2018.1

Sharin' is carin' <3

avatar image TSCSimulation_AH · Nov 30, 2018 at 02:34 PM 0
Share

Bit of an old post, but you don't need to create lastSelect as a new GameObject at all.

avatar image RickshawDerpyDerp · Jun 14, 2019 at 04:31 PM 0
Share

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

avatar image
6

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);
         }
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RodrigoSeVeN · Dec 10, 2015 at 02:27 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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();
             }
         }
     }
 }

alt text

alt text


01.png (46.0 kB)
02.png (48.1 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Failed to convert sprite texture type in unity UI 0 Answers

Making a dynamic radar type graph 3 Answers

Loading a scene after a random amount of time. 1 Answer

Need help with creating UI Text 1 Answer

the new UI system: scrollrect can not scroll when it's content have a PointerDown event 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges