Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by Chrisv007 · May 07, 2019 at 05:00 PM · buttoncontrollerhighlight

Checking if button is highlighted with a controller (no mouse)

Hi guys, I'm using a controller button to highlight 3 buttons (ammo selection). The buttons get highlighted fine. The problem is that I need to execute a method when one of these buttons is highlighted. I don't want the player to have to highlight the button THEN press OK. I found a whole bunch of threads on this but they all seem to involve a mouse. My game is played with a controller. The question is: How can I achieve this? I tried all the scripts found here and there but none of them work. Here is the last option I found. That code transforms the button into something else which I can't access to make it intractable or not from another script. It also removes the OnClick that I would still like to keep for people not using a controller but using the keyboard instead. Any help would be appreciated. I spent all morning just trying to get this to work. I'm on 2018.3. Thanks.

 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 //Use the Selectable class as a base class to access the IsHighlighted method
 public class BuckShotSelected : Selectable
 {
     //Use this to check what Events are happening
     BaseEventData m_BaseEvent;
 
     void Update()
     {
         //Check if the GameObject is being highlighted
         if (IsHighlighted(m_BaseEvent) == true)
         {
             //Output that the GameObject was highlighted, or do something else
             Debug.Log("Selectable is Highlighted");
         }
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Fuzysasquatch · Nov 02, 2019 at 06:27 PM

I had a similar problem where I needed to execute a task when a certain button was highlighted with the controller instead of the mouse. I'm not sure if this is the best way to do this but so far this is the only way I can figure out how to do it. Use EventSystem.current.currentSelectedGameObject to store the current highlighted button. Then just simply check the gameObjects name and run desired method.

 public class ClassName: MonoBehaviour
 {
     GameObject currentSelected;
 
     void Update()
     {
         currentSelected = EventSystem.current.currentSelectedGameObject;
 
         if(currentSelected.name == "NameOfButton")
         {
             ... run desired code or method
         }
     }
 }

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 Snotax · Nov 14, 2020 at 07:42 AM

@Chrisv007 @Fuzysasquatch The simplest way to implement this is with the ISelectHandler interface. You can just add the interface to a MonoBehaviour on the button and there you have it:

public class UIButton : MonoBehaviour, ISelectHandler{ public void OnSelect(BaseEventData eventData) { //DO STUFF } }

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

130 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

How To Add Button Images To GUIText? 1 Answer

How to know if finger is on joystick if it is at horizontal & vertical zero 0 Answers

Help Needed: How to Use Input Manager on UI Button 0 Answers

How to make a menu controled from Keyboard and using buttons? 0 Answers

What is the mapping for the Right Stick on Xbox 360 Controllers? 1 Answer


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