Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SetEnabled

Discussion in 'UI Toolkit' started by Devi-User, Dec 3, 2018.

  1. Devi-User

    Devi-User

    Joined:
    Apr 30, 2016
    Posts:
    61
    I like the functionality of SetEnabled, which can block events for an object. However, this operation makes the object visually inactive (which is undoubtedly good in a huge number of situations). However, I came across a case when I wanted to block an element, but did not include a visual change (this is animation, and the object is not active until the animation ends). Is there any built-in way to do this?
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    You can SetEnabled(false) and just override the style for the "disabled state" to not be transparent. But I would argue that it's good to give the user some indication as to why the UI is unresponsive.
     
  3. Devi-User

    Devi-User

    Joined:
    Apr 30, 2016
    Posts:
    61
    Code (CSharp):
    1. .disabled-ignore:disabled
    2. {
    3.     opacity: 1;
    4. }
    Code (CSharp):
    1. public static void SetEnabled(this VisualElement element, bool enabled, bool visual)
    2. {
    3.     var hasChanges = element.enabledSelf != enabled;
    4.     if (!hasChanges)
    5.         return;
    6.     element.SetEnabled(enabled);
    7.     if (visual)
    8.         return;
    9.     if (enabled)
    10.         element.RemoveFromClassList("disabled-ignore");
    11.     else
    12.         element.AddToClassList("disabled-ignore");
    13.  
    14.     for (int i = 0; i < element.childCount; i++)
    15.         SetEnabledForChildNonVisual(element[i], enabled);
    16. }
    17.  
    18. private static void SetEnabledForChildNonVisual(VisualElement element, bool enabled)
    19. {
    20.     if (element.enabledSelf)
    21.     {
    22.         if (enabled)
    23.             element.RemoveFromClassList("disabled-ignore");
    24.         else
    25.             element.AddToClassList("disabled-ignore");
    26.  
    27.         for (int i = 0; i < element.childCount; i++)
    28.             SetEnabledForChildNonVisual(element[i], enabled);
    29.     }
    30. }
    31.  
     
    Last edited: Dec 9, 2018