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
0
Question by kerembalci90 · Feb 01, 2015 at 05:23 PM · uibuttontextunity 4.6highlight

How to understand if 4.6 UI button is highlighted?

Hi all,

I want to change the color of the child text object of a button, when the button is highlighted. To do that, I am trying to find a way to understand if the button is highlighted? There is a protected method called IsHighlighted in UI.Button API; however, I haven't been able to make it work.

Any suggestions on how to do it?

Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Mr. Mud · Feb 01, 2015 at 06:54 PM

There is no reason to use scripts to solve this issue. However, depending on the circumstances, the solution might be slightly different. If only the text needs to change color (and not the background of the button), you can drag the text object in the "Target Graphic" field; now it should give you the desired behaviour.

In case you want to change multiple things at the same time, I would suggest to use "Animation" as the transition instead of "Color Tint". If no animator is present on the button, it will even create the animator with all required states (clicking "Auto Generate Animation"). From here on, you only need to create animations for the different states and assign them. This video from 3DBuzz explains most you need to know.

Comment
Add comment · Show 2 · 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 kerembalci90 · Feb 02, 2015 at 12:24 AM 0
Share

The tips you gave definitely solves my problem at the moment. Thanks. However, im asking just out of curiosity, is there any way to make IsHighlighted (UI.Button API) work? What is it's purpose?

avatar image Mr. Mud · Feb 02, 2015 at 11:21 AM 0
Share

Due to the property/field being protected, I have a feeling you are not supposed to use it for whatever you are trying to achieve. What it does precisely, is a mystery to me; I suppose it takes care of showing the correct states.

In any case though, you will most likely need to take care of the states themselves. Unless there is an easier method that I am not aware of. The interfaces in the UnityEngine.EventSystems namespace can help you in doing so, as they recieve the events for different states (the documentation on them is a bit lacking). I have bolded the ones you will most likely need:

IBeginDragHandler ICancelHandler IDeselectHandler IDragHandler IDropHandler IEndDragHandler IEventSystemHandler IInitializePotentialDragHandler I$$anonymous$$oveHandler IPointerClickHandler IPointerDownHandler IPointerEnterHandler IPointerExitHandler IPointerUpHandler IScrollHandler ISelectHandler ISubmitHandler IUpdateSelectedHandler

avatar image
0

Answer by Mmmpies · Feb 01, 2015 at 06:06 PM

You don't even need the EventSystem although do look into the EventSystem as it's very powerful.

With 4.6 UI buttons just change the color in the inspector, like this image show click on the Highlighted Color box and set it there:

ButtonColor


buttoncolor.png (14.7 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
avatar image
0

Answer by huulong · Feb 16, 2015 at 01:17 PM

The question of changing any properties has been answered by Yemachu, but if you are still interested in whether the button is highlighted:

First we assume that selected == highlighted. We will challenge this assumption later. The Button class derives from the Selectable class which contains what interests us, so I will consider GameObjects with a Selectable component. Below selectable stands for the component and selectableGameObject for the game object.

A. If you want to check whether a selectable is currently selected.

 using UnityEngine.EventSystems;
 ...
 if (EventSystem.current.currentSelectedGameObject == selectableGameObject) {...}

The problem is that you cannot trigger something when the object becomes selected, except inside Update() it is only useful for a one-time check.

B. If you want to trigger something when the object becomes selected/unselected, as Yemachu suggested, handle (de)selection in the OnXXX() methods associated to the interfaces of Selectable.

 Selectable : UIBehaviour,
         IMoveHandler,
         IPointerDownHandler, IPointerUpHandler,
         IPointerEnterHandler, IPointerExitHandler,
         ISelectHandler, IDeselectHandler
 Button : Selectable, IPointerClickHandler, ISubmitHandler

Since you want to use a button, you could create a subclass of Button and override some of the following methods:

OnDeselect(), OnSelect() ( BaseEventData parameter )

OnPointerEnter(), OnPointerExit() ( PointerEventData parameter )

and also OnPointerDown(), OnPointerUp() ( PointerEventData parameter ) if you want to add custom behaviours when the user presses / releases a mouse button.

Note: selectable.OnMove() will only check which other selectable object is in the direction you pressed (based on your current navigation mode), then select this object through OnSelect(), so you don't need to override OnMove() itself.

Do not forget to call base.OnXXX(eventData) at the beginning of your overriding method to ensure the normal behaviour is maintained.

Question: is selected equivalent to highlighted?

No, if you try to select an object from script while it is inactive in the hierarchy, or deactivate a selected object, and then you reactivate it, it will be selected but not highlighted. I am currently studying the source code of the UI and have found a few hacks and fixes to correct this behaviour, but first I must ensure what behaviour is really intended by the development team. If you do not manipulate your objects this way you should have no issues with that.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Best method for updating children of instantiated buttons? 0 Answers

How To Change Color Of Text On UI When It's Selected | Unity 4.6 3 Answers

Is there a way to have multiple sets of button highlights? 0 Answers

How do I set a GUI button's text using a string from another script? 0 Answers

Unable to reference a component. 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