예제 #1
0
        public GameObject Build()
        {
            float iv;

            // Bounds must be valid
            if (MaxValue.IsNaNOrInfinity())
            {
                throw new ArgumentException("MaxValue");
            }
            if (MinValue.IsNaNOrInfinity())
            {
                throw new ArgumentException("MinValue");
            }
            // max > min
            if (MaxValue <= MinValue)
            {
                throw new ArgumentOutOfRangeException("MaxValue");
            }
            // Initial value must be in range
            if (InitialValue.IsNaNOrInfinity())
            {
                iv = MinValue;
            }
            else
            {
                iv = InitialValue.InRange(MinValue, MaxValue);
            }
            var  slider     = PUIElements.CreateUI(null, Name);
            bool isVertical = Direction == Slider.Direction.BottomToTop || Direction ==
                              Slider.Direction.TopToBottom;
            var trueColor = HandleColor ?? PUITuning.Colors.ButtonBlueStyle;

            slider.SetActive(false);
            // Track (visual)
            var trackImg = slider.AddComponent <Image>();

            trackImg.sprite = isVertical ? PUITuning.Images.ScrollBorderVertical : PUITuning.
                              Images.ScrollBorderHorizontal;
            trackImg.type = Image.Type.Sliced;
            // Fill
            var fill    = PUIElements.CreateUI(slider, "Fill", true);
            var fillImg = fill.AddComponent <Image>();

            fillImg.sprite = isVertical ? PUITuning.Images.ScrollHandleVertical : PUITuning.
                             Images.ScrollHandleHorizontal;
            fillImg.color = trueColor.inactiveColor;
            fillImg.type  = Image.Type.Sliced;
            PUIElements.SetAnchorOffsets(fill, 1.0f, 1.0f, 1.0f, 1.0f);
            // Slider component itself
            var ks = slider.AddComponent <KSlider>();

            ks.maxValue     = MaxValue;
            ks.minValue     = MinValue;
            ks.value        = InitialValue;
            ks.wholeNumbers = IntegersOnly;
            ks.handleRect   = CreateHandle(slider).rectTransform();
            ks.fillRect     = fill.rectTransform();
            ks.SetDirection(Direction, true);
            if (OnValueChanged != null)
            {
                ks.onValueChanged.AddListener((value) => OnValueChanged(slider, value));
            }
            if (OnDrag != null)
            {
                ks.onDrag += () => OnDrag(slider, ks.value);
            }
            // Manually add tooltip with slider link
            string tt = ToolTip;

            if (!string.IsNullOrEmpty(tt))
            {
                var toolTip = slider.AddComponent <ToolTip>();
                toolTip.OnToolTip = () => string.Format(tt, ks.value);
                // Tooltip can be dynamically updated
                toolTip.refreshWhileHovering = true;
            }
            slider.SetActive(true);
            // Static layout!
            slider.SetMinUISize(isVertical ? new Vector2(TrackSize, PreferredLength) :
                                new Vector2(PreferredLength, TrackSize));
            slider.SetFlexUISize(FlexSize);
            OnRealize?.Invoke(slider);
            return(slider);
        }