Beispiel #1
0
        public static UISliderRail Create(CreateArgs input)
        {
            GameObject go = new GameObject(input.widgetName);

            go.tag   = "UICollider";
            go.layer = LayerMask.NameToLayer("CameraHidden");

            // Find the anchor of the parent if it is a UIElement
            Vector3 parentAnchor = Vector3.zero;

            if (input.parent)
            {
                UIElement elem = input.parent.gameObject.GetComponent <UIElement>();
                if (elem)
                {
                    parentAnchor = elem.Anchor;
                }
            }

            UISliderRail uiSliderRail = go.AddComponent <UISliderRail>();

            uiSliderRail.transform.parent        = input.parent;
            uiSliderRail.transform.localPosition = parentAnchor + input.relativeLocation;
            uiSliderRail.transform.localRotation = Quaternion.identity;
            uiSliderRail.transform.localScale    = Vector3.one;
            uiSliderRail.width     = input.width;
            uiSliderRail.height    = input.height;
            uiSliderRail.thickness = input.thickness;
            uiSliderRail.margin    = input.margin;

            // Setup the Meshfilter
            MeshFilter meshFilter = go.GetComponent <MeshFilter>();

            if (meshFilter != null)
            {
                meshFilter.sharedMesh = UIUtils.BuildRoundedBox(input.width, input.height, input.margin, input.thickness);
            }

            // Setup the MeshRenderer
            MeshRenderer meshRenderer = go.GetComponent <MeshRenderer>();

            if (meshRenderer != null && input.material != null)
            {
                Material newMaterial = Instantiate(input.material);
                newMaterial.name            = "UISliderRail_Material";
                meshRenderer.sharedMaterial = newMaterial;

                uiSliderRail._color.useConstant = false;
                uiSliderRail._color.constant    = input.c.value;
                uiSliderRail._color.reference   = input.c;
                meshRenderer.sharedMaterial.SetColor("_BaseColor", uiSliderRail.Color);

                meshRenderer.shadowCastingMode  = UnityEngine.Rendering.ShadowCastingMode.Off;
                meshRenderer.renderingLayerMask = 2; // "LightLayer 1"
            }

            return(uiSliderRail);
        }
Beispiel #2
0
        public static UISlider Create(CreateArgs input)
        {
            GameObject go = new GameObject(input.widgetName);

            go.tag = "UICollider";

            // Find the anchor of the parent if it is a UIElement
            Vector3 parentAnchor = Vector3.zero;

            if (input.parent)
            {
                UIElement elem = input.parent.gameObject.GetComponent <UIElement>();
                if (elem)
                {
                    parentAnchor = elem.Anchor;
                }
            }

            UISlider uiSlider = go.AddComponent <UISlider>(); // NOTE: also creates the MeshFilter, MeshRenderer and Collider components

            uiSlider.relativeLocation        = input.relativeLocation;
            uiSlider.transform.parent        = input.parent;
            uiSlider.transform.localPosition = parentAnchor + input.relativeLocation;
            uiSlider.transform.localRotation = Quaternion.identity;
            uiSlider.transform.localScale    = Vector3.one;
            uiSlider.width                     = input.width;
            uiSlider.height                    = input.height;
            uiSlider.margin                    = input.margin;
            uiSlider.thickness                 = input.thickness;
            uiSlider.sliderPositionBegin       = input.sliderBegin;
            uiSlider.sliderPositionEnd         = input.sliderEnd;
            uiSlider.railMargin                = input.railMargin;
            uiSlider.railThickness             = input.railThickness;
            uiSlider.knobRadius                = input.knobRadius;
            uiSlider.knobDepth                 = input.knobDepth;
            uiSlider.dataSource                = input.dataSource;
            uiSlider.minValue                  = input.minValue;
            uiSlider.maxValue                  = input.maxValue;
            uiSlider.currentValue              = input.currentValue;
            uiSlider.textContent               = input.caption;
            uiSlider.sourceMaterial            = input.material;
            uiSlider.sourceRailMaterial        = input.railMaterial;
            uiSlider.sourceKnobMaterial        = input.knobMaterial;
            uiSlider.baseColor.useConstant     = false;
            uiSlider.baseColor.reference       = input.color;
            uiSlider.textColor.useConstant     = false;
            uiSlider.textColor.reference       = input.textColor;
            uiSlider.pushedColor.useConstant   = false;
            uiSlider.pushedColor.reference     = input.pushedColor;
            uiSlider.selectedColor.useConstant = false;
            uiSlider.selectedColor.reference   = input.selectedColor;

            // Setup the Meshfilter
            MeshFilter meshFilter = go.GetComponent <MeshFilter>();

            if (meshFilter != null)
            {
                meshFilter.sharedMesh = UIUtils.BuildRoundedBox(input.width, input.height, input.margin, input.thickness);
                uiSlider.Anchor       = Vector3.zero;
                BoxCollider coll = go.GetComponent <BoxCollider>();
                if (coll != null)
                {
                    Vector3 initColliderCenter = meshFilter.sharedMesh.bounds.center;
                    Vector3 initColliderSize   = meshFilter.sharedMesh.bounds.size;
                    if (initColliderSize.z < UIElement.collider_min_depth_shallow)
                    {
                        coll.center = new Vector3(initColliderCenter.x, initColliderCenter.y, UIElement.collider_min_depth_shallow / 2.0f);
                        coll.size   = new Vector3(initColliderSize.x, initColliderSize.y, UIElement.collider_min_depth_shallow);
                    }
                    else
                    {
                        coll.center = initColliderCenter;
                        coll.size   = initColliderSize;
                    }
                    coll.isTrigger = true;
                }
            }

            // Setup the MeshRenderer
            MeshRenderer meshRenderer = go.GetComponent <MeshRenderer>();

            if (meshRenderer != null && input.material != null)
            {
                // Clone the material.
                meshRenderer.sharedMaterial = Instantiate(input.material);

                meshRenderer.shadowCastingMode  = UnityEngine.Rendering.ShadowCastingMode.Off;
                meshRenderer.renderingLayerMask = 2; // "LightLayer 1"

                uiSlider.SetColor(input.color.value);
            }

            //
            // RAIL
            //

            float   railWidth     = (input.width - 2 * input.margin) * (input.sliderEnd - input.sliderBegin);
            float   railHeight    = 3 * uiSlider.railMargin; // TODO: see if we can tie this to another variable, like height.
            float   railThickness = uiSlider.railThickness;
            float   railMargin    = uiSlider.railMargin;
            Vector3 railPosition  = new Vector3(input.margin + (input.width - 2 * input.margin) * input.sliderBegin, -input.height / 2, -railThickness); // put z = 0 back

            uiSlider.rail = UISliderRail.Create(
                new UISliderRail.CreateArgs
            {
                parent           = go.transform,
                widgetName       = "Rail",
                relativeLocation = railPosition,
                width            = railWidth,
                height           = railHeight,
                thickness        = railThickness,
                margin           = railMargin,
                material         = input.railMaterial,
                c = input.railColor
            }
                );

            // KNOB
            float newKnobRadius = uiSlider.knobRadius;
            float newKnobDepth  = uiSlider.knobDepth;

            float pct = (uiSlider.currentValue - uiSlider.minValue) / (uiSlider.maxValue - uiSlider.minValue);

            float widthWithoutMargins = input.width - 2.0f * input.margin;
            float startX = input.margin + widthWithoutMargins * uiSlider.sliderPositionBegin + railMargin;
            float endX   = input.margin + widthWithoutMargins * uiSlider.sliderPositionEnd - railMargin;
            float posX   = startX + pct * (endX - startX);

            Vector3 knobPosition = new Vector3(posX - uiSlider.knobRadius, uiSlider.knobRadius - (uiSlider.height / 2.0f), -uiSlider.knobDepth);

            uiSlider.knob = UISliderKnob.Create(
                new UISliderKnob.CreateArgs
            {
                widgetName       = "Knob",
                parent           = go.transform,
                relativeLocation = knobPosition,
                radius           = newKnobRadius,
                depth            = newKnobDepth,
                material         = input.knobMaterial,
                c = input.knobColor
            }
                );

            //
            // CANVAS (to hold the 2 texts)
            //

            GameObject canvas = new GameObject("Canvas");

            canvas.transform.parent = uiSlider.transform;

            Canvas c = canvas.AddComponent <Canvas>();

            c.renderMode = RenderMode.WorldSpace;

            RectTransform rt = canvas.GetComponent <RectTransform>(); // auto added when adding Canvas

            rt.localScale    = Vector3.one;
            rt.localRotation = Quaternion.identity;
            rt.anchorMin     = new Vector2(0, 1);
            rt.anchorMax     = new Vector2(0, 1);
            rt.pivot         = new Vector2(0, 1); // top left
            rt.sizeDelta     = new Vector2(uiSlider.width, uiSlider.height);
            rt.localPosition = Vector3.zero;

            CanvasScaler cs = canvas.AddComponent <CanvasScaler>();

            cs.dynamicPixelsPerUnit   = 300; // 300 dpi, sharp font
            cs.referencePixelsPerUnit = 100; // default?

            // Add a Text under the Canvas
            if (input.caption.Length > 0)
            {
                GameObject text = new GameObject("Text");
                text.transform.parent = canvas.transform;

                TextMeshProUGUI t = text.AddComponent <TextMeshProUGUI>();
                t.text             = input.caption;
                t.enableAutoSizing = true;
                t.fontSizeMin      = 1;
                t.fontSizeMax      = 500;
                t.fontStyle        = FontStyles.Normal;
                t.alignment        = TextAlignmentOptions.Left;
                t.color            = input.textColor.value;
                t.ForceMeshUpdate();

                RectTransform trt = t.GetComponent <RectTransform>();
                trt.localScale    = 0.01f * Vector3.one;
                trt.localRotation = Quaternion.identity;
                trt.anchorMin     = new Vector2(0, 1);
                trt.anchorMax     = new Vector2(0, 1);
                trt.pivot         = new Vector2(0, 1); // top left
                trt.sizeDelta     = new Vector2((uiSlider.width - 2 * uiSlider.margin) * uiSlider.sliderPositionBegin * 100.0f, (input.height - 2.0f * input.margin) * 100.0f);
                float textPosLeft = uiSlider.margin;
                trt.localPosition = new Vector3(textPosLeft, -uiSlider.margin, -0.002f);
            }

            // Text VALUE
            //if (caption.Length > 0)
            {
                GameObject text = new GameObject("TextValue");
                text.transform.parent = canvas.transform;

                TextMeshProUGUI t = text.AddComponent <TextMeshProUGUI>();
                t.text             = input.currentValue.ToString("#0.00");
                t.enableAutoSizing = true;
                t.fontSizeMin      = 1;
                t.fontSizeMax      = 500;
                t.fontSize         = 1.85f;
                t.fontStyle        = FontStyles.Normal;
                t.alignment        = TextAlignmentOptions.Right;
                t.color            = input.textColor.value;

                RectTransform trt = t.GetComponent <RectTransform>();
                trt.localScale    = 0.01f * Vector3.one;
                trt.localRotation = Quaternion.identity;
                trt.anchorMin     = new Vector2(0, 1);
                trt.anchorMax     = new Vector2(0, 1);
                trt.pivot         = new Vector2(1, 1); // top right?
                trt.sizeDelta     = new Vector2((uiSlider.width - 2 * uiSlider.margin) * (1 - uiSlider.sliderPositionEnd) * 100.0f, (input.height - 2.0f * input.margin) * 100.0f);
                float textPosRight = uiSlider.width - uiSlider.margin;
                trt.localPosition = new Vector3(textPosRight, -uiSlider.margin, -0.002f);
            }

            UIUtils.SetRecursiveLayer(go, "CameraHidden");

            return(uiSlider);
        }