Search Unity

GUILayout.Box and OnMouseEnter not firing

Discussion in 'Immediate Mode GUI (IMGUI)' started by therealdude, Apr 15, 2010.

  1. therealdude

    therealdude

    Joined:
    Mar 8, 2009
    Posts:
    10
    My Issue: I can't get OnMouseEnter events from a GUILayout.Box.
    My Goal: To create a GUI that has a large table or tree view of information from which the user can select an item or range of items.
    My Experience: years in coding, but very little with Unity and immediate mode GUI coding.
    My System: Unity 2.6.1 on Snow Leapord

    I'm experimenting with a long list of Box controls in a scroll view as follows. The script is tied to my Main Camera. Any ideas why I'm not getting the event or suggestions on how I can better achieve my goal? Thanks!


    void OnGUI() {
    GUI.skin = theSkin;
    if (isVisible)
    {
    Rect outer = new Rect((Screen.width - WIDTH), (Screen.height - HEIGHT) - 40, WIDTH, HEIGHT);
    scrollPosition = GUI.BeginScrollView(outer, scrollPosition, new Rect(0, 0, WIDTH-30, 60000), false, true);

    GUILayout.BeginVertical();
    mEnumerator.Reset();
    while (mEnumerator.MoveNext())
    {
    mItem = (MyItemType)mEnumerator.Current;
    GUILayout.Box(mItem.Name.ToUpper() + ":", myOptions);
    GUILayout.Box(mItem.Text, textStyle, mOptions);
    }
    GUI.EndScrollView();
    }

    }
    void OnMouseEnter() {
    Debug.Log("OnMouseEnter");
    }
    void OnMouseOver() {
    Debug.Log("OnMouseOver");
    }

    void OnMouseExit() {
    Debug.Log("OnMouseExit");
    }
     
  2. humptygrumpty

    humptygrumpty

    Joined:
    Apr 8, 2010
    Posts:
    5
    I'm definately not entirely confident to be giving advice, since I've only been going for a few days, but since you're in the OnGUI(), you might want to look at this, for getting mouse events

    Event.current

    ie inside the OnGUI()

    Code (csharp):
    1.  
    2. if (Event.current.type == EventType.mouseUp) {
    3.  
    4. }
    5.  
    You can just do a Debug.Log (Event.current); to get a stream of events inside the onGUI.
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The OnMouseXXX functions work when the mouse moves over an object with a collider or a GUIText/GUITexture component. You can check if the mouse position is within a control's rectangle using Rect.Contains.
     
  4. therealdude

    therealdude

    Joined:
    Mar 8, 2009
    Posts:
    10
    Thanks for the replies.
    It seems like I can get the job done using Button because I'll know exactly which button was pressed and I can tag my item as selected. Whereas with Event and Box, I'm not sure I'm guaranteed that the mouse event came from the last Box I drew.

    Do you think there is significant overhead using Button versus Box or Label? I have 1600 buttons in a Scroll View and I'm surprised how fast it's rendering my table on my MacBook. I want this to work on an iPad.