| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | public class AssetPromise_Font : AssetPromise<Asset_Font> |
| | 12 | | { |
| | 13 | | private const string RESOURCE_FONT_FOLDER = "Fonts & Materials"; |
| | 14 | | private const string DEFAULT_SANS_SERIF_HEAVY = "Inter-Heavy SDF"; |
| | 15 | | private const string DEFAULT_SANS_SERIF_BOLD = "Inter-Bold SDF"; |
| | 16 | | private const string DEFAULT_SANS_SERIF_SEMIBOLD = "Inter-SemiBold SDF"; |
| | 17 | | private const string DEFAULT_SANS_SERIF = "Inter-Regular SDF"; |
| | 18 | |
|
| 49 | 19 | | private readonly Dictionary<string, string> fontsMapping = new Dictionary<string, string>() |
| | 20 | | { |
| | 21 | | { "builtin:SF-UI-Text-Regular SDF", DEFAULT_SANS_SERIF }, |
| | 22 | | { "builtin:SF-UI-Text-Heavy SDF", DEFAULT_SANS_SERIF_HEAVY }, |
| | 23 | | { "builtin:SF-UI-Text-Semibold SDF", DEFAULT_SANS_SERIF_SEMIBOLD }, |
| | 24 | | { "builtin:LiberationSans SDF", "LiberationSans SDF" }, |
| | 25 | | { "SansSerif", DEFAULT_SANS_SERIF }, |
| | 26 | | { "SansSerif_Heavy", DEFAULT_SANS_SERIF_HEAVY }, |
| | 27 | | { "SansSerif_Bold", DEFAULT_SANS_SERIF_BOLD }, |
| | 28 | | { "SansSerif_SemiBold", DEFAULT_SANS_SERIF_SEMIBOLD }, |
| | 29 | | }; |
| | 30 | |
|
| | 31 | | private string src; |
| | 32 | | private Coroutine fontCoroutine; |
| | 33 | |
|
| 147 | 34 | | public AssetPromise_Font(string src) { this.src = src; } |
| | 35 | |
|
| 41 | 36 | | protected override void OnAfterLoadOrReuse() { } |
| | 37 | |
|
| 51 | 38 | | protected override void OnBeforeLoadOrReuse() { } |
| | 39 | |
|
| | 40 | | protected override void OnCancelLoading() |
| | 41 | | { |
| 10 | 42 | | CoroutineStarter.Stop(fontCoroutine); |
| 10 | 43 | | } |
| | 44 | |
|
| | 45 | | public override void Cleanup() |
| | 46 | | { |
| 37 | 47 | | base.Cleanup(); |
| 37 | 48 | | CoroutineStarter.Stop(fontCoroutine); |
| 37 | 49 | | } |
| | 50 | |
|
| | 51 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 52 | | { |
| 30 | 53 | | if (fontsMapping.TryGetValue(src, out string fontResourceName)) |
| | 54 | | { |
| 30 | 55 | | fontCoroutine = CoroutineStarter.Start(GetFontFromResources(OnSuccess,OnFail,fontResourceName)); |
| 30 | 56 | | } |
| | 57 | | else |
| | 58 | | { |
| 0 | 59 | | OnFail?.Invoke(new Exception("Font doesn't correspond with any know font")); |
| | 60 | | } |
| 0 | 61 | | } |
| | 62 | |
|
| 260 | 63 | | public override object GetId() { return src; } |
| | 64 | |
|
| | 65 | | IEnumerator GetFontFromResources(Action OnSuccess, Action<Exception> OnFail, string fontResourceName) |
| | 66 | | { |
| 30 | 67 | | ResourceRequest request = Resources.LoadAsync($"{RESOURCE_FONT_FOLDER}/{fontResourceName}", |
| | 68 | | typeof(TMP_FontAsset)); |
| | 69 | |
|
| 30 | 70 | | yield return request; |
| | 71 | |
|
| 20 | 72 | | if (request.asset != null) |
| | 73 | | { |
| 20 | 74 | | asset.font = request.asset as TMP_FontAsset; |
| 20 | 75 | | OnSuccess?.Invoke(); |
| 20 | 76 | | } |
| | 77 | | else |
| | 78 | | { |
| 0 | 79 | | string message = $"couldn't fetch font from resources {fontResourceName}"; |
| 0 | 80 | | Debug.Log(message); |
| 0 | 81 | | OnFail?.Invoke(new Exception(message)); |
| | 82 | | } |
| 20 | 83 | | } |
| | 84 | | } |
| | 85 | | } |