/// <summary> /// Create the basic UI button. /// </summary> /// <remarks> /// Hierarchy: /// (root) /// Button /// -Text /// </remarks> /// <param name="resources">The resources to use for creation.</param> /// <returns>The root GameObject of the created element.</returns> public static GameObject CreateButton(Resources resources) { GameObject buttonRoot = CreateUIElementRoot("Button", s_ThickElementSize); GameObject childText = new GameObject("Text"); childText.AddComponent <RectTransform>(); SetParentAndAlign(childText, buttonRoot); Image image = buttonRoot.AddComponent <Image>(); image.sprite = resources.standard; image.type = Image.Type.Sliced; image.color = s_DefaultSelectableColor; Button bt = buttonRoot.AddComponent <Button>(); SetDefaultColorTransitionValues(bt); BUIText text = childText.AddComponent <BUIText>(); text.text = "Button"; text.alignment = TextAnchor.MiddleCenter; SetDefaultTextValues(text); RectTransform textRectTransform = childText.GetComponent <RectTransform>(); textRectTransform.anchorMin = Vector2.zero; textRectTransform.anchorMax = Vector2.one; textRectTransform.sizeDelta = Vector2.zero; return(buttonRoot); }
/// <summary> /// Create the basic UI Toggle. /// </summary> /// <remarks> /// Hierarchy: /// (root) /// Toggle /// - Background /// - Checkmark /// - Label /// </remarks> /// <param name="resources">The resources to use for creation.</param> /// <returns>The root GameObject of the created element.</returns> public static GameObject CreateToggle(Resources resources) { // Set up hierarchy GameObject toggleRoot = CreateUIElementRoot("Toggle", s_ThinElementSize); GameObject background = CreateUIObject("Background", toggleRoot); GameObject checkmark = CreateUIObject("Checkmark", background); GameObject childLabel = CreateUIObject("Label", toggleRoot); // Set up components Toggle toggle = toggleRoot.AddComponent <Toggle>(); toggle.isOn = true; Image bgImage = background.AddComponent <Image>(); bgImage.sprite = resources.standard; bgImage.type = Image.Type.Sliced; bgImage.color = s_DefaultSelectableColor; Image checkmarkImage = checkmark.AddComponent <Image>(); checkmarkImage.sprite = resources.checkmark; BUIText label = childLabel.AddComponent <BUIText>(); label.text = "Toggle"; SetDefaultTextValues(label); toggle.graphic = checkmarkImage; toggle.targetGraphic = bgImage; SetDefaultColorTransitionValues(toggle); RectTransform bgRect = background.GetComponent <RectTransform>(); bgRect.anchorMin = new Vector2(0f, 1f); bgRect.anchorMax = new Vector2(0f, 1f); bgRect.anchoredPosition = new Vector2(10f, -10f); bgRect.sizeDelta = new Vector2(kThinHeight, kThinHeight); RectTransform checkmarkRect = checkmark.GetComponent <RectTransform>(); checkmarkRect.anchorMin = new Vector2(0.5f, 0.5f); checkmarkRect.anchorMax = new Vector2(0.5f, 0.5f); checkmarkRect.anchoredPosition = Vector2.zero; checkmarkRect.sizeDelta = new Vector2(20f, 20f); RectTransform labelRect = childLabel.GetComponent <RectTransform>(); labelRect.anchorMin = new Vector2(0f, 0f); labelRect.anchorMax = new Vector2(1f, 1f); labelRect.offsetMin = new Vector2(23f, 1f); labelRect.offsetMax = new Vector2(-5f, -2f); return(toggleRoot); }
private static void SetDefaultTextValues(BUIText lbl) { // Set text values we want across UI elements in default controls. // Don't set values which are the same as the default values for the Text component, // since there's no point in that, and it's good to keep them as consistent as possible. lbl.color = s_TextColor; // Reset() is not called when playing. We still want the default font to be assigned lbl.font = UnityEngine.Resources.GetBuiltinResource <Font>("Arial.ttf"); }
/// <summary> /// Create the basic UI input field. /// </summary> /// <remarks> /// Hierarchy: /// (root) /// InputField /// - PlaceHolder /// - Text /// </remarks> /// <param name="resources">The resources to use for creation.</param> /// <returns>The root GameObject of the created element.</returns> public static GameObject CreateInputField(Resources resources) { GameObject root = CreateUIElementRoot("InputField", s_ThickElementSize); GameObject childPlaceholder = CreateUIObject("Placeholder", root); GameObject childText = CreateUIObject("Text", root); Image image = root.AddComponent <Image>(); image.sprite = resources.inputField; image.type = Image.Type.Sliced; image.color = s_DefaultSelectableColor; InputField inputField = root.AddComponent <InputField>(); SetDefaultColorTransitionValues(inputField); BUIText text = childText.AddComponent <BUIText>(); text.text = ""; text.supportRichText = false; SetDefaultTextValues(text); BUIText placeholder = childPlaceholder.AddComponent <BUIText>(); placeholder.text = "Enter text..."; placeholder.fontStyle = FontStyle.Italic; // Make placeholder color half as opaque as normal text color. Color placeholderColor = text.color; placeholderColor.a *= 0.5f; placeholder.color = placeholderColor; RectTransform textRectTransform = childText.GetComponent <RectTransform>(); textRectTransform.anchorMin = Vector2.zero; textRectTransform.anchorMax = Vector2.one; textRectTransform.sizeDelta = Vector2.zero; textRectTransform.offsetMin = new Vector2(10, 6); textRectTransform.offsetMax = new Vector2(-10, -7); RectTransform placeholderRectTransform = childPlaceholder.GetComponent <RectTransform>(); placeholderRectTransform.anchorMin = Vector2.zero; placeholderRectTransform.anchorMax = Vector2.one; placeholderRectTransform.sizeDelta = Vector2.zero; placeholderRectTransform.offsetMin = new Vector2(10, 6); placeholderRectTransform.offsetMax = new Vector2(-10, -7); inputField.textComponent = text; inputField.placeholder = placeholder; return(root); }
/// <summary> /// Create the basic UI Text. /// </summary> /// <remarks> /// Hierarchy: /// (root) /// Text /// </remarks> /// <param name="resources">The resources to use for creation.</param> /// <returns>The root GameObject of the created element.</returns> public static GameObject CreateText(Resources resources) { GameObject go = CreateUIElementRoot("BUIText", s_ThickElementSize); BUIText lbl = go.AddComponent <BUIText>(); lbl.text = "New Text"; SetDefaultTextValues(lbl); return(go); }
/// <summary> /// Create the basic UI dropdown. /// </summary> /// <remarks> /// Hierarchy: /// (root) /// Dropdown /// - Label /// - Arrow /// - Template /// - Viewport /// - Content /// - Item /// - Item Background /// - Item Checkmark /// - Item Label /// - Scrollbar /// - Sliding Area /// - Handle /// </remarks> /// <param name="resources">The resources to use for creation.</param> /// <returns>The root GameObject of the created element.</returns> public static GameObject CreateDropdown(Resources resources) { GameObject root = CreateUIElementRoot("Dropdown", s_ThickElementSize); GameObject label = CreateUIObject("Label", root); GameObject arrow = CreateUIObject("Arrow", root); GameObject template = CreateUIObject("Template", root); GameObject viewport = CreateUIObject("Viewport", template); GameObject content = CreateUIObject("Content", viewport); GameObject item = CreateUIObject("Item", content); GameObject itemBackground = CreateUIObject("Item Background", item); GameObject itemCheckmark = CreateUIObject("Item Checkmark", item); GameObject itemLabel = CreateUIObject("Item Label", item); // Sub controls. GameObject scrollbar = CreateScrollbar(resources); scrollbar.name = "Scrollbar"; SetParentAndAlign(scrollbar, template); Scrollbar scrollbarScrollbar = scrollbar.GetComponent <Scrollbar>(); scrollbarScrollbar.SetDirection(Scrollbar.Direction.BottomToTop, true); RectTransform vScrollbarRT = scrollbar.GetComponent <RectTransform>(); vScrollbarRT.anchorMin = Vector2.right; vScrollbarRT.anchorMax = Vector2.one; vScrollbarRT.pivot = Vector2.one; vScrollbarRT.sizeDelta = new Vector2(vScrollbarRT.sizeDelta.x, 0); // Setup item UI components. BUIText itemLabelText = itemLabel.AddComponent <BUIText>(); SetDefaultTextValues(itemLabelText); itemLabelText.alignment = TextAnchor.MiddleLeft; Image itemBackgroundImage = itemBackground.AddComponent <Image>(); itemBackgroundImage.color = new Color32(245, 245, 245, 255); Image itemCheckmarkImage = itemCheckmark.AddComponent <Image>(); itemCheckmarkImage.sprite = resources.checkmark; Toggle itemToggle = item.AddComponent <Toggle>(); itemToggle.targetGraphic = itemBackgroundImage; itemToggle.graphic = itemCheckmarkImage; itemToggle.isOn = true; // Setup template UI components. Image templateImage = template.AddComponent <Image>(); templateImage.sprite = resources.standard; templateImage.type = Image.Type.Sliced; ScrollRect templateScrollRect = template.AddComponent <ScrollRect>(); templateScrollRect.content = (RectTransform)content.transform; templateScrollRect.viewport = (RectTransform)viewport.transform; templateScrollRect.horizontal = false; templateScrollRect.movementType = ScrollRect.MovementType.Clamped; templateScrollRect.verticalScrollbar = scrollbarScrollbar; templateScrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport; templateScrollRect.verticalScrollbarSpacing = -3; Mask scrollRectMask = viewport.AddComponent <Mask>(); scrollRectMask.showMaskGraphic = false; Image viewportImage = viewport.AddComponent <Image>(); viewportImage.sprite = resources.mask; viewportImage.type = Image.Type.Sliced; // Setup dropdown UI components. BUIText labelText = label.AddComponent <BUIText>(); SetDefaultTextValues(labelText); labelText.alignment = TextAnchor.MiddleLeft; Image arrowImage = arrow.AddComponent <Image>(); arrowImage.sprite = resources.dropdown; Image backgroundImage = root.AddComponent <Image>(); backgroundImage.sprite = resources.standard; backgroundImage.color = s_DefaultSelectableColor; backgroundImage.type = Image.Type.Sliced; Dropdown dropdown = root.AddComponent <Dropdown>(); dropdown.targetGraphic = backgroundImage; SetDefaultColorTransitionValues(dropdown); dropdown.template = template.GetComponent <RectTransform>(); dropdown.captionText = labelText; dropdown.itemText = itemLabelText; // Setting default Item list. itemLabelText.text = "Option A"; dropdown.options.Add(new Dropdown.OptionData { text = "Option A" }); dropdown.options.Add(new Dropdown.OptionData { text = "Option B" }); dropdown.options.Add(new Dropdown.OptionData { text = "Option C" }); dropdown.RefreshShownValue(); // Set up RectTransforms. RectTransform labelRT = label.GetComponent <RectTransform>(); labelRT.anchorMin = Vector2.zero; labelRT.anchorMax = Vector2.one; labelRT.offsetMin = new Vector2(10, 6); labelRT.offsetMax = new Vector2(-25, -7); RectTransform arrowRT = arrow.GetComponent <RectTransform>(); arrowRT.anchorMin = new Vector2(1, 0.5f); arrowRT.anchorMax = new Vector2(1, 0.5f); arrowRT.sizeDelta = new Vector2(20, 20); arrowRT.anchoredPosition = new Vector2(-15, 0); RectTransform templateRT = template.GetComponent <RectTransform>(); templateRT.anchorMin = new Vector2(0, 0); templateRT.anchorMax = new Vector2(1, 0); templateRT.pivot = new Vector2(0.5f, 1); templateRT.anchoredPosition = new Vector2(0, 2); templateRT.sizeDelta = new Vector2(0, 150); RectTransform viewportRT = viewport.GetComponent <RectTransform>(); viewportRT.anchorMin = new Vector2(0, 0); viewportRT.anchorMax = new Vector2(1, 1); viewportRT.sizeDelta = new Vector2(-18, 0); viewportRT.pivot = new Vector2(0, 1); RectTransform contentRT = content.GetComponent <RectTransform>(); contentRT.anchorMin = new Vector2(0f, 1); contentRT.anchorMax = new Vector2(1f, 1); contentRT.pivot = new Vector2(0.5f, 1); contentRT.anchoredPosition = new Vector2(0, 0); contentRT.sizeDelta = new Vector2(0, 28); RectTransform itemRT = item.GetComponent <RectTransform>(); itemRT.anchorMin = new Vector2(0, 0.5f); itemRT.anchorMax = new Vector2(1, 0.5f); itemRT.sizeDelta = new Vector2(0, 20); RectTransform itemBackgroundRT = itemBackground.GetComponent <RectTransform>(); itemBackgroundRT.anchorMin = Vector2.zero; itemBackgroundRT.anchorMax = Vector2.one; itemBackgroundRT.sizeDelta = Vector2.zero; RectTransform itemCheckmarkRT = itemCheckmark.GetComponent <RectTransform>(); itemCheckmarkRT.anchorMin = new Vector2(0, 0.5f); itemCheckmarkRT.anchorMax = new Vector2(0, 0.5f); itemCheckmarkRT.sizeDelta = new Vector2(20, 20); itemCheckmarkRT.anchoredPosition = new Vector2(10, 0); RectTransform itemLabelRT = itemLabel.GetComponent <RectTransform>(); itemLabelRT.anchorMin = Vector2.zero; itemLabelRT.anchorMax = Vector2.one; itemLabelRT.offsetMin = new Vector2(20, 1); itemLabelRT.offsetMax = new Vector2(-10, -2); template.SetActive(false); return(root); }