40 lines
1.4 KiB
C#
40 lines
1.4 KiB
C#
using Cryville.Common.Font;
|
|
using System;
|
|
using System.Reflection;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.TextCore.LowLevel;
|
|
using UnityEngine.TextCore.Text;
|
|
using IFont = UnityEngine.Font;
|
|
|
|
namespace Cryville.Common.Unity.UI {
|
|
[RequireComponent(typeof(TextMeshProUGUI))]
|
|
public class TMPAutoFont : MonoBehaviour {
|
|
public static Shader DefaultShader;
|
|
public static FontMatcher FontMatcher;
|
|
static FontAsset _font;
|
|
TextMeshProUGUI _text;
|
|
|
|
[SerializeField]
|
|
Shader m_shader;
|
|
void Awake() {
|
|
if (FontMatcher == null) return;
|
|
_text = GetComponent<TextMeshProUGUI>();
|
|
if (_font == null) {
|
|
foreach (var typeface in FontMatcher.MatchScript(null, true)) {
|
|
try {
|
|
var _ifont = new IFont(typeface.File.FullName);
|
|
_font = (FontAsset)typeof(FontAsset).GetMethod("CreateFontAsset", BindingFlags.Static | BindingFlags.NonPublic, null, new Type[] { typeof(IFont), typeof(int), typeof(int), typeof(int), typeof(GlyphRenderMode), typeof(int), typeof(int), typeof(AtlasPopulationMode), typeof(bool) },
|
|
null).Invoke(null, new object[] { _ifont, typeface.IndexInFile, 90, 9, GlyphRenderMode.SDFAA, 1024, 1024, Type.Missing, Type.Missing });
|
|
break;
|
|
}
|
|
catch (Exception) { }
|
|
}
|
|
}
|
|
_text.font = _font;
|
|
if (m_shader) _font.material.shader = m_shader;
|
|
else if (DefaultShader) _font.material.shader = DefaultShader;
|
|
}
|
|
}
|
|
}
|