Example #1
0
        /// <summary>
        /// Selects the option by index.
        /// </summary>
        /// <param name="optionIndex">Option index.</param>
        public void SelectOptionByIndex(int index)
        {
            // If the list is open, use the toggle to select the option
            if (this.IsOpen)
            {
                UISelectField_Option option = this.m_OptionObjects[index];

                if (option != null)
                {
                    option.isOn = true;
                }
            }
            else             // otherwise set as selected
            {
                // Set as selected
                this.m_SelectedItem = this.options[index];

                // Trigger change
                this.TriggerChangeEvent();
            }
        }
Example #2
0
        /// <summary>
        /// Creates a option.
        /// </summary>
        /// <param name="index">Index.</param>
        protected void CreateOption(int index, ToggleGroup toggleGroup)
        {
            if (this.m_ListObject == null)
            {
                return;
            }

            // Create the option game object with it's components
            GameObject optionObject = new GameObject("Option " + index.ToString(), typeof(RectTransform));

            optionObject.layer = this.gameObject.layer;

            // Change parents
            optionObject.transform.SetParent(this.m_ListObject.transform, false);
            optionObject.transform.localScale    = new Vector3(1f, 1f, 1f);
            optionObject.transform.localPosition = Vector3.zero;

            // Get the option component
            UISelectField_Option optionComp = optionObject.AddComponent <UISelectField_Option>();

            // Prepare the option background
            if (this.optionBackgroundSprite != null)
            {
                Image image = optionObject.AddComponent <Image>();
                image.sprite = this.optionBackgroundSprite;
                image.type   = this.optionBackgroundSpriteType;
                image.color  = this.optionBackgroundSpriteColor;

                // Add the graphic as the option transition target
                optionComp.targetGraphic = image;
            }

            // Prepare the option for animation
            if (this.optionBackgroundTransitionType == Transition.Animation)
            {
                // Attach animator component
                Animator animator = optionObject.AddComponent <Animator>();

                // Set the animator controller
                animator.runtimeAnimatorController = this.optionBackgroundAnimatorController;
            }

            // Apply the option padding
            VerticalLayoutGroup vlg = optionObject.AddComponent <VerticalLayoutGroup>();

            vlg.padding = this.optionPadding;

            // Create the option text
            GameObject textObject = new GameObject("Label", typeof(RectTransform));

            // Change parents
            textObject.transform.SetParent(optionObject.transform, false);
            textObject.transform.localScale    = Vector3.one;
            textObject.transform.localPosition = Vector3.zero;

            // Apply pivot
            (textObject.transform as RectTransform).pivot = new Vector2(0f, 1f);

            // Prepare the text
            Text text = textObject.AddComponent <Text>();

            text.font      = this.optionFont;
            text.fontSize  = this.optionFontSize;
            text.fontStyle = this.optionFontStyle;
            text.color     = this.optionColor;

            if (this.options != null)
            {
                text.text = this.options[index];
            }

            // Apply normal state transition color
            if (this.optionTextTransitionType == OptionTextTransitionType.CrossFade)
            {
                text.canvasRenderer.SetColor(this.optionTextTransitionColors.normalColor);
            }

            // Add and prepare the text effect
            if (this.optionTextEffectType != OptionTextEffectType.None)
            {
                if (this.optionTextEffectType == OptionTextEffectType.Shadow)
                {
                    Shadow effect = textObject.AddComponent <Shadow>();
                    effect.effectColor     = this.optionTextEffectColor;
                    effect.effectDistance  = this.optionTextEffectDistance;
                    effect.useGraphicAlpha = this.optionTextEffectUseGraphicAlpha;
                }
                else if (this.optionTextEffectType == OptionTextEffectType.Outline)
                {
                    Outline effect = textObject.AddComponent <Outline>();
                    effect.effectColor     = this.optionTextEffectColor;
                    effect.effectDistance  = this.optionTextEffectDistance;
                    effect.useGraphicAlpha = this.optionTextEffectUseGraphicAlpha;
                }
            }

            // Prepare the option hover overlay
            if (this.optionHoverOverlay != null)
            {
                GameObject hoverOverlayObj = new GameObject("Hover Overlay", typeof(RectTransform));
                hoverOverlayObj.layer = this.gameObject.layer;
                hoverOverlayObj.transform.localScale    = Vector3.one;
                hoverOverlayObj.transform.localPosition = Vector3.zero;

                // Add layout element
                LayoutElement hoverLayoutElement = hoverOverlayObj.AddComponent <LayoutElement>();
                hoverLayoutElement.ignoreLayout = true;

                // Change parents
                hoverOverlayObj.transform.SetParent(optionObject.transform, false);
                hoverOverlayObj.transform.localScale = new Vector3(1f, 1f, 1f);

                // Add image
                Image hoImage = hoverOverlayObj.AddComponent <Image>();
                hoImage.sprite = this.optionHoverOverlay;
                hoImage.color  = this.optionHoverOverlayColor;
                hoImage.type   = Image.Type.Sliced;

                // Apply pivot
                (hoverOverlayObj.transform as RectTransform).pivot = new Vector2(0f, 1f);

                // Apply anchors
                (hoverOverlayObj.transform as RectTransform).anchorMin = new Vector2(0f, 0f);
                (hoverOverlayObj.transform as RectTransform).anchorMax = new Vector2(1f, 1f);

                // Apply offsets
                (hoverOverlayObj.transform as RectTransform).offsetMin = new Vector2(0f, 0f);
                (hoverOverlayObj.transform as RectTransform).offsetMax = new Vector2(0f, 0f);

                // Add the highlight transition component
                UISelectField_OptionOverlay hoht = optionObject.AddComponent <UISelectField_OptionOverlay>();
                hoht.targetGraphic = hoImage;
                hoht.transition    = UISelectField_OptionOverlay.Transition.ColorTint;
                hoht.colorBlock    = this.optionHoverOverlayColorBlock;
                hoht.InternalEvaluateAndTransitionToNormalState(true);
            }

            // Prepare the option press overlay
            if (this.optionPressOverlay != null)
            {
                GameObject pressOverlayObj = new GameObject("Press Overlay", typeof(RectTransform));
                pressOverlayObj.layer = this.gameObject.layer;
                pressOverlayObj.transform.localScale    = Vector3.one;
                pressOverlayObj.transform.localPosition = Vector3.zero;

                // Add layout element
                LayoutElement pressLayoutElement = pressOverlayObj.AddComponent <LayoutElement>();
                pressLayoutElement.ignoreLayout = true;

                // Change parents
                pressOverlayObj.transform.SetParent(optionObject.transform, false);
                pressOverlayObj.transform.localScale = new Vector3(1f, 1f, 1f);

                // Add image
                Image poImage = pressOverlayObj.AddComponent <Image>();
                poImage.sprite = this.optionPressOverlay;
                poImage.color  = this.optionPressOverlayColor;
                poImage.type   = Image.Type.Sliced;

                // Apply pivot
                (pressOverlayObj.transform as RectTransform).pivot = new Vector2(0f, 1f);

                // Apply anchors
                (pressOverlayObj.transform as RectTransform).anchorMin = new Vector2(0f, 0f);
                (pressOverlayObj.transform as RectTransform).anchorMax = new Vector2(1f, 1f);

                // Apply offsets
                (pressOverlayObj.transform as RectTransform).offsetMin = new Vector2(0f, 0f);
                (pressOverlayObj.transform as RectTransform).offsetMax = new Vector2(0f, 0f);

                // Add the highlight transition component
                UISelectField_OptionOverlay poht = optionObject.AddComponent <UISelectField_OptionOverlay>();
                poht.targetGraphic = poImage;
                poht.transition    = UISelectField_OptionOverlay.Transition.ColorTint;
                poht.colorBlock    = this.optionPressOverlayColorBlock;
                poht.InternalEvaluateAndTransitionToNormalState(true);
            }

            // Initialize the option component
            optionComp.Initialize(this, text);

            // Set active if it's the selected one
            if (index == this.selectedOptionIndex)
            {
                optionComp.isOn = true;
            }

            // Register to the toggle group
            if (toggleGroup != null)
            {
                optionComp.group = toggleGroup;
            }

            // Hook some events
            optionComp.onSelectOption.AddListener(OnOptionSelect);
            optionComp.onPointerUp.AddListener(OnOptionPointerUp);

            // Add it to the list
            if (this.m_OptionObjects != null)
            {
                this.m_OptionObjects.Add(optionComp);
            }
        }
        /// <summary>
        /// Creates a option.
        /// </summary>
        /// <param name="index">Index.</param>
        protected void CreateOption(int index, ToggleGroup toggleGroup)
        {
            if (this.m_ListObject == null)
            {
                return;
            }

            // Create the option game object with it's components
            GameObject optionObject = new GameObject("Option " + index.ToString(), typeof(RectTransform));

            // Change parents
            optionObject.transform.SetParent(this.m_ListObject.transform, false);

            // Get the option component
            UISelectField_Option optionComp = optionObject.AddComponent <UISelectField_Option>();

            // Prepare the option background
            if (this.optionBackgroundSprite != null)
            {
                Image image = optionObject.AddComponent <Image>();
                image.sprite = this.optionBackgroundSprite;
                image.type   = this.optionBackgroundSpriteType;
                image.color  = this.optionBackgroundSpriteColor;

                // Add the graphic as the option transition target
                optionComp.targetGraphic = image;
            }

            // Prepare the option for animation
            if (this.optionBackgroundTransitionType == Transition.Animation)
            {
                // Attach animator component
                Animator animator = optionObject.AddComponent <Animator>();

                // Set the animator controller
                animator.runtimeAnimatorController = this.optionBackgroundAnimatorController;
            }

            // Apply the option padding
            VerticalLayoutGroup vlg = optionObject.AddComponent <VerticalLayoutGroup>();

            vlg.padding = this.optionPadding;

            // Create the option text
            GameObject textObject = new GameObject("Label", typeof(RectTransform));

            // Change parents
            textObject.transform.SetParent(optionObject.transform, false);

            // Apply pivot
            (textObject.transform as RectTransform).pivot = new Vector2(0f, 1f);

            // Prepare the text
            Text text = textObject.AddComponent <Text>();

            text.font      = this.optionFont;
            text.fontSize  = this.optionFontSize;
            text.fontStyle = this.optionFontStyle;
            text.color     = this.optionColor;

            if (this.options != null)
            {
                text.text = this.options[index];
            }

            // Apply normal state transition color
            if (this.optionTextTransitionType == OptionTextTransitionType.CrossFade)
            {
                text.canvasRenderer.SetColor(this.optionTextTransitionColors.normalColor);
            }

            // Add and prepare the text effect
            if (this.optionTextEffectType != OptionTextEffectType.None)
            {
                if (this.optionTextEffectType == OptionTextEffectType.Shadow)
                {
                    Shadow effect = textObject.AddComponent <Shadow>();
                    effect.effectColor     = this.optionTextEffectColor;
                    effect.effectDistance  = this.optionTextEffectDistance;
                    effect.useGraphicAlpha = this.optionTextEffectUseGraphicAlpha;
                }
                else if (this.optionTextEffectType == OptionTextEffectType.Outline)
                {
                    Outline effect = textObject.AddComponent <Outline>();
                    effect.effectColor     = this.optionTextEffectColor;
                    effect.effectDistance  = this.optionTextEffectDistance;
                    effect.useGraphicAlpha = this.optionTextEffectUseGraphicAlpha;
                }
            }

            // Initialize the option component
            optionComp.Initialize(this, text);

            // Set active if it's the selected one
            if (index == this.selectedOptionIndex)
            {
                optionComp.isOn = true;
            }

            // Register to the toggle group
            if (toggleGroup != null)
            {
                optionComp.group = toggleGroup;
            }

            // Hook some events
            optionComp.onSelectOption.AddListener(OnOptionSelect);
            optionComp.onPointerUp.AddListener(OnOptionPointerUp);

            // Add it to the list
            if (this.m_OptionObjects != null)
            {
                this.m_OptionObjects.Add(optionComp);
            }
        }