Search Unity

GUI.TextField doesn't allow Mouse Events to be Processed on overlapping GUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by Curtonius, May 18, 2021.

  1. Curtonius

    Curtonius

    Joined:
    Sep 26, 2018
    Posts:
    1
    Hello everyone,

    I am working on a Tool in the UnityEditor and I am running into an issue where if a UI element that requires Events from Mouse Input to be processed is overlapping a GUI.TextField then I cannot process any Mouse events.

    I have already tried to do
    Code (CSharp):
    1. Event.current.Use()
    to use the event and have also tried to use
    Code (CSharp):
    1. GUI.FocusControl("")
    and
    Code (CSharp):
    1. GUI.FocusControl(null)
    but I can't seem to get it to work, instead when I select the UI element it will just select the TextField below it.

    upload_2021-5-18_14-57-39.png

    In the above image I have two overlapping UI elements for the Tool. The red dot is where I selected to select this UI Element. The green dot is the GUI.TextField below that is selected. When I selected that point it immediately began focusing on the TextField. And did not process any other events.

    I still need to be ABLE to edit the TextField at points so I will not be able to outright remove that ability.

    Thoughts and ideas are appreciated
     
  2. BachmannT

    BachmannT

    Joined:
    Nov 20, 2016
    Posts:
    388
    Hello
    I discover your post a little in late :) when I got the same issue.
    I wanted something like that: a textfield (with layout) and a button to clear the field upon the textfield.

    upload_2023-4-7_8-34-10.png

    The X button was never triggered because the texfield consume the events.
    On top of that, that was not possible to draw the X before the textfield because the textfield overlap the X button.
    The solution is to process the event before the textfield using it:

    Code (CSharp):
    1.  
    2.                 Event currentEvent = Event.current;
    3.                 // Check clearing the textfield before processing the textfield
    4.                 // Is a mouse down and the mouse position inside the X label ?
    5.                 if (currentEvent.type == EventType.MouseDown && rectClear.Contains(currentEvent.mousePosition))
    6.                     midiFilter = "";
    7.  
    8.                 midiFilter = GUILayout.TextField(midiFilter, MPTKGui.TextField);
    9.                 if (currentEvent.type != EventType.Layout && currentEvent.type != EventType.Used)
    10.                 {
    11.                     // Get the rect position of the textfield
    12.                     Rect last = GUILayoutUtility.GetLastRect();
    13.                     // Build the position of the label with X, rectClear must be an instance properties
    14.                     rectClear.x = last.x + last.width - 20;
    15.                     rectClear.y = last.y + 1;
    16.                     rectClear.width = rectClear.height = 20;
    17.                 }
    18.                 // Build a label which overlap the textfield
    19.                 GUI.Label(rectClear, new GUIContent(MPTKGui.IconDelete, "Clear Filter"), MPTKGui.Label);
    The full code will be available mid-April (2023) look at the Maestro Midi File Player Tool Kit.
    I hope that could help.
    Thierry
     
    Last edited: Apr 7, 2023