Пример #1
0
        /// <summary>
        /// Creates the list and it's options.
        /// </summary>
        protected void CreateList()
        {
            // Get the root canvas
            Canvas rootCanvas = UIUtility.FindInParents <Canvas>(this.gameObject);

            // Reset the last list size
            this.m_LastListSize = Vector2.zero;

            // Clear the option texts list
            this.m_OptionObjects.Clear();

            // Create the list game object with the necessary components
            this.m_ListObject       = new GameObject("UISelectField - List", typeof(RectTransform));
            this.m_ListObject.layer = this.gameObject.layer;

            // Change the parent of the list
            this.m_ListObject.transform.SetParent(this.transform, false);

            // Get the select field list component
            UISelectField_List listComp = this.m_ListObject.AddComponent <UISelectField_List>();

            // Make sure it's the top-most element
            UIAlwaysOnTop aot = this.m_ListObject.AddComponent <UIAlwaysOnTop>();

            aot.order = UIAlwaysOnTop.SelectFieldOrder;

            // Get the list canvas group component
            this.m_ListCanvasGroup = this.m_ListObject.AddComponent <CanvasGroup>();

            // Change the anchor and pivot of the list
            RectTransform rect = (this.m_ListObject.transform as RectTransform);

            rect.localScale    = new Vector3(1f, 1f, 1f);
            rect.localPosition = Vector3.zero;
            rect.anchorMin     = Vector2.zero;
            rect.anchorMax     = Vector2.zero;
            rect.pivot         = new Vector2(0f, 1f);

            // Prepare the position of the list
            rect.anchoredPosition = new Vector3(this.listMargins.left, (this.listMargins.top * -1f), 0f);

            // Prepare the width of the list
            float width = (this.transform as RectTransform).sizeDelta.x;

            if (this.listMargins.left > 0)
            {
                width -= this.listMargins.left;
            }
            else
            {
                width += Math.Abs(this.listMargins.left);
            }
            if (this.listMargins.right > 0)
            {
                width -= this.listMargins.right;
            }
            else
            {
                width += Math.Abs(this.listMargins.right);
            }
            rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);

            // Hook the Dimensions Change event
            listComp.onDimensionsChange.AddListener(ListDimensionsChanged);

            // Apply the background sprite
            Image image = this.m_ListObject.AddComponent <Image>();

            if (this.listBackgroundSprite != null)
            {
                image.sprite = this.listBackgroundSprite;
            }
            image.type  = this.listBackgroundSpriteType;
            image.color = this.listBackgroundColor;

            // Prepare the vertical layout group
            VerticalLayoutGroup layoutGroup = this.m_ListObject.AddComponent <VerticalLayoutGroup>();

            layoutGroup.padding = this.listPadding;
            layoutGroup.spacing = this.listSpacing;

            // Prepare the content size fitter
            ContentSizeFitter fitter = this.m_ListObject.AddComponent <ContentSizeFitter>();

            fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;

            // Get the list toggle group
            ToggleGroup toggleGroup = this.m_ListObject.AddComponent <ToggleGroup>();

            // Create the options
            for (int i = 0; i < this.options.Count; i++)
            {
                if (i == 0 && this.startSeparator)
                {
                    this.m_StartSeparatorObject = this.CreateSeparator(i - 1);
                }

                // Create the option
                this.CreateOption(i, toggleGroup);

                // Create a separator if this is not the last option
                if (i < (this.options.Count - 1))
                {
                    this.CreateSeparator(i);
                }
            }

            // Prepare the list for the animation
            if (this.listAnimationType == ListAnimationType.None || this.listAnimationType == ListAnimationType.Fade)
            {
                // Starting alpha should be zero
                this.m_ListCanvasGroup.alpha = 0f;
            }
            else if (this.listAnimationType == ListAnimationType.Animation)
            {
                // Attach animator component
                Animator animator = this.m_ListObject.AddComponent <Animator>();

                // Set the animator controller
                animator.runtimeAnimatorController = this.listAnimatorController;

                // Set the animation triggers so we can use them to detect when animations finish
                listComp.SetTriggers(this.listAnimationOpenTrigger, this.listAnimationCloseTrigger);

                // Hook a callback on the finish event
                listComp.onAnimationFinish.AddListener(OnListAnimationFinish);
            }

            // Check if the navigation is disabled
            if (this.navigation.mode == Navigation.Mode.None)
            {
                this.CreateBlocker(rootCanvas);
            }
        }
        /// <summary>
        /// Creates the list and it's options.
        /// </summary>
        protected void CreateList()
        {
            // Reset the last list size
            this.m_LastListSize = Vector2.zero;

            // Clear the option texts list
            this.m_OptionObjects.Clear();

            // Create the list game object with the necessary components
            this.m_ListObject = new GameObject("UISelectField - List", typeof(RectTransform));

            // Change the parent of the list
            this.m_ListObject.transform.SetParent(this.transform, false);

            // Get the select field list component
            UISelectField_List listComp = this.m_ListObject.AddComponent <UISelectField_List>();

            // Get the list canvas group component
            this.m_ListCanvasGroup = this.m_ListObject.AddComponent <CanvasGroup>();

            // Change the anchor and pivot of the list
            RectTransform rect = (this.m_ListObject.transform as RectTransform);

            rect.anchorMin = Vector2.zero;
            rect.anchorMax = Vector2.zero;
            rect.pivot     = new Vector2(0f, 1f);

            // Prepare the position of the list
            rect.anchoredPosition = new Vector3((float)this.listMargins.left, ((float)this.listMargins.top * -1f), 0f);

            // Prepare the width of the list
            rect.sizeDelta = new Vector2((this.targetGraphic.rectTransform.sizeDelta.x - (this.listMargins.left + this.listMargins.right)), 0f);

            // Hook the Dimensions Change event
            listComp.onDimensionsChange.AddListener(ListDimensionsChanged);

            // Apply the background sprite
            Image image = this.m_ListObject.AddComponent <Image>();

            if (this.listBackgroundSprite != null)
            {
                image.sprite = this.listBackgroundSprite;
            }
            image.type  = this.listBackgroundSpriteType;
            image.color = this.listBackgroundColor;

            // Prepare the vertical layout group
            VerticalLayoutGroup layoutGroup = this.m_ListObject.AddComponent <VerticalLayoutGroup>();

            layoutGroup.padding = this.listPadding;
            layoutGroup.spacing = this.listSpacing;

            // Prepare the content size fitter
            ContentSizeFitter fitter = this.m_ListObject.AddComponent <ContentSizeFitter>();

            fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;

            // Get the list toggle group
            ToggleGroup toggleGroup = this.m_ListObject.AddComponent <ToggleGroup>();

            // Create the options
            for (int i = 0; i < this.options.Count; i++)
            {
                // Create the option
                this.CreateOption(i, toggleGroup);

                // Create a separator if this is not the last option
                if (i < (this.options.Count - 1))
                {
                    this.CreateSeparator(i);
                }
            }

            // Prepare the list for the animation
            if (this.listAnimationType == ListAnimationType.None || this.listAnimationType == ListAnimationType.Fade)
            {
                // Starting alpha should be zero
                this.m_ListCanvasGroup.alpha = 0f;
            }
            else if (this.listAnimationType == ListAnimationType.Animation)
            {
                // Attach animator component
                Animator animator = this.m_ListObject.AddComponent <Animator>();

                // Set the animator controller
                animator.runtimeAnimatorController = this.listAnimatorController;

                // Set the animation triggers so we can use them to detect when animations finish
                listComp.SetTriggers(this.listAnimationOpenTrigger, this.listAnimationCloseTrigger);

                // Hook a callback on the finish event
                listComp.onAnimationFinish.AddListener(OnListAnimationFinish);
            }
        }