自定义字体

This commit is contained in:
tianmo committed 2026-10-20 18:59:18 +08:00
1 parent 1aeaebcddb
commit b26a10ceb9
20 files changed
+2861 -88

No files matched your search

@@ -0,0 +1,75 @@
using System.Globalization;
using System.Text;
using TMPro;
using UnityEngine;
[RequireComponent(typeof(TextMeshProUGUI))]
public class digits_sheet_auto : MonoBehaviour
{
[SerializeField, Tooltip("要显示的数字,支持小数和负数")]
private double value;
[SerializeField, Tooltip("是否显示千分位逗号")]
private bool showComma = true;
[SerializeField, Tooltip("最多保留几位小数,尾数为 0 时不显示")]
private int decimalPlaces = 3;
private TextMeshProUGUI label;
private void Awake()
{
label = GetComponent<TextMeshProUGUI>();
if (label == null)
label = gameObject.AddComponent<TextMeshProUGUI>();
label.richText = true;
Refresh();
}
private void OnValidate()
{
label = GetComponent<TextMeshProUGUI>();
if (label != null)
Refresh();
}
public void SetValue(double newValue)
{
value = newValue;
Refresh();
}
private void Refresh()
{
if (label == null)
return;
int places = Mathf.Clamp(decimalPlaces, 0, 15);
string format = showComma ? "#,##0" : "0";
if (places > 0)
format += "." + new string('#', places);
string number = value.ToString(format, CultureInfo.InvariantCulture);
label.text = ToSpriteTags(number);
}
private static string ToSpriteTags(string number)
{
var sb = new StringBuilder(number.Length * 24);
foreach (char c in number)
{
if (c >= '0' && c <= '9')
sb.Append("<sprite name=\"").Append(c).Append("\">");
else if (c == ',')
sb.Append("<sprite name=\"comma\">");
else if (c == '.')
sb.Append("<sprite name=\"dot\">");
else
sb.Append(c); // 图集里没有负号,负号退回普通字体
}
return sb.ToString();
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: aff4690ca9bc94b8b84c2ee53b298588
@@ -0,0 +1,30 @@
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(digits_sheet_auto))]
public class digits_sheet_autoEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
SerializedProperty prop = serializedObject.GetIterator();
bool enterChildren = true;
while (prop.NextVisible(enterChildren))
{
enterChildren = false;
if (prop.name == "m_Script")
continue;
if (prop.name == "showComma")
EditorGUILayout.PropertyField(prop, new GUIContent("显示逗号‘,’"));
else if (prop.name == "decimalPlaces")
EditorGUILayout.PropertyField(prop, new GUIContent("保留小数点位数"));
else
EditorGUILayout.PropertyField(prop, true);
}
serializedObject.ApplyModifiedProperties();
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 48f5d0673cac847c3b3da256f68eeb4e