ThreeKingdomsSlingshot/Assets/Scripts/Runtime/TextUtil.cs

56 lines
1.8 KiB
C#
Raw Permalink Normal View History

using UnityEngine;
namespace TKS
{
/// <summary>한글 표시용 폰트와 월드 텍스트(TextMesh) 생성.</summary>
public static class TextUtil
{
static Font font;
public static Font Font
{
get
{
if (font == null) font = LoadFont();
return font;
}
}
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;
}
}
}