using System; //using TMPro; using UnityEngine; using UnityEngine.UI; namespace UnityExplorer.UI { public static class UIFactory { internal static Vector2 thickSize = new Vector2(160f, 30f); internal static Vector2 thinSize = new Vector2(160f, 20f); internal static Color defaultTextColor = new Color(0.95f, 0.95f, 0.95f, 1f); internal static Font s_defaultFont; public static GameObject CreateUIObject(string name, GameObject parent, Vector2 size = default) { GameObject obj = new GameObject(name); RectTransform rect = obj.AddComponent(); if (size != default) { rect.sizeDelta = size; } SetParentAndAlign(obj, parent); return obj; } 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 T AddGraphic(this GameObject obj) where T : Graphic { var ret = obj.AddComponent(); ret.material = UIManager.UIMaterial; return ret; } private static void SetDefaultTextValues(Text lbl) { lbl.color = defaultTextColor; if (!s_defaultFont) s_defaultFont = Resources.GetBuiltinResource("Arial.ttf"); if (s_defaultFont) lbl.font = s_defaultFont; } public static void SetDefaultColorTransitionValues(Selectable selectable) { ColorBlock colors = selectable.colors; colors.normalColor = new Color(0.35f, 0.35f, 0.35f); colors.highlightedColor = new Color(0.45f, 0.45f, 0.45f); colors.pressedColor = new Color(0.25f, 0.25f, 0.25f); //colors.disabledColor = new Color(0.6f, 0.6f, 0.6f); // 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) { #if CPP button.onClick.AddListener(new Action(() => { button.OnDeselect(null); })); #else button.onClick.AddListener(Deselect); void Deselect() { button.OnDeselect(null); } #endif } selectable.colors = colors; } public static GameObject CreatePanel(GameObject parent, string name, out GameObject content) { GameObject panelObj = CreateUIObject($"Panel_{name}", parent, thickSize); RectTransform rect = panelObj.GetComponent(); rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.anchoredPosition = Vector2.zero; rect.sizeDelta = Vector2.zero; Image image = panelObj.AddGraphic(); image.type = Image.Type.Filled; image.color = new Color(0.05f, 0.05f, 0.05f); VerticalLayoutGroup 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.AddGraphic(); image2.type = Image.Type.Filled; image2.color = new Color(0.1f, 0.1f, 0.1f); VerticalLayoutGroup 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 CreateGridGroup(GameObject parent, Vector2 cellSize, Vector2 spacing, Color color = default) { GameObject groupObj = CreateUIObject("GridLayout", parent); GridLayoutGroup gridGroup = groupObj.AddComponent(); gridGroup.childAlignment = TextAnchor.UpperLeft; gridGroup.cellSize = cellSize; gridGroup.spacing = spacing; Image image = groupObj.AddGraphic(); if (color != default) { image.color = color; } else { image.color = new Color(44f / 255f, 44f / 255f, 44f / 255f); } return groupObj; } public static GameObject CreateVerticalGroup(GameObject parent, Color color = default) { GameObject groupObj = CreateUIObject("VerticalLayout", parent); VerticalLayoutGroup horiGroup = groupObj.AddComponent(); horiGroup.childAlignment = TextAnchor.UpperLeft; horiGroup.childControlWidth = false; Image image = groupObj.AddGraphic(); 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) { GameObject groupObj = CreateUIObject("HorizontalLayout", parent); HorizontalLayoutGroup horiGroup = groupObj.AddComponent(); horiGroup.childAlignment = TextAnchor.UpperLeft; horiGroup.childControlWidth = false; Image image = groupObj.AddGraphic(); if (color != default) { image.color = color; } else { image.color = new Color(44f / 255f, 44f / 255f, 44f / 255f); } return groupObj; } //public static GameObject CreateTMPLabel(GameObject parent, TextAlignmentOptions alignment) //{ // GameObject labelObj = CreateUIObject("Label", parent, thinSize); // TextMeshProUGUI text = labelObj.AddGraphic(); // text.alignment = alignment; // text.richText = true; // return labelObj; //} public static GameObject CreateLabel(GameObject parent, TextAnchor alignment) { GameObject labelObj = CreateUIObject("Label", parent, thinSize); Text text = labelObj.AddGraphic(); SetDefaultTextValues(text); text.alignment = alignment; text.supportRichText = true; return labelObj; } public static GameObject CreateButton(GameObject parent, Color normalColor = default) { GameObject buttonObj = CreateUIObject("Button", parent, thinSize); GameObject textObj = new GameObject("Text"); textObj.AddComponent(); SetParentAndAlign(textObj, buttonObj); Image image = buttonObj.AddGraphic(); image.type = Image.Type.Sliced; image.color = new Color(1, 1, 1, 0.75f); SetDefaultColorTransitionValues(buttonObj.AddComponent