Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

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:
    118
    Are you fcking kidding ?
     
    Spark88 and nx-sm like this.