Search Unity

[Solved] Custom Editor GUI.Button clickable area does not fit the button

Discussion in 'Immediate Mode GUI (IMGUI)' started by Dliix66, Nov 7, 2019.

  1. Dliix66

    Dliix66

    Joined:
    Sep 5, 2017
    Posts:
    16
    Hello,

    I'm adding a button in a custom inspector like this:

    Code (CSharp):
    1.  
    2. public override void OnInspectorGUI()
    3. {
    4.     // some other stuff
    5.    
    6.     var myRect = new Rect(xPos, yPos, width, height);
    7.     var previousColor = GUI.color;
    8.     GUI.color = Color.red;
    9.    
    10.     if (GUI.Button(myRect, new GUIContent("X", "Remove")))
    11.     {
    12.         // stuff in here when button is clicked
    13.     }
    14.    
    15.     GUI.color = previousColor;
    16.    
    17.     // some other stuff
    18. }

    And it is only clickable below the text and/or (no matter the width) as show in the attached gif.

    Any idea about what am I doing wrong and/or how to fix this behavior?

    Thanks for your help,

    Regards,

    PS: I can not use `GUILayout.Button` because I want to specify a rect to place the button wherever I need it.
     

    Attached Files:

  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    Some other GUI element is probably consuming the click event before it gets to your button. So the problem most likely lies somewhere in the " // some other stuff" section before your button. Any buttons, foldouts and other interactive GUI elements could be the culprit.
     
  3. Dliix66

    Dliix66

    Joined:
    Sep 5, 2017
    Posts:
    16
    Hello SisusCo,

    Thanks for your reply, Indeed, the button is Displayed on the top right part of a foldout. As . the foldout is trasnparent, I thought it was over it. Is there any way to force it to be OVER the foldout ? (The `GUI.Button` code is after the foldout code.)

    Thanks again for taking the time to help me !

    Regards,
     
  4. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    Yeah it's a bit counter-intuitive, but IMGUI elements consume click events in the order they are called in your code. So if you were to draw two buttons on top of each other, only the first one that is behind the second one would react to the input.

    If you draw the transparent foldout after the button, then the button should start working.

    If that is not possible in your case, you can also manually detect click events instead of relying on GUI.Button to do it for you.

    Code (CSharp):
    1. bool buttonClicked = Event.current.rawType == EventType.MouseDown && myRect.Contains(Event.current.mousePosition);
    2. if(GUI.Button(myRect, new GUIContent("X", "Remove")) || buttonClicked)
    3. {
    4.     // stuff in here when button is clicked
    5. }
    Event.rawType (undocumented property) can be used to get the type of the event even after it has been used, so it should work even if another overlapping GUI element already reacted to the click.
     
    Dliix66 likes this.
  5. Dliix66

    Dliix66

    Joined:
    Sep 5, 2017
    Posts:
    16

    Thanks ! Everything works fine now !
     
    SisusCo likes this.
  6. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    Glad to hear it :)
     
    Dliix66 likes this.