Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

GUI - Button hover change text color (Solved)

Discussion in 'Immediate Mode GUI (IMGUI)' started by DrDecipher, Aug 15, 2014.

  1. DrDecipher

    DrDecipher

    Joined:
    Dec 5, 2012
    Posts:
    54
    This has been driving me nuts. There is a bug in the GUI that prevents .hover and .onHover not working with text on a button. This is how it is worked around.

    Additionally you'll see a simple click and swap texture button another user posted as a solution.
    Note: This code is dynamically creating 10 buttons. That's the reason the Rects are being created on the fly.

    There is a delay on the hover swap that seems to be based on the OnGUI execute speed. If anyone has some thoughts on how to add another change handler,
    like:
    if (GUI.Button(layerVisPos, "", new GUIStyle()))

    for:
    onHover, that would be a welcome find.

    Hope this help some folks that never got their answer to this question.

    Cheers,
    Doc

    Code (CSharp):
    1. void OnGUI()
    2.     {
    3.         for (int i = 0; i < 10; i++)
    4.         {
    5.             // CLICK AND SWAP TEXTURE BUTTON
    6.                 // Button Rect
    7.                 Rect layerVisPos = new Rect(4, i * 26, 26, 26);
    8.  
    9.                 // Button Clicked
    10.                 if (GUI.Button(layerVisPos, "", new GUIStyle()))
    11.                     if (layerVisState)
    12.                         layerVisState = false;
    13.                     else
    14.                         layerVisState = true;
    15.                
    16.                 // Display proper texture
    17.                 if (layerVisState)
    18.                     GUI.DrawTexture(layerVisPos, layerVisOn);
    19.                 else
    20.                     GUI.DrawTexture(layerVisPos, layerVisOff);
    21.  
    22.             // HOVER CHANGE TEXT COLOR BUTTON
    23.                 // Rect
    24.                 layerSelectPos = new Rect(56, (i * 26), position.width - 54, 25);
    25.  
    26.                 //  Detect hover and swap color
    27.                 curEvent = Event.current;
    28.                 if (layerSelectPos.Contains(curEvent.mousePosition))
    29.                     guiButtonStyle.normal.textColor = Color.white;
    30.                 else
    31.                     guiButtonStyle.normal.textColor = Color.black;
    32.  
    33.                 // Hover Text Button
    34.                 GUI.Button(layerSelectPos, "Layer Name", guiButtonStyle);
    35.         }
    36.     }
     
  2. DrDecipher

    DrDecipher

    Joined:
    Dec 5, 2012
    Posts:
    54
    Wha BLAMMM!!

    This will give you a rollover that updates instantly and is only triggered when you are over the window itself.

    Code (CSharp):
    1.   void Update()
    2.     {
    3.         try
    4.         {
    5.             if (mouseOverWindow.GetType() == (typeof(YourWindowEditorClassHere)))
    6.                 Repaint();
    7.         }
    8.         catch { }
    9.     }
    Note: Admittedly I believe try/catch statement to be bad coding, but it exists for just such a situation.
    mouseOverWindow.GetType() can not be compared to null when you are on another monitor(I have a few). It will throw an exception no matter what you test it against.

    Happy Coding,
    Doc
     
    Fewes likes this.
  3. Tortuap

    Tortuap

    Joined:
    Dec 4, 2013
    Posts:
    137
    Are you fcking kidding ?
     
    Spark88 and nx-sm like this.