TMP_ResourcesManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace TMPro
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class TMP_ResourceManager
  10. {
  11. private static readonly TMP_ResourceManager s_instance = new TMP_ResourceManager();
  12. static TMP_ResourceManager() { }
  13. // ======================================================
  14. // TEXT SETTINGS MANAGEMENT
  15. // ======================================================
  16. private static TMP_Settings s_TextSettings;
  17. internal static TMP_Settings GetTextSettings()
  18. {
  19. if (s_TextSettings == null)
  20. {
  21. // Try loading the TMP Settings from a Resources folder in the user project.
  22. s_TextSettings = Resources.Load<TMP_Settings>("TextSettings"); // ?? ScriptableObject.CreateInstance<TMP_Settings>();
  23. #if UNITY_EDITOR
  24. if (s_TextSettings == null)
  25. {
  26. // Open TMP Resources Importer to enable the user to import the TMP Essential Resources and option TMP Examples & Extras
  27. TMP_PackageResourceImporterWindow.ShowPackageImporterWindow();
  28. }
  29. #endif
  30. }
  31. return s_TextSettings;
  32. }
  33. // ======================================================
  34. // FONT ASSET MANAGEMENT - Fields, Properties and Functions
  35. // ======================================================
  36. private static readonly List<TMP_FontAsset> s_FontAssetReferences = new List<TMP_FontAsset>();
  37. private static readonly Dictionary<int, TMP_FontAsset> s_FontAssetReferenceLookup = new Dictionary<int, TMP_FontAsset>();
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. /// <param name="fontAsset"></param>
  42. public static void AddFontAsset(TMP_FontAsset fontAsset)
  43. {
  44. int hashcode = fontAsset.hashCode;
  45. if (s_FontAssetReferenceLookup.ContainsKey(hashcode))
  46. return;
  47. s_FontAssetReferences.Add(fontAsset);
  48. s_FontAssetReferenceLookup.Add(hashcode, fontAsset);
  49. }
  50. /// <summary>
  51. ///
  52. /// </summary>
  53. /// <param name="hashcode"></param>
  54. /// <param name="fontAsset"></param>
  55. /// <returns></returns>
  56. public static bool TryGetFontAsset(int hashcode, out TMP_FontAsset fontAsset)
  57. {
  58. fontAsset = null;
  59. return s_FontAssetReferenceLookup.TryGetValue(hashcode, out fontAsset);
  60. }
  61. internal static void RebuildFontAssetCache(int instanceID)
  62. {
  63. // Iterate over loaded font assets to update affected font assets
  64. for (int i = 0; i < s_FontAssetReferences.Count; i++)
  65. {
  66. TMP_FontAsset fontAsset = s_FontAssetReferences[i];
  67. if (fontAsset.FallbackSearchQueryLookup.Contains(instanceID))
  68. fontAsset.ReadFontAssetDefinition();
  69. }
  70. }
  71. }
  72. }