UIBuilder.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using UnityEngine;
  2. using System.Linq;
  3. using UnityEngine.UI;
  4. using TMPro;
  5. namespace SongBrowserPlugin
  6. {
  7. public static class UIBuilder
  8. {
  9. static public Button CreateUIButton(RectTransform parent, string buttonTemplate, Button buttonInstance)
  10. {
  11. if (buttonInstance == null)
  12. {
  13. return null;
  14. }
  15. Button btn = UnityEngine.Object.Instantiate(Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == buttonTemplate)), parent, false);
  16. UnityEngine.Object.DestroyImmediate(btn.GetComponent<GameEventOnUIButtonClick>());
  17. btn.onClick = new Button.ButtonClickedEvent();
  18. return btn;
  19. }
  20. static public TextMeshProUGUI CreateText(RectTransform parent, string text, Vector2 position)
  21. {
  22. TextMeshProUGUI textMesh = new GameObject("TextMeshProUGUI_GO").AddComponent<TextMeshProUGUI>();
  23. textMesh.rectTransform.SetParent(parent, false);
  24. textMesh.text = text;
  25. textMesh.fontSize = 4;
  26. textMesh.color = Color.white;
  27. textMesh.font = Resources.Load<TMP_FontAsset>("Teko-Medium SDF No Glow");
  28. textMesh.rectTransform.anchorMin = new Vector2(0.5f, 1f);
  29. textMesh.rectTransform.anchorMax = new Vector2(0.5f, 1f);
  30. textMesh.rectTransform.sizeDelta = new Vector2(60f, 10f);
  31. textMesh.rectTransform.anchoredPosition = position;
  32. return textMesh;
  33. }
  34. static public void SetButtonText(ref Button button, string text)
  35. {
  36. if (button.GetComponentInChildren<TextMeshProUGUI>() != null)
  37. {
  38. button.GetComponentInChildren<TextMeshProUGUI>().text = text;
  39. }
  40. }
  41. static public void SetButtonTextSize(ref Button button, float fontSize)
  42. {
  43. if (button.GetComponentInChildren<TextMeshProUGUI>() != null)
  44. {
  45. button.GetComponentInChildren<TextMeshProUGUI>().fontSize = fontSize;
  46. }
  47. }
  48. static public void SetButtonIcon(ref Button button, Sprite icon)
  49. {
  50. if (button.GetComponentsInChildren<UnityEngine.UI.Image>().Count() > 1)
  51. {
  52. button.GetComponentsInChildren<UnityEngine.UI.Image>()[1].sprite = icon;
  53. }
  54. }
  55. static public void SetButtonIconEnabled(ref Button button, bool enabled)
  56. {
  57. if (button.GetComponentsInChildren<UnityEngine.UI.Image>().Count() > 1)
  58. {
  59. button.GetComponentsInChildren<UnityEngine.UI.Image>()[1].enabled = enabled;
  60. }
  61. }
  62. static public void SetButtonBackground(ref Button button, Sprite background)
  63. {
  64. if (button.GetComponentsInChildren<Image>().Any())
  65. {
  66. button.GetComponentsInChildren<UnityEngine.UI.Image>()[0].sprite = background;
  67. }
  68. }
  69. static public void SetButtonBorder(ref Button button, Color color)
  70. {
  71. if (button.GetComponentsInChildren<Image>().Any())
  72. {
  73. button.GetComponentsInChildren<UnityEngine.UI.Image>()[0].color = color;
  74. }
  75. }
  76. }
  77. }