| 12345678910111213141516171819202122232425262728293031323334353637 |
- using System;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Fateforge
- {
- public static class Ui
- {
- public static RectTransform Rect(Transform parent,string name,float x,float y,float width,float height)
- {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;}
- public static void Place(RectTransform rect,float x,float y,float width,float height)
- {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);}
- public static void Stretch(RectTransform r,float left=0,float top=0,float right=0,float bottom=0)
- {r.anchorMin=Vector2.zero;r.anchorMax=Vector2.one;r.offsetMin=new Vector2(left,bottom);r.offsetMax=new Vector2(-right,-top);}
- public static Image Image(RectTransform rect,Sprite sprite,Color color,bool raycast=false)
- {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;}
- 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)
- {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;}
- 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)
- {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;
- 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;}
- public static void Skin(Button button,UiTheme theme,bool primary)
- {
- var image=button.targetGraphic as Image;if(image==null)return;
- image.sprite=primary?theme.primarySprite:theme.secondarySprite;
- if(image.sprite==null){image.sprite=theme.panelSprite;image.color=primary?theme.gold:theme.panel;}
- else image.color=Color.white;
- image.type=UnityEngine.UI.Image.Type.Sliced;image.pixelsPerUnitMultiplier=1.6f;
- var label=button.GetComponentInChildren<TMP_Text>(true);if(label)label.color=primary?new Color(.16f,.075f,.035f):theme.text;
- }
- public static UiGlyph Glyph(Transform parent,string name,UiGlyph.Shape shape,float x,float y,float w,float h,Color color)
- {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;}
- public static void Label(Button button,string value){button.GetComponentInChildren<TMP_Text>(true).text=value;}
- 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);}}
- }
- }
|