示例#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);
        }
示例#2
0
        public void RebuildMesh(float newKnobRadius, float newKnobDepth)
        {
            MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>();
            // Make a cylinder using RoundedBox
            Mesh theNewMesh = UIUtils.BuildRoundedBox(2.0f * newKnobRadius, 2.0f * newKnobRadius, newKnobRadius, newKnobDepth);

            theNewMesh.name       = "UISliderKnob_GeneratedMesh";
            meshFilter.sharedMesh = theNewMesh;

            radius = newKnobRadius;
            depth  = newKnobDepth;
        }
示例#3
0
        public static UIVerticalSliderKnob 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;
                }
            }

            UIVerticalSliderKnob uiSliderKnob = go.AddComponent <UIVerticalSliderKnob>();

            uiSliderKnob.transform.parent        = input.parent;
            uiSliderKnob.transform.localPosition = parentAnchor + input.relativeLocation;
            uiSliderKnob.transform.localRotation = Quaternion.identity;
            uiSliderKnob.transform.localScale    = Vector3.one;
            uiSliderKnob.radius = input.radius;
            uiSliderKnob.depth  = input.depth;

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

            if (meshFilter != null)
            {
                meshFilter.sharedMesh = UIUtils.BuildRoundedBox(2.0f * input.radius, 2.0f * input.radius, input.radius, input.depth);
            }

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

            if (meshRenderer != null && input.material != null)
            {
                meshRenderer.sharedMaterial     = Instantiate(input.material);
                uiSliderKnob._color.useConstant = false;
                uiSliderKnob._color.reference   = input.c;
                meshRenderer.sharedMaterial.SetColor("_BaseColor", uiSliderKnob.Color);

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

            return(uiSliderKnob);
        }
示例#4
0
        public void RebuildMesh(float newWidth, float newHeight, float newThickness, float newMargin)
        {
            MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>();
            Mesh       theNewMesh = UIUtils.BuildRoundedBox(width, height, margin, thickness);

            theNewMesh.name       = "UISliderRail_GeneratedMesh";
            meshFilter.sharedMesh = theNewMesh;

            width     = newWidth;
            height    = newHeight;
            thickness = newThickness;
            margin    = newMargin;
        }
示例#5
0
        public static UISpinner 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;
                }
            }

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

            uiSpinner.relativeLocation        = input.relativeLocation;
            uiSpinner.transform.parent        = input.parent;
            uiSpinner.transform.localPosition = parentAnchor + input.relativeLocation;
            uiSpinner.transform.localRotation = Quaternion.identity;
            uiSpinner.transform.localScale    = Vector3.one;
            uiSpinner.width                      = input.width;
            uiSpinner.height                     = input.height;
            uiSpinner.margin                     = input.margin;
            uiSpinner.thickness                  = input.thickness;
            uiSpinner.separationPositionPct      = input.spinner_separation_pct;
            uiSpinner.textAndValueVisibilityType = input.visibility_type;
            uiSpinner.spinnerValueType           = input.value_type;
            uiSpinner.minValue                   = input.min_spinner_value;
            uiSpinner.maxValue                   = input.max_spinner_value;
            uiSpinner.currentValue               = input.cur_spinner_value;
            uiSpinner.valueRate                  = input.spinner_value_rate;
            uiSpinner.valueRateRay               = input.spinner_value_rate_ray;
            uiSpinner.textContent                = input.caption;
            uiSpinner.baseColor.useConstant      = false;
            uiSpinner.baseColor.reference        = input.background_color;
            uiSpinner.textColor.useConstant      = false;
            uiSpinner.textColor.reference        = input.textColor;
            uiSpinner.pushedColor.useConstant    = false;
            uiSpinner.pushedColor.reference      = input.pushedColor;
            uiSpinner.selectedColor.useConstant  = false;
            uiSpinner.selectedColor.reference    = input.selectedColor;
            uiSpinner.sourceMaterial             = input.background_material;

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

            if (meshFilter != null)
            {
                meshFilter.sharedMesh = UIUtils.BuildRoundedBox(input.width, input.height, input.margin, input.thickness);
                uiSpinner.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.background_material != null)
            {
                // Clone the material.
                meshRenderer.sharedMaterial = Instantiate(input.background_material);

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

                uiSpinner.SetColor(input.background_color.value);
            }

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

            GameObject canvas = new GameObject("Canvas");

            canvas.transform.parent = uiSpinner.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(uiSpinner.width, uiSpinner.height);
            rt.localPosition = Vector3.zero;

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

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

            bool hasText = (input.visibility_type == TextAndValueVisibilityType.ShowTextAndValue);

            // Add a Text under the Canvas
            {
                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;

                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(
                    (uiSpinner.width - 2 * uiSpinner.margin) * uiSpinner.separationPositionPct * 100.0f,
                    (uiSpinner.height - 2 * uiSpinner.margin) * 100.0f);
                float textPosLeft = uiSpinner.margin;
                trt.localPosition = new Vector3(textPosLeft, -uiSpinner.margin, -0.002f);

                // hide if ValueOnly
                text.SetActive(hasText);
            }

            // Text VALUE
            {
                GameObject text = new GameObject("TextValue");
                text.transform.parent = canvas.transform;

                TextMeshProUGUI t = text.AddComponent <TextMeshProUGUI>();
                t.text = (input.value_type == SpinnerValueType.Float)
                    ? input.cur_spinner_value.ToString(default_value_format)
                    : Mathf.RoundToInt(input.cur_spinner_value).ToString();
                t.enableAutoSizing = true;
                t.fontSizeMin      = 1;
                t.fontSizeMax      = 500;
                t.fontStyle        = FontStyles.Normal;
                t.alignment        = hasText ? TextAlignmentOptions.Right : TextAlignmentOptions.Center;
                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     = hasText ?
                                    new Vector2(
                    (uiSpinner.width - 2 * uiSpinner.margin) * (1 - uiSpinner.separationPositionPct) * 100.0f,
                    (uiSpinner.height - 2 * uiSpinner.margin) * 100.0f)
                    : new Vector2(
                    (uiSpinner.width - 2 * uiSpinner.margin) * 100.0f,
                    (uiSpinner.height - 2 * uiSpinner.margin) * 100.0f);
                float textPos = hasText ?
                                uiSpinner.width - uiSpinner.margin // right
                    : uiSpinner.width - uiSpinner.margin;          // or middle
                trt.localPosition = new Vector3(textPos, -uiSpinner.margin, -0.002f);
            }

            UIUtils.SetRecursiveLayer(go, "CameraHidden");

            return(uiSpinner);
        }
示例#6
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);
        }
示例#7
0
        public static UICheckbox Create(CreateParams input)
        {
            GameObject go = new GameObject(input.widgetName)
            {
                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;
                }
            }

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

            uiCheckbox.relativeLocation        = input.relativeLocation;
            uiCheckbox.transform.parent        = input.parent;
            uiCheckbox.transform.localPosition = parentAnchor + input.relativeLocation;
            uiCheckbox.transform.localRotation = Quaternion.identity;
            uiCheckbox.transform.localScale    = Vector3.one;
            uiCheckbox.width                         = input.width;
            uiCheckbox.height                        = input.height;
            uiCheckbox.margin                        = input.margin;
            uiCheckbox.thickness                     = input.thickness;
            uiCheckbox.content                       = input.content;
            uiCheckbox.checkedSprite                 = input.checkedIcon;
            uiCheckbox.uncheckedSprite               = input.uncheckedIcon;
            uiCheckbox.textContent                   = input.caption;
            uiCheckbox.source_material               = input.material;
            uiCheckbox.baseColor.useConstant         = false;
            uiCheckbox.baseColor.reference           = input.color;
            uiCheckbox.textColor.useConstant         = false;
            uiCheckbox.textColor.reference           = input.textColor;
            uiCheckbox.disabledTextColor.useConstant = false;
            uiCheckbox.disabledTextColor.reference   = input.disabledTextColor;
            uiCheckbox.pushedColor.useConstant       = false;
            uiCheckbox.pushedColor.reference         = input.pushedColor;
            uiCheckbox.selectedColor.useConstant     = false;
            uiCheckbox.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);
                uiCheckbox.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 && uiCheckbox.source_material != null)
            {
                // Clone the material.
                meshRenderer.sharedMaterial     = Instantiate(uiCheckbox.source_material);
                meshRenderer.shadowCastingMode  = UnityEngine.Rendering.ShadowCastingMode.Off;
                meshRenderer.renderingLayerMask = 2; // "LightLayer 1"

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

            // Add a Canvas
            GameObject canvas = new GameObject("Canvas");

            canvas.transform.parent = uiCheckbox.transform;

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

            c.renderMode   = RenderMode.WorldSpace;
            c.sortingOrder = 1;

            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(uiCheckbox.width, uiCheckbox.height);
            rt.localPosition = Vector3.zero;

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

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

            float minSide = Mathf.Min(uiCheckbox.width, uiCheckbox.height);

            // Add an Image under the Canvas
            if (input.uncheckedIcon != null && input.checkedIcon != null)
            {
                GameObject image = new GameObject("Image");
                image.transform.parent = canvas.transform;

                Image img = image.AddComponent <Image>();
                img.sprite = input.uncheckedIcon;
                img.color  = input.textColor.value;

                RectTransform trt = image.GetComponent <RectTransform>();
                trt.localScale    = 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
                // TODO: non square icons ratio...
                trt.sizeDelta     = new Vector2(minSide - 2.0f * input.margin, minSide - 2.0f * input.margin);
                trt.localPosition = new Vector3(input.margin, -input.margin, -0.001f);
            }

            // Add a Text under the Canvas
            if (input.content == CheckboxContent.CheckboxAndText)
            {
                GameObject text = new GameObject("Text");
                text.transform.parent = canvas.transform;

                TextMeshProUGUI t = text.AddComponent <TextMeshProUGUI>();
                t.text             = input.caption;
                t.enableAutoSizing = false;
                t.fontSize         = 18;
                t.fontSizeMin      = 1;
                t.fontSizeMin      = 500;
                t.fontStyle        = FontStyles.Normal;
                t.alignment        = TextAlignmentOptions.MidlineLeft;
                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(0, 1); // top left

                trt.sizeDelta     = new Vector2((input.width - minSide - input.margin) * 100.0f, (input.height - 2.0f * input.margin) * 100.0f);
                trt.localPosition = new Vector3(minSide, -input.margin, -0.002f);
            }

            UIUtils.SetRecursiveLayer(go, "CameraHidden");

            return(uiCheckbox);
        }
示例#8
0
        public static UIKeyView Create(CreateParams 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;
                }
            }

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

            uiKeyView.relativeLocation        = input.relativeLocation;
            uiKeyView.transform.parent        = input.parent;
            uiKeyView.transform.localPosition = parentAnchor + input.relativeLocation;
            uiKeyView.transform.localRotation = Quaternion.identity;
            uiKeyView.transform.localScale    = Vector3.one;
            uiKeyView.width                     = input.width;
            uiKeyView.height                    = input.height;
            uiKeyView.margin                    = input.margin;
            uiKeyView.thickness                 = input.thickness;
            uiKeyView.source_material           = input.material;
            uiKeyView.baseColor.useConstant     = false;
            uiKeyView.baseColor.reference       = input.bgcolor;
            uiKeyView.textColor.useConstant     = false;
            uiKeyView.textColor.reference       = input.fgcolor;
            uiKeyView.pushedColor.useConstant   = false;
            uiKeyView.pushedColor.reference     = input.pushedColor;
            uiKeyView.selectedColor.useConstant = false;
            uiKeyView.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);
                uiKeyView.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);
                Material sharedMaterial = meshRenderer.sharedMaterial;
                meshRenderer.shadowCastingMode  = UnityEngine.Rendering.ShadowCastingMode.Off;
                meshRenderer.rendererPriority   = 1;
                meshRenderer.renderingLayerMask = 2; // "LightLayer 1"

                uiKeyView.SetColor(input.bgcolor.value);
            }

            UIUtils.SetRecursiveLayer(go, "CameraHidden");

            return(uiKeyView);
        }
示例#9
0
        public static void 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;
                }
            }

            UIVerticalSlider uiSlider = go.AddComponent <UIVerticalSlider>(); // 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.textValueAlign            = input.textValueAlign;
            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     = 2 * uiSlider.railMargin;
            float   railHeight    = (input.height - 2 * input.margin) * (input.sliderEnd - input.sliderBegin);
            float   railThickness = uiSlider.railThickness;
            float   railMargin    = uiSlider.railMargin;
            Vector3 railPosition  = new Vector3(input.width / 2.0f - railMargin, -input.height + input.margin + (input.height - 2 * input.margin) * uiSlider.sliderPositionEnd, -railThickness);

            uiSlider.rail = UIVerticalSliderRail.Create(
                new UIVerticalSliderRail.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 heightWithoutMargins = input.height - 2.0f * input.margin;
            float startY = -input.height + input.margin + heightWithoutMargins * uiSlider.sliderPositionBegin + railMargin;
            float endY   = -input.height + input.margin + heightWithoutMargins * uiSlider.sliderPositionEnd - railMargin;
            float posY   = startY + pct * (endY - startY);

            Vector3 knobPosition = new Vector3((input.width / 2.0f) - uiSlider.knobRadius, posY + uiSlider.knobRadius, -uiSlider.knobDepth);

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

            //
            // CANVAS (to hold the image)
            //

            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;      // top left

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

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

            float minSide = Mathf.Min(uiSlider.width, uiSlider.height);

            // Add an Image under the Canvas
            if (input.icon != null)
            {
                GameObject image = new GameObject("Image");
                image.transform.parent = canvas.transform;

                Image img = image.AddComponent <Image>();
                img.sprite = input.icon;
                img.color  = input.textColor.value;

                RectTransform trt = image.GetComponent <RectTransform>();
                trt.localScale    = 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
                // TODO: non square icons ratio...
                trt.sizeDelta     = new Vector2(minSide - 2.0f * input.margin, minSide - 2.0f * input.margin);
                trt.localPosition = new Vector3(input.margin, -input.margin, -0.001f); // top-left minus margins
            }

            // Text VALUE
            {
                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.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(5, uiSlider.knobRadius * 2.0f * 100.0f); // size = 5, enough to hold the 0.00 float.
                float textPosRight = -2.0f * uiSlider.margin;                            // TMP: au pif pour le moment
                trt.localPosition = new Vector3(textPosRight, knobPosition.y, -0.002f);
            }

            UIUtils.SetRecursiveLayer(go, "CameraHidden");
        }
示例#10
0
文件: UIPanel.cs 项目: ubisoft/vrtist
        public static UIPanel Create(CreatePanelParams 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;
                }
            }

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

            uiPanel.relativeLocation        = input.relativeLocation;
            uiPanel.transform.parent        = input.parent;
            uiPanel.transform.localPosition = parentAnchor + input.relativeLocation;
            uiPanel.transform.localRotation = Quaternion.identity;
            uiPanel.transform.localScale    = Vector3.one;
            uiPanel.width     = input.width;
            uiPanel.height    = input.height;
            uiPanel.margin    = input.margin;
            uiPanel.radius    = input.radius;
            uiPanel.thickness = input.thickness;
            uiPanel.backgroundGeometryStyle = input.backgroundGeometryStyle;
            uiPanel.source_material         = input.material;
            uiPanel.baseColor.useConstant   = false;
            uiPanel.baseColor.reference     = input.color;

            MeshFilter meshFilter = go.GetComponent <MeshFilter>();

            if (meshFilter != null)
            {
                meshFilter.sharedMesh =
                    (uiPanel.backgroundGeometryStyle == BackgroundGeometryStyle.Tube)
                        ? UIUtils.BuildRoundedRectTube(input.width, input.height, input.margin, input.radius)
                        : UIUtils.BuildRoundedBox(input.width, input.height, input.margin, input.thickness);

                uiPanel.Anchor = Vector3.zero; // TODO: thickness goes +Z and Anchor stays zero? or thickness goes -Z and Anchor follows the surface?

                BoxCollider coll = go.GetComponent <BoxCollider>();
                if (coll != null)
                {
                    Vector3 initColliderCenter = meshFilter.sharedMesh.bounds.center;
                    Vector3 initColliderSize   = meshFilter.sharedMesh.bounds.size;
                    coll.center    = initColliderCenter;
                    coll.size      = initColliderSize;
                    coll.isTrigger = true;
                }
            }

            MeshRenderer meshRenderer = go.GetComponent <MeshRenderer>();

            if (meshRenderer != null && input.material != null)
            {
                // Clone the material.
                meshRenderer.sharedMaterial = Instantiate(input.material);
                Material sharedMaterial = meshRenderer.sharedMaterial;
                meshRenderer.shadowCastingMode  = UnityEngine.Rendering.ShadowCastingMode.Off;
                meshRenderer.renderingLayerMask = 2; // "LightLayer 1"

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

            UIUtils.SetRecursiveLayer(go, "CameraHidden");

            return(uiPanel);
        }