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 /
  • Help Room /
avatar image
0
Question by LarsBlack · Aug 03, 2018 at 06:43 AM · androiduibuttoncolorsound

[Need help] How to higlight button color when passing the mouse after a click?

Hello everyone,

I'm doing a piano application thought to be played on mobile devices (Android). All the piano keys are UI buttons that have the "property" pressed color to a grey one in order to properly indicate when a piano key is emiting sound.
My current problem is that when I first click on a key and after that I drag the mouse over the following keys, only the first one that I clicked is getting the change of color (the idea is that the change of color duration finishes when other key starts sound).
I also tryied setting the higlighted color property to the same color as the pressed color with the Navigation parameter to none (if it's set to automatic it happens some kind of bug that the color is getting "stuck" until I make sound another key), but the result it's still the same.

Edit: I update the issue with some progress thath I made: I'm trying to change the pressed color with a script thanks to the events Pointer exnter and exit (both events are placed on a Event trigger in every button). Code:

 public class ChangeKeyColor : MonoBehaviour{

 public Button button;

 void Start()
 {

 }

 void Update()
 {

 }

 public void EnterKey () {
     Debug.Log("Enter the key");
     ColorBlock colors = button.colors;
     colors.normalColor = new Color(179, 179, 179, 255);
     //colors.highlightedColor = new Color32(179, 179, 179, 255);
     button.colors = colors;
 }

 public void ExitKey()
 {
     Debug.Log("Exits the key");
     ColorBlock colors = button.colors;
     colors.normalColor = Color.white;
     //colors.highlightedColor = new Color32(255, 255, 255, 255);
     //colors.pressedColor = Color.white;
     button.colors = colors;
 }

 }

The only improvement that I obtained is that now when I'm dragging the mouse (maintaning it) the first button returns to white color, but I think that this is happening because now I only setted to gray color the pressed color option... Does anyone know why the pressed color change that I'm making in the script isn't happening? When I drag the mouse to another key isn't considered as a pressed button?

Regards!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ilyavitek · Aug 03, 2018 at 10:04 AM

I think that you can use 2 event triggers: https://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.OnPointerEnter.html https://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.OnPointerExit.html

**Edited: implemented by code and Added event to turn off highlight on last "button"*

You can use script below, it should work well (you can use it without button component or just change Image to Button):

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 [RequireComponent (typeof (Image))]
 public class MyBtn : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler, IPointerDownHandler, IPointerUpHandler {
 
     public delegate void PointerAction ();
     public static event PointerAction OnPointerUpEvent;
 
     Image image;
 
     bool isPointerHere = false;
 
     static bool isPointerDown = false;
 
     void Awake() {
         image = GetComponent<Image> ();
     }
 
     public void OnPointerExit (PointerEventData eventData) {
         MyBtn.OnPointerUpEvent -= CheckColor;
         isPointerHere = false;
         CheckColor ();
     }
 
     public void OnPointerEnter (PointerEventData eventData) {
         MyBtn.OnPointerUpEvent += CheckColor;
         isPointerHere = true;
         CheckColor ();
     }
 
     public void OnPointerDown (PointerEventData eventData) {
         isPointerHere = true;
         isPointerDown = true;
         CheckColor ();
     }
 
     public void OnPointerUp (PointerEventData eventData) {
         isPointerHere = false;
         isPointerDown = false;
         CheckColor ();
         OnPointerUpEvent ();
     }
 
     void CheckColor() {
         bool shouldBeHighlighted = isPointerDown == true && isPointerHere == true;
         image.color = shouldBeHighlighted ? Color.red : Color.white;
     }
 }




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 LarsBlack · Aug 03, 2018 at 10:24 AM 0
Share

I recently updated my issue, if you want to have a look.

avatar image ilyavitek LarsBlack · Aug 08, 2018 at 09:37 AM 0
Share

Cool! Now everything works as you expected? Also you can do it without attaching references on Event trigger by just mixing this two codes: https://docs.unity3d.com/ScriptReference/UI.Selectable.OnPointerExit.html https://docs.unity3d.com/ScriptReference/UI.Selectable.OnPointerEnter.html

avatar image LarsBlack ilyavitek · Aug 08, 2018 at 10:13 AM 0
Share

No, I make only a little progress as I updated my post, but there is still the issue that when I drag the finger along all the keys only the first one makes the color change... I will try without attaching references on EventTrigger as you said, and doing by code.

Show more comments

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

218 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 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

PlayOneShot getting cut off 2 Answers

Unity UI - Button Slow On Some Devices? 0 Answers

How do I get my Buttons to switch colors? 0 Answers

Change button color to intermediate value on onclick() event 1 Answer

UI text wont highlight 0 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