Exemple #1
0
            /// <summary>
            ///   Creates a button, inside a parent, using specific coordinates (based on bottom-left) and size.
            /// </summary>
            /// <param name="parent">The parent under which the button will be located</param>
            /// <param name="position">The button's from-top-bottom position</param>
            /// <param name="size">The button's size</param>
            /// <param name="caption">The button's settings</param>
            /// <param name="name">The button's name</param>
            /// <param name="textColor">The button's text color</param>
            /// <param name="colors">The button's color settings</param>
            /// <param name="fontSize">Optional font size. Will default to half the height</param>
            /// <returns>The button being created, or <c>null</c> if arguments are negative or somehow inconsistent</returns>
            public static Button AddButton(RectTransform parent, Vector2 position, Vector2 size, string caption, string name, Color textColor, ColorBlock colors, int fontSize = 0)
            {
                if (size.x <= 0 || size.y <= 0 || position.x < 0 || position.y < 0)
                {
                    return(null);
                }
                GameObject buttonObject = new GameObject(name);

                buttonObject.transform.parent = parent;
                RectTransform rectTransformComponent = Behaviours.AddComponent <RectTransform>(buttonObject);

                rectTransformComponent.pivot      = Vector2.zero;
                rectTransformComponent.anchorMin  = Vector2.zero;
                rectTransformComponent.anchorMax  = Vector2.zero;
                rectTransformComponent.offsetMin  = position;
                rectTransformComponent.offsetMax  = position;
                rectTransformComponent.sizeDelta  = size;
                rectTransformComponent.localScale = Vector3.one;
                Image buttonImageComponent = Behaviours.AddComponent <Image>(buttonObject);

                buttonImageComponent.sprite     = AssetDatabase.GetBuiltinExtraResource <Sprite>("UI/Skin/UISprite.psd");
                buttonImageComponent.type       = Image.Type.Sliced;
                buttonImageComponent.fillCenter = true;
                Button buttonComponent = Behaviours.AddComponent <Button>(buttonObject);

                buttonComponent.colors        = colors;
                buttonComponent.targetGraphic = buttonImageComponent;
                GameObject textObject = new GameObject("Text");

                textObject.transform.parent = buttonObject.transform;
                Text textComponent = Behaviours.AddComponent <Text>(textObject);

                textComponent.text      = caption;
                textComponent.fontSize  = (fontSize >= 0) ? fontSize : (int)(size.y / 2);
                textComponent.alignment = TextAnchor.MiddleCenter;
                textComponent.color     = textColor;
                RectTransform textRectTransform = textObject.GetComponent <RectTransform>();

                textRectTransform.pivot      = Vector2.one / 2f;
                textRectTransform.anchorMin  = Vector2.zero;
                textRectTransform.anchorMax  = Vector2.one;
                textRectTransform.offsetMin  = Vector2.zero;
                textRectTransform.offsetMax  = Vector2.zero;
                textRectTransform.localScale = Vector3.one;
                return(buttonComponent);
            }