refactor: Update Unity to 2022.3.62

This commit is contained in:
2025-06-30 16:04:04 +08:00
parent 4758b65159
commit 44a72d1a8f
329 changed files with 4201 additions and 100185 deletions

View File

@@ -2,7 +2,7 @@
"name": "Cryville.Common",
"rootNamespace": "",
"references": [
"GUID:da293eebbcb9a4947a212534c52d1a32"
"GUID:6055be8ebefd69e48b49212b09b47b2f"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -6,8 +6,9 @@ using System.Globalization;
using System.Reflection;
using TMPro;
using UnityEngine;
using UnityEngine.TextCore;
using UnityEngine.TextCore.LowLevel;
using UnityEngine.TextCore.Text;
using AtlasPopulationMode = TMPro.AtlasPopulationMode;
namespace Cryville.Common.Unity.UI {
[RequireComponent(typeof(TMP_Text))]
@@ -16,7 +17,7 @@ namespace Cryville.Common.Unity.UI {
public static FontMatcher FontMatcher;
public static int MaxFallbackCount = 4;
static readonly Dictionary<CultureInfo, FontAsset> _cachedFonts = new();
static readonly Dictionary<CultureInfo, TMP_FontAsset> _cachedFonts = new();
[SerializeField]
Shader m_shader;
@@ -46,7 +47,7 @@ namespace Cryville.Common.Unity.UI {
if (MaxFallbackCount <= 0) break;
}
else {
font.fallbackFontAssetTable ??= new List<FontAsset>();
font.fallbackFontAssetTable ??= new List<TMP_FontAsset>();
font.fallbackFontAssetTable.Add(ifont);
if (font.fallbackFontAssetTable.Count >= MaxFallbackCount) break;
}
@@ -58,23 +59,93 @@ namespace Cryville.Common.Unity.UI {
Text.font = font;
}
static MethodInfo _methodCreateFontAsset;
static readonly object[] _paramsCreateFontAsset = new object[] { null, null, 90, 9, GlyphRenderMode.SDFAA, 1024, 1024, Type.Missing, Type.Missing };
static FontAsset CreateFontAsset(string path, int index) {
if (_methodCreateFontAsset == null) {
_methodCreateFontAsset = typeof(FontAsset).GetMethod(
"CreateFontAsset", BindingFlags.Static | BindingFlags.NonPublic, null,
new Type[] {
typeof(string), typeof(int), typeof(int), typeof(int),
typeof(GlyphRenderMode), typeof(int), typeof(int),
typeof(AtlasPopulationMode), typeof(bool)
},
null
);
static TMP_FontAsset CreateFontAsset(string path, int index) => CreateFontAsset(path, index, 90, 9, GlyphRenderMode.SDFAA, 1024, 1024, AtlasPopulationMode.Dynamic);
static readonly Lazy<FieldInfo> _f_m_Version = new(() => typeof(TMP_FontAsset).GetField("m_Version", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
static readonly Lazy<PropertyInfo> _p_atlasWidth = new(() => typeof(TMP_FontAsset).GetProperty("atlasWidth", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
static readonly Lazy<PropertyInfo> _p_atlasHeight = new(() => typeof(TMP_FontAsset).GetProperty("atlasHeight", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
static readonly Lazy<PropertyInfo> _p_atlasPadding = new(() => typeof(TMP_FontAsset).GetProperty("atlasPadding", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
static readonly Lazy<PropertyInfo> _p_atlasRenderMode = new(() => typeof(TMP_FontAsset).GetProperty("atlasRenderMode", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
static readonly Lazy<PropertyInfo> _p_freeGlyphRects = new(() => typeof(TMP_FontAsset).GetProperty("freeGlyphRects", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
static readonly Lazy<PropertyInfo> _p_usedGlyphRects = new(() => typeof(TMP_FontAsset).GetProperty("usedGlyphRects", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
static readonly Lazy<PropertyInfo> _p_ShaderRef_MobileBitmap = new(() => typeof(ShaderUtilities).GetProperty("ShaderRef_MobileBitmap", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
static readonly Lazy<PropertyInfo> _p_ShaderRef_MobileSDF = new(() => typeof(ShaderUtilities).GetProperty("ShaderRef_MobileSDF", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
public static TMP_FontAsset CreateFontAsset(string path, int faceIndex, int samplingPointSize, int atlasPadding, GlyphRenderMode renderMode, int atlasWidth, int atlasHeight, AtlasPopulationMode atlasPopulationMode = AtlasPopulationMode.Dynamic, bool enableMultiAtlasSupport = true) {
// Initialize FontEngine
FontEngine.InitializeFontEngine();
// Load Font Face
if (FontEngine.LoadFontFace(path, samplingPointSize, faceIndex) != FontEngineError.Success) {
Debug.LogWarning("Unable to load font face at path " + path);
return null;
}
_paramsCreateFontAsset[0] = path;
_paramsCreateFontAsset[1] = index;
return (FontAsset)_methodCreateFontAsset.Invoke(null, _paramsCreateFontAsset);
// Create new font asset
TMP_FontAsset fontAsset = ScriptableObject.CreateInstance<TMP_FontAsset>();
_f_m_Version.Value.SetValue(fontAsset, "1.1.0");
fontAsset.faceInfo = FontEngine.GetFaceInfo();
fontAsset.atlasPopulationMode = atlasPopulationMode;
_p_atlasWidth.Value.SetValue(fontAsset, atlasWidth);
_p_atlasHeight.Value.SetValue(fontAsset, atlasHeight);
_p_atlasPadding.Value.SetValue(fontAsset, atlasPadding);
_p_atlasRenderMode.Value.SetValue(fontAsset, renderMode);
// Initialize array for the font atlas textures.
fontAsset.atlasTextures = new Texture2D[1];
// Create and add font atlas texture.
var texture = new Texture2D(0, 0, TextureFormat.Alpha8, false);
fontAsset.atlasTextures[0] = texture;
fontAsset.isMultiAtlasTexturesEnabled = enableMultiAtlasSupport;
// Add free rectangle of the size of the texture.
int packingModifier;
if (((int)renderMode & 0x10) != 0) {
packingModifier = 0;
// Optimize by adding static ref to shader.
var tmp_material = new Material((Shader)_p_ShaderRef_MobileBitmap.Value.GetValue(null));
//tmp_material.name = texture.name + " Material";
tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
fontAsset.material = tmp_material;
}
else {
packingModifier = 1;
// Optimize by adding static ref to shader.
var tmp_material = new Material((Shader)_p_ShaderRef_MobileSDF.Value.GetValue(null));
//tmp_material.name = texture.name + " Material";
tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, atlasPadding + packingModifier);
tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
fontAsset.material = tmp_material;
}
_p_freeGlyphRects.Value.SetValue(fontAsset, new List<GlyphRect>(8) { new(0, 0, atlasWidth - packingModifier, atlasHeight - packingModifier) });
_p_usedGlyphRects.Value.SetValue(fontAsset, new List<GlyphRect>(8));
// TODO: Consider adding support for extracting glyph positioning data
fontAsset.ReadFontAssetDefinition();
return fontAsset;
}
}
}