| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using Object = UnityEngine.Object; |
| | 8 | |
|
| | 9 | | public class EmbeddedFontProvider : IFontAssetProvider |
| | 10 | | { |
| | 11 | | private const string RESOURCE_FONT_FOLDER = "Fonts & Materials"; |
| | 12 | | private const string DEFAULT_SANS_SERIF_HEAVY = "Inter-Heavy SDF"; |
| | 13 | | private const string DEFAULT_SANS_SERIF_BOLD = "Inter-Bold SDF"; |
| | 14 | | private const string DEFAULT_SANS_SERIF_SEMIBOLD = "Inter-SemiBold SDF"; |
| | 15 | | private const string DEFAULT_SANS_SERIF = "Inter-Regular SDF"; |
| | 16 | |
|
| 1 | 17 | | private static readonly Dictionary<string, string> fontsMapping = new () |
| | 18 | | { |
| | 19 | | { "builtin:SF-UI-Text-Regular SDF", DEFAULT_SANS_SERIF }, |
| | 20 | | { "builtin:SF-UI-Text-Heavy SDF", DEFAULT_SANS_SERIF_HEAVY }, |
| | 21 | | { "builtin:SF-UI-Text-Semibold SDF", DEFAULT_SANS_SERIF_SEMIBOLD }, |
| | 22 | | { "builtin:LiberationSans SDF", "LiberationSans SDF" }, |
| | 23 | | { "SansSerif", DEFAULT_SANS_SERIF }, |
| | 24 | | { "SansSerif_Heavy", DEFAULT_SANS_SERIF_HEAVY }, |
| | 25 | | { "SansSerif_Bold", DEFAULT_SANS_SERIF_BOLD }, |
| | 26 | | { "SansSerif_SemiBold", DEFAULT_SANS_SERIF_SEMIBOLD }, |
| | 27 | | }; |
| | 28 | |
|
| | 29 | | public async UniTask<TMP_FontAsset> GetFontAsync(string src, CancellationToken cancellationToken = default) |
| | 30 | | { |
| 33 | 31 | | if (!fontsMapping.TryGetValue(src, out string fontResourceName)) |
| 0 | 32 | | throw new Exception("Font doesn't correspond with any know font"); |
| | 33 | |
|
| 99 | 34 | | Object result = await Resources.LoadAsync<TMP_FontAsset>($"{RESOURCE_FONT_FOLDER}/{fontResourceName}").WithCance |
| 29 | 35 | | return (TMP_FontAsset)result; |
| 29 | 36 | | } |
| | 37 | | } |