using System; using System.Collections.Generic; using System.Linq; using System.Text; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace ExplorerBeta.UI { public static class UIFactory { private static Vector2 s_ThickElementSize = new Vector2(160f, 30f); private static Vector2 s_ThinElementSize = new Vector2(160f, 20f); private static Color s_DefaultSelectableColor = new Color(1f, 1f, 1f, 1f); private static Color s_TextColor = new Color(0.95f, 0.95f, 0.95f, 1f); //private static Color s_PanelColor = new Color(0.1f, 0.1f, 0.1f, 1.0f); //private static Vector2 s_ImageElementSize = new Vector2(100f, 100f); public static Resources UIResources { get; set; } public struct Resources { public Sprite standard; public Sprite background; public Sprite inputField; public Sprite knob; public Sprite checkmark; public Sprite dropdown; public Sprite mask; } public static GameObject CreateUIObject(string name, GameObject parent, Vector2 size = default) { GameObject obj = new GameObject(name); var rect = obj.AddComponent(); if (size != default) { rect.sizeDelta = size; } SetParentAndAlign(obj, parent); return obj; } private static void SetDefaultTextValues(Text lbl) { lbl.color = s_TextColor; lbl.AssignDefaultFont(); //lbl.alignment = alignment; //lbl.resizeTextForBestFit = true; } private static void SetDefaultColorTransitionValues(Selectable selectable) { ColorBlock colors = selectable.colors; colors.normalColor = new Color(0.4f, 0.4f, 0.4f); colors.highlightedColor = new Color(0.45f, 0.45f, 0.45f); colors.pressedColor = new Color(0.1f, 0.1f, 0.1f); colors.disabledColor = new Color(0.7f, 0.7f, 0.7f); // fix to make all buttons become de-selected after being clicked. // this is because i'm not setting any ColorBlock.selectedColor, because it is commonly stripped. if (selectable is Button button) { button.onClick.AddListener(new Action(() => { button.OnDeselect(EventSystem.current?.baseEventDataCache); })); } selectable.colors = colors; } private static void SetParentAndAlign(GameObject child, GameObject parent) { if (parent == null) { return; } child.transform.SetParent(parent.transform, false); SetLayerRecursively(child); } public static void SetLayerRecursively(GameObject go) { go.layer = 5; Transform transform = go.transform; for (int i = 0; i < transform.childCount; i++) { SetLayerRecursively(transform.GetChild(i).gameObject); } } public static GameObject CreatePanel(GameObject parent, string name, out GameObject content) { GameObject panelObj = CreateUIObject($"Panel_{name}", parent, s_ThickElementSize); RectTransform rect = panelObj.GetComponent(); rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.anchoredPosition = Vector2.zero; rect.sizeDelta = Vector2.zero; Image image = panelObj.AddComponent(); image.type = Image.Type.Filled; image.color = new Color(0.05f, 0.05f, 0.05f); var group = panelObj.AddComponent(); group.padding.left = 3; group.padding.right = 3; group.padding.bottom = 3; group.padding.top = 3; group.childControlHeight = true; group.childControlWidth = true; group.childForceExpandHeight = true; group.childForceExpandWidth = true; content = new GameObject("Content"); content.transform.parent = panelObj.transform; Image image2 = content.AddComponent(); image2.type = Image.Type.Filled; image2.color = new Color(0.1f, 0.1f, 0.1f); var group2 = content.AddComponent(); group2.padding.left = 5; group2.padding.right = 5; group2.padding.bottom = 5; group2.padding.top = 5; group2.spacing = 5; group2.childControlHeight = true; group2.childControlWidth = true; group2.childForceExpandHeight = false; group2.childForceExpandWidth = true; return panelObj; } public static GameObject CreateVerticalGroup(GameObject parent, Color color = default) { var groupObj = CreateUIObject("VerticalLayout", parent); var horiGroup = groupObj.AddComponent(); horiGroup.childAlignment = TextAnchor.UpperLeft; horiGroup.childControlWidth = false; var image = groupObj.AddComponent(); if (color != default) image.color = color; else image.color = new Color(44f / 255f, 44f / 255f, 44f / 255f); return groupObj; } public static GameObject CreateHorizontalGroup(GameObject parent, Color color = default) { var groupObj = CreateUIObject("HorizontalLayout", parent); var horiGroup = groupObj.AddComponent(); horiGroup.childAlignment = TextAnchor.UpperLeft; horiGroup.childControlWidth = false; var image = groupObj.AddComponent(); if (color != default) image.color = color; else image.color = new Color(44f / 255f, 44f / 255f, 44f / 255f); return groupObj; } public static GameObject CreateLabel(GameObject parent, TextAnchor alignment) { GameObject labelObj = CreateUIObject("Label", parent, s_ThinElementSize); var text = labelObj.AddComponent(); SetDefaultTextValues(text); text.alignment = alignment; text.supportRichText = true; return labelObj; } public static GameObject CreateButton(GameObject parent) { GameObject buttonObj = CreateUIObject("Button", parent, s_ThinElementSize); GameObject textObj = new GameObject("Text"); textObj.AddComponent(); SetParentAndAlign(textObj, buttonObj); Image image = buttonObj.AddComponent(); image.sprite = UIResources.standard; image.type = Image.Type.Sliced; image.color = s_DefaultSelectableColor; SetDefaultColorTransitionValues(buttonObj.AddComponent