Search Unity

ObjectField keyboard selection changes focus to inspector window

Discussion in 'UIElements' started by Dunskat, Sep 3, 2019.

  1. Dunskat

    Dunskat

    Joined:
    Oct 17, 2017
    Posts:
    8
    Hey,
    I made an EditorWindow using UIElements, and for this specific workflow I require a lot of keyboard input, with not a lot of mouse interaction, and I'm trying to avoid extra mouse clicks when possible.
    I have an ObjectField and after it a TextField.
    When Tabbing to the ObjectField I can hit return, and the file selection dialogue allows me to use arrows and search to get the right file, but after hitting return it loses window focus and instead focuses on Inspector.

    I tried bringing the focus OnValueChange, however value change happens when the arrow button is hit on the file selection popupwindow, so it closes the popup window immediately before I pointed to the right file if I do that.

    Anyone know how to override this? Should this be addressed as a bug report?
     
  2. Dunskat

    Dunskat

    Joined:
    Oct 17, 2017
    Posts:
    8
    More details - it seems its not going specifically to the inspector, but to the last selected Unity Window that is not a custom EditorWindow (inspector/Hierarchy/Project). This problem appears to not be exclusive to UIElements like I originally suspected but more to EditorWindow.

    Is there a way to override ObjectPicker's default response on close?
     
  3. hugobd

    hugobd

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    15
    Hi @Dunskat!
    Unfortunately, the only way in UIElements to get callbacks from the ObjectSelector is by using the IMGUIContainer. There's actually a blog post talking about it: https://forum.unity.com/threads/uielements-questions-and-feedback.655279/

    I think something like this would work:
    Code (CSharp):
    1.        
    2. TextField tf = new TextField();
    3. IMGUIContainer imguiContainer = new IMGUIContainer(() =>
    4. {
    5.     EditorGUILayout.ObjectField(null, null, true);
    6.     if (Event.current.commandName == "ObjectSelectorClosed")
    7.         tf.Focus();
    8. });
    9.  
     
  4. Dunskat

    Dunskat

    Joined:
    Oct 17, 2017
    Posts:
    8
    Thanks, it works!
    A small thing, since I have many of those object fields in a single window, the result is a focus back to my window many times, and I wonder if I could filter it somehow.

    Is there a way to know which ObjectField or SerializedProperty the specific "ObjectSelectorClosed" was trying to modify?
     
  5. hugobd

    hugobd

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    15
    Hi!
    You need to assign an ID to object fields using EditorGUIUtility.ShowObjectPicker(). Then, you should be able to filter the events using EditorGUIUtility.GetObjectPickerControlID().