Ui.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Fateforge
  6. {
  7. public static class Ui
  8. {
  9. public static RectTransform Rect(Transform parent,string name,float x,float y,float width,float height)
  10. {var go=new GameObject(name,typeof(RectTransform));go.transform.SetParent(parent,false);var rect=go.GetComponent<RectTransform>();Place(rect,x,y,width,height);return rect;}
  11. public static void Place(RectTransform rect,float x,float y,float width,float height)
  12. {rect.anchorMin=rect.anchorMax=new Vector2(.5f,1);rect.pivot=new Vector2(.5f,1);rect.anchoredPosition=new Vector2(x,-y);rect.sizeDelta=new Vector2(width,height);}
  13. public static void Stretch(RectTransform r,float left=0,float top=0,float right=0,float bottom=0)
  14. {r.anchorMin=Vector2.zero;r.anchorMax=Vector2.one;r.offsetMin=new Vector2(left,bottom);r.offsetMax=new Vector2(-right,-top);}
  15. public static Image Image(RectTransform rect,Sprite sprite,Color color,bool raycast=false)
  16. {var image=rect.gameObject.AddComponent<Image>();image.sprite=sprite;image.color=color;image.raycastTarget=raycast;if(sprite!=null&&sprite.border!=Vector4.zero)image.type=UnityEngine.UI.Image.Type.Sliced;return image;}
  17. public static TMP_Text Text(Transform parent,string name,string value,float x,float y,float w,float h,float size,UiTheme theme,TextAlignmentOptions align=TextAlignmentOptions.Center)
  18. {var rect=Rect(parent,name,x,y,w,h);var text=rect.gameObject.AddComponent<TextMeshProUGUI>();text.font=theme.bodyFont;text.text=value;text.fontSize=size;text.enableAutoSizing=true;text.fontSizeMin=size*.75f;text.fontSizeMax=size;text.color=theme.text;text.alignment=align;text.textWrappingMode=TextWrappingModes.Normal;text.overflowMode=TextOverflowModes.Ellipsis;text.raycastTarget=false;return text;}
  19. public static Button Button(Transform parent,string name,string label,float x,float y,float w,float h,UiTheme theme,Action action=null,bool primary=false)
  20. {var rect=Rect(parent,name,x,y,w,h);Image(rect,null,Color.clear,true);var surface=Rect(rect,"Surface",0,0,w,h);Stretch(surface);var image=Image(surface,theme.panelSprite,Color.white);var button=rect.gameObject.AddComponent<Button>();button.targetGraphic=image;var colors=button.colors;colors.highlightedColor=new Color(1.08f,1.08f,1.08f);colors.pressedColor=new Color(.75f,.75f,.75f);colors.disabledColor=new Color(.55f,.55f,.55f,.6f);button.colors=colors;
  21. var text=Text(rect,"Label",label,0,0,w-12,h,26,theme);Stretch(text.rectTransform,6,0,6,0);text.color=primary?theme.background:theme.text;Skin(button,theme,primary);if(action!=null)button.onClick.AddListener(()=>action());return button;}
  22. public static void Skin(Button button,UiTheme theme,bool primary)
  23. {
  24. var image=button.targetGraphic as Image;if(image==null)return;
  25. image.sprite=primary?theme.primarySprite:theme.secondarySprite;
  26. if(image.sprite==null){image.sprite=theme.panelSprite;image.color=primary?theme.gold:theme.panel;}
  27. else image.color=Color.white;
  28. image.type=UnityEngine.UI.Image.Type.Sliced;image.pixelsPerUnitMultiplier=1.6f;
  29. var label=button.GetComponentInChildren<TMP_Text>(true);if(label)label.color=primary?new Color(.16f,.075f,.035f):theme.text;
  30. }
  31. public static UiGlyph Glyph(Transform parent,string name,UiGlyph.Shape shape,float x,float y,float w,float h,Color color)
  32. {var rect=Rect(parent,name,x,y,w,h);rect.gameObject.AddComponent<CanvasRenderer>();var glyph=rect.gameObject.AddComponent<UiGlyph>();glyph.shape=shape;glyph.color=color;glyph.raycastTarget=false;return glyph;}
  33. public static void Label(Button button,string value){button.GetComponentInChildren<TMP_Text>(true).text=value;}
  34. public static void Clear(Transform parent){for(int i=parent.childCount-1;i>=0;i--){var go=parent.GetChild(i).gameObject;go.SetActive(false);UnityEngine.Object.Destroy(go);}}
  35. }
  36. }