Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Event.current.clickCount...?

Discussion in 'Immediate Mode GUI (IMGUI)' started by _joseph, Nov 12, 2011.

  1. _joseph

    _joseph

    Joined:
    Jun 14, 2011
    Posts:
    44
    Hi,

    I am using the mouse to click a button (which enables it) and then I need to start drawing via GUILayout.Begin and EndArea().

    Basically...:

    Code (csharp):
    1.  
    2. public GUIStyle s;
    3. void OnGUI () {
    4.    
    5.     if (GUI.Button(new Rect(100,100,100,100), s)) {
    6.         okToDraw = true;
    7.     }
    8.  
    9.     if (okToDraw) {
    10.  
    11.       GUILayout.BeginArea(new Rect(300,200,100,100));
    12.       //...
    13.       GUILayout.EndArea();
    14.     }
    15.  
    16. }
    17.  
    18.  
    I know that i need to do something with Event.current.clickCount or Event.current.Repaint but I do not know what =(

    EDIT: The exact error is: etting control 0's position in a group with only 0 controls when doing Repaint
    Aborting
     
    Last edited: Nov 12, 2011
  2. _joseph

    _joseph

    Joined:
    Jun 14, 2011
    Posts:
    44
    Hello,

    I wrote a simple example in a new unity project and the above code does not replicate the error.

    When the error is triggered I debug the line:

    Debug.Log("Event " + Event.current.type.ToString());

    which show two events:

    Layout and Repaint.

    I tried handling like this:

    if (Event.current.type == EventType.repaint
    Event.current.type == EventType.layout) {

    //Draw buttons
    }

    (and also EventType.Repaint EventType.Layout)

    But I do not see my buttons!

    Please let me know if that is still not enough information to help.
     
  3. _joseph

    _joseph

    Joined:
    Jun 14, 2011
    Posts:
    44
    When the error happens, I debug the event like this:

    Debug.Log(EventType.ToString());

    I get:

    Repaint
    Layout

    Which repeats...Thus, shouldn't I be writing a condition to handle that?

    Code (csharp):
    1.  
    2. if (Event.current.type == EventType.Repaint
    3.         Event.current.type == EventType.Layout) {
    4.  
    5.        GUILayout.BeginArea(new Rect(100,100,100,100));
    6.        GUILayout.Button("Hello", s);
    7.        GUILayout.EndArea();
    8. }
    9.  
    However, this prevents any gui draw calls within that condition. For example, the button with the string, "Hello" is never seen.

    I think its looking like incrementing a height variable and doing without the GUILayout..
     
  4. mstevenson

    mstevenson

    Joined:
    Sep 24, 2009
    Posts:
    189
    All of Unity's built-in GUI elements are self-contained. When called, they will handle their own click events and repainting automatically. You should be able to call GUI.Button() or GUILayout.Button() directly within the OnGUI() method and have it display.

    Event.current.clickCount and Event.current.Repaint are generally used for creating your own GUI elements completely from scratch.

    GUILayout.BeginArea and GUILayout.EndArea creates a rect in which to lay out multiple GUI elements. The elements that it contains will draw regardless of whether they're contained within an area, the area is simply an easy way to position an entire group of elements.

    There's also an issue in your first code example: "okToDraw" will only be true during the click. It will immediately revert to false on the next OnGUI call. To have it persist, you'll need to cache this value outside of the OnGUI method.
     
    Last edited: Nov 14, 2011
  5. mstevenson

    mstevenson

    Joined:
    Sep 24, 2009
    Posts:
    189
    If Unity's built-in button doesn't include all of the behavior that you need, you may have to create a new button type from scratch. This is the code for Unity's own GUI.Button, it's a pretty good example for how to use Event.current:

    Code (csharp):
    1.     public static bool Button(Rect position, GUIContent content, GUIStyle style)
    2.     {
    3.       GUIUtility.CheckOnGUI();
    4.       int controlId = GUIUtility.GetControlID(GUI.buttonHash, FocusType.Native, position);
    5.  
    6.       switch (Event.current.GetTypeForControl(controlId))
    7.       {
    8.         case EventType.MouseDown:
    9.           if (position.Contains(Event.current.mousePosition))
    10.           {
    11.             GUIUtility.hotControl = controlId;
    12.             Event.current.Use();
    13.           }
    14.           return false;
    15.         case EventType.MouseUp:
    16.           if (GUIUtility.hotControl != controlId)
    17.             return false;
    18.           GUIUtility.hotControl = 0;
    19.           Event.current.Use();
    20.           return position.Contains(Event.current.mousePosition);
    21.         case EventType.MouseDrag:
    22.           if (GUIUtility.hotControl == controlId)
    23.           {
    24.             Event.current.Use();
    25.             break;
    26.           }
    27.           else
    28.             break;
    29.         case EventType.Repaint:
    30.           style.Draw(position, content, controlId);
    31.           break;
    32.       }
    33.       return false;
    34.     }