KSP-Conformal-Decals/Source/ConformalDecals/Text/FontLoader.cs

36 lines
1.4 KiB
C#
Raw Normal View History

using System.IO;
using System.Collections;
using System.Collections.Generic;
using TMPro;
2020-06-19 17:36:13 +00:00
using UniLinq;
using UnityEngine;
namespace ConformalDecals.Text {
[DatabaseLoaderAttrib(new[] {"kspfont"})]
public class FontLoader : DatabaseLoader<GameDatabase.TextureInfo> {
2020-06-19 17:36:13 +00:00
private const string FallbackName = "NotoSans-Regular SDF";
private static TMP_FontAsset _fallbackFont;
public override IEnumerator Load(UrlDir.UrlFile urlFile, FileInfo fileInfo) {
2020-06-19 17:36:13 +00:00
if (_fallbackFont == null) {
_fallbackFont = Resources.FindObjectsOfTypeAll<TMP_FontAsset>().First(o => o.name == FallbackName);
if (_fallbackFont == null) Debug.LogError($"Could not find fallback font '{FallbackName}'");
}
Debug.Log($"[ConformalDecals] '{urlFile.fullPath}'");
var bundle = AssetBundle.LoadFromFile(urlFile.fullPath);
if (!bundle) {
Debug.Log($"[ConformalDecals] could not load font asset {urlFile.fullPath}");
}
else {
var loadedFonts = bundle.LoadAllAssets<TMP_FontAsset>();
foreach (var font in loadedFonts) {
2020-06-19 17:36:13 +00:00
Debug.Log($"[ConformalDecals] adding font {font.name}");
font.fallbackFontAssets.Add(_fallbackFont);
}
}
2020-06-19 17:36:13 +00:00
yield break;
}
}
}