Unity Community

Results 1 to 5 of 5

  1. Posts



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


    1. public GUIStyle s;
    2. void OnGUI () {
    4.     if (GUI.Button(new Rect(100,100,100,100), s)) {
    5.         okToDraw = true;
    6.     }
    8.     if (okToDraw) {
    10.       GUILayout.BeginArea(new Rect(300,200,100,100));
    11.       //...
    12.       GUILayout.EndArea();
    13.     }
    15. }

    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
    Last edited by _joseph; 11-11-2011 at 09:11 PM.

  2. Posts

    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. Posts
    When the error happens, I debug the event like this:


    I get:


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

    1. if (Event.current.type == EventType.Repaint &&
    2.         Event.current.type == EventType.Layout) {
    4.        GUILayout.BeginArea(new Rect(100,100,100,100));
    5.        GUILayout.Button("Hello", s);
    6.        GUILayout.EndArea();
    7. }

    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. Location
    San Francisco
    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 by MStevenson; 11-13-2011 at 07:25 PM.

  5. Location
    San Francisco
    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:

    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);
    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.     }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts