Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question ObjectField : Catch MouseDownEvent while disabled

Discussion in 'UI Toolkit' started by mcallet_microidslyon, Mar 2, 2022.

  1. mcallet_microidslyon

    mcallet_microidslyon

    Joined:
    Sep 6, 2021
    Posts:
    2
    Hi,

    I am using the UIElements API and trying to display an ObjectField with the state Disabled.

    Here is my code :
    Code (CSharp):
    1. var objectField = new ObjectField("ObjectField");
    2. objectField.value = mySO;
    3. objectField.SetEnabled(false);
    The ObjectField is showing good, with the state Disabled, but like the SetEnabled method says : "A disabled VisualElement does not receive mosts events."

    My needs :
    I would like to have the same behaviour as the IMGUI doing : clicking on the disabled ObjectField will ping the object reference (using EditorGUIUtility.PingObject())

    I have tried to register an event with the RegisterCallback method, but it don't seems to be called (like SetEnabled says). I have the feeling that the element who catch the mouse event is disabled too.

    Anyone have a suggestion to handle this issue ?

    Thanks in advance :)
     
  2. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    198
    Unfortunately there's no easy way to do that in the current API, we have special internal variations of RegisterCallback and ExecuteDefaultAction for events whose targets are disabled, which we used to implement the ObjectField and some other special control behaviors but that's pretty much it for the moment. It is in our plans to come up with a clean expression for all of that in the API eventually, I just can't say exactly when and what form it will have for now.
     
    mcallet_microidslyon likes this.
  3. mcallet_microidslyon

    mcallet_microidslyon

    Joined:
    Sep 6, 2021
    Posts:
    2
    Alright, thanks for the answer.

    For now, i'll go with this workaround:

    Making an empty VisualElement who will catch the MouseDownEvent and place it in front of the disabled ObjectField.

    Code (CSharp):
    1.  
    2. var objectField = new ObjectField("Disabled Object Field");
    3. objectField .value = myObject;
    4. objectField .SetEnabled(false);
    5.  
    6. VisualElement clickCatcherContainer = new VisualElement();
    7. clickCatcherContainer .RegisterCallback<MouseDownEvent>((evt) => { EditorGUIUtility.PingObject(myObject); });
    8. clickCatcherContainer.AddToClassList("clickcatcher-container");
    9.  
    I don't know if I can set the "top" parameter through c#, so I'm using a .uss file :
    Code (csharp):
    1.  
    2. .clickcatcher-container
    3. {
    4.     top: -20px;
    5.     height: 19px;
    6. }
    7.  
    Hope this helps someone!
     
  4. quabug

    quabug

    Joined:
    Jul 18, 2015
    Posts:
    66
    Here's the hack I am using to make the object field clickable while disabled in hierarchy
    Code (CSharp):
    1.  
    2.         public static void ForceEnableClickable(this ObjectField objectField)
    3.         {
    4.             var obj = objectField.Q(className: ObjectField.objectUssClassName);
    5.             var pseudoStatesProperty = typeof(VisualElement).GetProperty("pseudoStates", BindingFlags.Instance | BindingFlags.NonPublic);
    6.             var states = (int)pseudoStatesProperty.GetValue(obj);
    7.             pseudoStatesProperty.SetValue(obj, states & ~32/* PseudoStates.Disabled */);
    8.         }
    9.  
     
    mcallet_microidslyon likes this.