ThreeKingdomsSlingshot/Assets/Scripts/Runtime/TextUtil.cs

77 lines
2.8 KiB
C#

using UnityEngine;
namespace TKS
{
/// <summary>한글 표시용 폰트와 월드 텍스트(TextMesh) 생성.</summary>
public static class TextUtil
{
static Font font;
/// <summary>본문 폰트 = UI 테마 body(넥슨 Lv1 고딕 · ui_theme.csv) · 테마 없으면 OS 한글 폰트.</summary>
public static Font Font
{
get
{
var th = UITheme.Current;
if (th != null && th.bodyFont != null) return th.bodyFont;
if (font == null) font = LoadFont();
return font;
}
}
/// <summary>굵은 본문 폰트(테마 body_bold) · 없으면 null.</summary>
public static Font BoldFont
{
get
{
var th = UITheme.Current;
return th != null ? th.bodyBoldFont : null;
}
}
/// <summary>본문 글자 폰트·굵기 — 굵은 폰트 파일이 있으면 그것을 보통 굵기로(굵게 흉내를 겹치지 않음).</summary>
public static void UseBody(UnityEngine.UI.Text t, bool bold)
{
var b = bold ? BoldFont : null;
t.font = b != null ? b : Font;
t.fontStyle = bold && b == null ? FontStyle.Bold : FontStyle.Normal;
}
static Font LoadFont()
{
try
{
var f = UnityEngine.Font.CreateDynamicFontFromOSFont(
new[] { "Malgun Gothic", "맑은 고딕", "NanumGothic", "Noto Sans CJK KR", "Apple SD Gothic Neo", "Arial" }, 32);
if (f != null) return f;
}
catch (System.Exception e)
{
Debug.LogWarning("[TKS] OS 폰트 로드 실패 → 내장 폰트 사용: " + e.Message);
}
return Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
}
/// <summary>월드 좌표에 놓는 텍스트. worldHeight = 글자 높이(월드 단위).</summary>
public static TextMesh CreateWorldText(Transform parent, string text, float worldHeight, Color color, int sortingOrder,
TextAnchor anchor = TextAnchor.MiddleCenter)
{
var go = new GameObject("Text");
go.transform.SetParent(parent, false);
var tm = go.AddComponent<TextMesh>();
tm.font = Font;
tm.fontSize = 64;
tm.characterSize = worldHeight * 10f / 64f;
tm.text = text;
tm.color = color;
tm.anchor = anchor;
tm.alignment = TextAlignment.Center;
tm.richText = false;
var mr = go.GetComponent<MeshRenderer>();
mr.sharedMaterial = Font.material;
mr.sortingOrder = sortingOrder;
return tm;
}
}
}