Search Unity

Getting the GUI control under the mouse pointer

Discussion in 'Immediate Mode GUI (IMGUI)' started by Neural Echo, Nov 18, 2007.

  1. Neural Echo

    Neural Echo

    Joined:
    Jul 5, 2007
    Posts:
    83
    How do you get the GUI control (eg, GUI.Button) underneath the mouse pointer? I've tried the following code inside the OnGUI() function, but it doesn't work.

    Code (csharp):
    1.     var myguiLayer : GUILayer;
    2.     myguiLayer = Camera.main.GetComponent(GUILayer);  // Returns a reference.
    3.        
    4.     var activeControl : GUIElement;
    5.     activeControl = myguiLayer.HitTest(Input.mousePosition);
    The myguiLayer.HitTest() function call always appears to be returning 'null', even after I move the mouse pointer over a GUI control,

    Could this problem have something to do with the top-left origin of GUI co-ordinates versus the bottom-left origin of Mouse co-ordinates?
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    GUILayer.HitTest only finds old-school GUI components.

    In the new GUI system, there are no button objects created. Every frame, OnGUI is run, and the button is drawn immediately.

    If you zoom out a bit, what is the overall goal you're trying to accomplish?
     
  3. Neural Echo

    Neural Echo

    Joined:
    Jul 5, 2007
    Posts:
    83
    Ah, I see. Thanks for pointing that out.

    I am trying to get the co-ordinates and size of the button the mouse is over so I can position the tooltip relative to the button's location.