Search Unity

Disabled PropertyField is not clickable

Discussion in 'UI Toolkit' started by androshchuk-vladyslav, Aug 20, 2019.

  1. androshchuk-vladyslav

    androshchuk-vladyslav

    Joined:
    Dec 13, 2015
    Posts:
    127
    Hi. I want to disable PropertyField but leave possibility to click on object (it selects that object in Project view). In old UI we had DisabledScope/BeginDisabled.

    Now when you make PropertyField as Disabled it stops to get any events.
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Hello,

    This is not supported at the moment since UIElements will treat everything disabled as not clickable.

    If you're OK with not using disabled state at all, you could simply block double click events on the ObjectField:

    Code (CSharp):
    1. root.Q<ObjectField>().RegisterCallback<MouseDownEvent>(evt => {
    2. if (evt.clickCount == 2)
    3.    evt.StopPropagation();
    4. });
     
  3. androshchuk-vladyslav

    androshchuk-vladyslav

    Joined:
    Dec 13, 2015
    Posts:
    127
    Like I want to:
    - Disable it visually;
    - Disable ability to choose object and drag-n-drop.

    But leave ability to:
    - Double click to select object in project view.

    Like readonly. Thanks!
     
    Kekito likes this.
  4. Aldeminor

    Aldeminor

    Joined:
    May 3, 2014
    Posts:
    18
    Hello, dear Unity Team and fellow Developers!
    It may look like I'm figured out how to emulate the original 'disabled but clickable' behavior of the script field.
    Basically I just wrapped the disabled ObjectField with another VisualElement and assigned click handler to it, here is the code:
    Code (CSharp):
    1.  
    2. [CustomEditor(typeof(UnityEngine.Object))]
    3. public class DefaultUIElementsEditor : Editor
    4. {
    5.     private const string script_property_name = "m_Script";
    6.     public override VisualElement CreateInspectorGUI()
    7.     {
    8.         var root = new VisualElement();
    9.         var iterator = serializedObject.GetIterator();
    10.         if (iterator.NextVisible(true))
    11.         {
    12.             do
    13.             {
    14.                 VisualElement propertyField;
    15.  
    16.                 if (iterator.propertyPath.Equals(script_property_name) && serializedObject.targetObject)
    17.                 {
    18.                     propertyField = CommonUIElements.CreateDisabledClickableObjectField(
    19.                         iterator.Copy(),
    20.                         obj=> AssetDatabase.OpenAsset(obj),
    21.                         out _);
    22.                 }
    23.                 else propertyField = new PropertyField(iterator.Copy()) { name = $"PropertyField: {iterator.propertyPath}" };
    24.  
    25.                 root.Add(propertyField);
    26.             }
    27.             while (iterator.NextVisible(false));
    28.         }
    29.         return root;
    30.     }
    31. }
    32. ... (following is in another static class for shorthand)
    33. public static VisualElement CreateDisabledClickableObjectField(
    34.     SerializedProperty objectReferenceProperty,
    35.     Action<Object> doubleClickAction,
    36.     out ObjectField underlyingField)
    37. {
    38.    
    39.     var objectField = new ObjectField(objectReferenceProperty.displayName)
    40.     {
    41.         value = objectReferenceProperty.objectReferenceValue
    42.     };
    43.     objectField.SetEnabled(false);
    44.     var clickableContainer = new VisualElement();
    45.     clickableContainer.Add(objectField);
    46.     clickableContainer.RegisterCallback<MouseDownEvent>(evt =>
    47.     {
    48.         if (!objectField.value)
    49.             return;
    50.         EditorGUIUtility.PingObject(objectField.value);
    51.         if (evt.clickCount == 2)
    52.             doubleClickAction?.Invoke(objectField.value);
    53.            
    54.     } );
    55.     underlyingField = objectField;
    56.     return clickableContainer;
    57. }
    58.