/// <summary>
        /// Instantiates a button.
        /// </summary>
        /// <param name="name">The name of the button.</param>
        /// <param name="parent">The parent of the new button.</param>
        /// <param name="args">Instantiation arguments.</param>
        /// <returns>The button.</returns>
        public static GameObject GenerateButton(Transform parent, ButtonArgs args)
        {
            var button = new GameObject(args.Name);

            button.transform.SetParent(parent, false);

            var rect = button.AddComponent <RectTransform>();

            rect.pivot            = args.Pivot;
            rect.anchorMin        = args.AnchorMin;
            rect.anchorMax        = args.AnchorMax;
            rect.sizeDelta        = args.Size;
            rect.anchoredPosition = args.Position;

            var script = button.AddComponent <Button>();

            script.Toggleable     = args.Toggleable;
            script.DefaultColor   = args.DefaultColor;
            script.MouseOverColor = args.MouseOverColor;
            script.SelectedColor  = args.SelectedColor;
            script.OnSelect       = args.OnSelect;
            script.OnDeselect     = args.OnDeselect;
            script.Tooltip        = args.Tooltip;

            foreach (var child in args.Children ?? new Button[0])
            {
                child.SelectionParent = script;
            }

            if (args.IconImage != null)
            {
                var icon = new GameObject("icon");
                icon.transform.SetParent(button.transform, false);

                var iconRect = icon.AddComponent <RectTransform>();
                iconRect.sizeDelta = new Vector2(args.IconImage.rect.width, args.IconImage.rect.height);

                var iconImage = icon.AddComponent <Image>();
                iconImage.sprite        = args.IconImage;
                iconImage.raycastTarget = false;

                script.IconImage = iconImage;
            }

            return(button);
        }
示例#2
0
        /// <summary>
        /// Convert Button GameData into arguments for the Factory method.
        /// Called by CreateButtonGroup.
        /// </summary>
        private ButtonArgs[] CreateButtonArgs(List <ButtonData> buttons, UIConfig config)
        {
            ButtonArgs[] buttonArgs = new ButtonArgs[buttons.Count];
            for (int i = 0; i < buttonArgs.Length; ++i)
            {
                ButtonData button = buttons[i];

                buttonArgs[i] = new ButtonArgs()
                {
                    Name      = button.Name,
                    Tooltip   = button.Tooltip,
                    IconImage = button.IconImage,
                    // OnSelect and OnDeselect linked during second pass construction
                };
            }

            return(buttonArgs);
        }