Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

GUI.Button OnMouseOver events

Discussion in 'Scripting' started by besuser, Nov 19, 2007.

  1. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    Hi all,

    I'd like to execute different functions based on which button the user mouses over. I've dug around and the solution isn't obvious to me. Any ideas? I notice that any MonoBehaviour has an OnMouseOver function to override, but I'm not sure how I can take advantage of it in the context of a GUI (since the Buttons are sub elements of the GUI monobehaviour).

    Thanks in advance!
     
  2. Neural Echo

    Neural Echo

    Joined:
    Jul 5, 2007
    Posts:
    83
    I don't think you can use the OnMouseOver function with the Unity version 2.0 GUI because the buttons are not objects, they are only drawn when the OnGUI() function is executed.

    The only way I think you can determine which GUI button the mouse is over, is to set the tooltip for each button to a unique identifier, and use the tooltip string to determine which function to execute.

    PS: There is now a dedicated GUI forum you can post GUI related topics in.
     
  3. besuser

    besuser

    Joined:
    Oct 9, 2007
    Posts:
    292
    That just might work, thanks! Didn't notice the GUI forum, I apologize.
     
  4. Neural Echo

    Neural Echo

    Joined:
    Jul 5, 2007
    Posts:
    83
    I found out today that the OnGUI() function gets called twice during each frame (once for layout, and again for rendering), and that the tooltip string is only set during one of these calls. This means that depending on how you're using the tooltip string, it may only return a value every second execution of OnGUI().
     
  5. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    The OnGUI function can get called more or less any number of times during one frame - basically you get two calls for each event (mouse down, mouse up, mouse Drag, KeyDown, KeyUp, etc)

    At the end you get the repaint events.

    The tooltip is set during this repaint, so you can only count on it being set when
    Event.current.type == EventType.repaint