Visibilty option in custom control

Hi!
I have created a custom slider inherited from default UI Toolkit Slider. It works everything I want, but, when I toggle visibility option in builder or vie code - my control don’t respond and stays always visible.

Here is the full code of my custom slider, I think I need to somehow manually manage visibility toggle, but IDK how… Thanks in advance!

    public class MDT_Slider : UnityEngine.UIElements.Slider
    {
        private VisualElement _filledTracker;
        private VisualElement _dragger;
        private VisualElement _tracker;
        private float _minValue = 0;
        private float _maxValue = 1;

        public float MinValue
        {
            get => _minValue;
            set
            {
                _minValue = value;
                this.lowValue = value;
            }
        }

        public float MaxValue
        {
            get => _maxValue;
            set
            {
                _maxValue = value;
                this.highValue = value;
            }
        }

        public MDT_Slider() : base()
        {
            var dragContainer = this.Q<VisualElement>("unity-drag-container");
            _dragger = this.Q<VisualElement>("unity-dragger");
            _tracker = this.Q<VisualElement>("unity-tracker");
            if (dragContainer != null)
            {
                _filledTracker = CreateTracker();
                dragContainer.Add(_filledTracker);
                dragContainer.Remove(_filledTracker);

                // Вставляем элемент на новую позицию, ниже остальных элементов
                dragContainer.Insert(0, _filledTracker);
            }
            this.RegisterCallback<ChangeEvent<float>>(OnValueChanged);
            _filledTracker.RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
        }

        private void OnGeometryChanged(GeometryChangedEvent evt)
        {
            _filledTracker.style.width = _dragger.resolvedStyle.translate.x + _dragger.resolvedStyle.width / 2;
        }

        private void OnValueChanged(ChangeEvent<float> evt)
        {
            _filledTracker.style.width = _dragger.resolvedStyle.translate.x + _dragger.resolvedStyle.width/2;
        }

        public VisualElement CreateTracker()
        {
            var filledTracker = new VisualElement() { name = "filled_tracker", 
                style = { left = _tracker.resolvedStyle.left, 
                top = _tracker.resolvedStyle.top,
                width = 0f} };
            filledTracker.style.position = _tracker.resolvedStyle.position;

            return filledTracker;
        }


        public new class UxmlFactory : UxmlFactory<MDT_Slider, UxmlTraits> { }

        public new class UxmlTraits : VisualElement.UxmlTraits 
        {
            UxmlFloatAttributeDescription attribute_minValue =
            new UxmlFloatAttributeDescription { name = "minValue", defaultValue = 0f };
            UxmlFloatAttributeDescription attribute_maxValue =
            new UxmlFloatAttributeDescription { name = "maxValue", defaultValue = 10f };

            public override void Init(VisualElement visual, IUxmlAttributes bag, CreationContext cc)
            {
                base.Init(visual, bag, cc);
                var slider = visual as MDT_Slider;

                slider.MinValue = attribute_minValue.GetValueFromBag(bag, cc);
                slider.MaxValue = attribute_maxValue.GetValueFromBag(bag, cc);
            }
        }
    }
}

That’s very odd. I can’t repro this with the standard Slider. Can you confirm:

  1. does Visibility work for the default Slider for you?
  2. does Display:none hide the whole MDT_Slider or does it also not work?
  3. in the UI Builder, if you turn on Preview mode, you can use the UI Debugger to pick your slider and see exactly the style properties of all child elements inside - might be able to see where the Visibility style property is reset

Hey, thanks for your attention!

  1. Yes it works fine with basic slider
  2. Yes, Display:none also works fine.
  3. I have checked with debugger and it seems that toggling the visibility for parent not affecting children’s visibility, which is strange to me. Next to visibility option of children in debugger there is inline, but in default slider’s children there is inherited - and that’s the main problem I see, but how can I solve it?
    image

image

Sometime after I’ve discovered, that custom style, that overwrites default ones make this, If I turn off all custom styles visibility works just fine
image

Are you saying that one of the children of your custom Slider has Visibility set to Visible, inline? If so, that would explain why setting the parent’s Visibility doesn’t have any effect. Visibility is like font size, it gets inherited from parents, but if the child has its own Visibility set to anything, that overrides what comes from parents.

Find the child element in the UI Builder that has this Visibility set and you can hover over the affordance icon right next to the field in the inspector and it should tell you where it’s coming from (you can also just right-click and unset it)
image

Thank you! I will try it out!