/// <summary> /// Creates a copy of a template button and returns it. /// </summary> /// <param name="parent">The transform to parent the button to.</param> /// <param name="buttonTemplate">The name of the button to make a copy of. Example: "QuitButton", "PlayButton", etc.</param> /// <param name="anchoredPosition">The position the button should be anchored to.</param> /// <param name="sizeDelta">The size of the buttons RectTransform.</param> /// <param name="onClick">Callback for when the button is pressed.</param> /// <param name="buttonText">The text that should be shown on the button.</param> /// <param name="icon">The icon that should be shown on the button.</param> /// <returns>The newly created button.</returns> public static Button CreateUIButton(String name, RectTransform parent, string buttonTemplate, Vector2 anchoredPosition, Vector2 sizeDelta, UnityAction onClick = null, string buttonText = "BUTTON") { Logger.Debug("CreateUIButton({0}, {1}, {2}, {3}, {4}", name, parent, buttonTemplate, anchoredPosition, sizeDelta); Button btn = CreateBaseButton(name, parent, buttonTemplate); btn.gameObject.SetActive(true); Polyglot.LocalizedTextMeshProUGUI localizer = btn.GetComponentInChildren <Polyglot.LocalizedTextMeshProUGUI>(); if (localizer != null) { GameObject.Destroy(localizer); } BeatSaberMarkupLanguage.Components.ExternalComponents externalComponents = btn.gameObject.AddComponent <BeatSaberMarkupLanguage.Components.ExternalComponents>(); TextMeshProUGUI textMesh = btn.GetComponentInChildren <TextMeshProUGUI>(); textMesh.richText = true; externalComponents.components.Add(textMesh); var contentTransform = btn.transform.Find("Content").GetComponent <LayoutElement>(); if (contentTransform != null) { GameObject.Destroy(contentTransform); } ContentSizeFitter buttonSizeFitter = btn.gameObject.AddComponent <ContentSizeFitter>(); buttonSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; buttonSizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained; LayoutGroup stackLayoutGroup = btn.GetComponentInChildren <LayoutGroup>(); if (stackLayoutGroup != null) { externalComponents.components.Add(stackLayoutGroup); } btn.onClick.RemoveAllListeners(); if (onClick != null) { btn.onClick.AddListener(onClick); } (btn.transform as RectTransform).anchorMin = new Vector2(0.5f, 0.5f); (btn.transform as RectTransform).anchorMax = new Vector2(0.5f, 0.5f); (btn.transform as RectTransform).anchoredPosition = anchoredPosition; (btn.transform as RectTransform).sizeDelta = sizeDelta; btn.SetButtonText(buttonText); return(btn); }
public KEY(KEYBOARD kb, Vector2 position, string text, float width, float height, Color color) { value = text; this.kb = kb; name = text; mybutton = Button.Instantiate(kb.BaseButton, kb.container, false); GameObject.Destroy(mybutton.GetComponent <UIKeyboardKey>()); Polyglot.LocalizedTextMeshProUGUI localizer = mybutton.GetComponentInChildren <Polyglot.LocalizedTextMeshProUGUI>(true); if (localizer != null) { GameObject.Destroy(localizer); } ExternalComponents externalComponents = mybutton.gameObject.AddComponent <ExternalComponents>(); TextMeshProUGUI textMesh = mybutton.GetComponentInChildren <TextMeshProUGUI>(); textMesh.richText = true; externalComponents.components.Add(textMesh); (mybutton.transform as RectTransform).anchorMin = new Vector2(0.5f, 0.5f); (mybutton.transform as RectTransform).anchorMax = new Vector2(0.5f, 0.5f); TMP_Text txt = mybutton.GetComponentInChildren <TMP_Text>(); mybutton.ToggleWordWrapping(false); mybutton.transform.localScale = new Vector3(kb.scale, kb.scale, 1.0f); mybutton.SetButtonTextSize(5f); mybutton.SetButtonText(text); mybutton.GetComponentInChildren <Image>().color = color; if (width == 0) { Vector2 v = txt.GetPreferredValues(text); v.x += 10f; v.y += 2f; width = v.x; } // Adjust starting position so button aligns to upper left of current drawing position position.x += kb.scale * width / 2; position.y -= kb.scale * height / 2; (mybutton.transform as RectTransform).anchoredPosition = position; (mybutton.transform as RectTransform).sizeDelta = new Vector2(width, height); kb.currentposition.x += width * kb.scale + kb.padding; mybutton.onClick.RemoveAllListeners(); mybutton.onClick.AddListener(delegate() { if (keyaction != null) { keyaction(this); kb.DrawCursor(); return; } if (value.EndsWith("%CR%")) { kb.KeyboardText.text += value.Substring(0, value.Length - 4); kb.Enter(this); kb.DrawCursor(); return; } string x = kb.Shift ? shifted : value; if (x == "") { x = value; } if (kb.Caps) { x = value.ToUpper(); } kb.KeyboardText.text += x; kb.DrawCursor(); }); HoverHint _MyHintText = mybutton.gameObject.AddComponent <HoverHint>(); _MyHintText.text = value; _MyHintText.SetField("_hoverHintController", Resources.FindObjectsOfTypeAll <HoverHintController>().First()); }