FontManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public class FontManager
  10. {
  11. public static Dictionary<string, BaseFont> sFontFactory = new Dictionary<string, BaseFont>();
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. /// <param name="font"></param>
  16. /// <param name="alias"></param>
  17. static public void RegisterFont(BaseFont font, string alias = null)
  18. {
  19. sFontFactory[font.name] = font;
  20. if (alias != null)
  21. sFontFactory[alias] = font;
  22. }
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. /// <param name="font"></param>
  27. static public void UnregisterFont(BaseFont font)
  28. {
  29. List<string> toDelete = new List<string>();
  30. foreach (KeyValuePair<string, BaseFont> kv in sFontFactory)
  31. {
  32. if (kv.Value == font)
  33. toDelete.Add(kv.Key);
  34. }
  35. foreach (string key in toDelete)
  36. sFontFactory.Remove(key);
  37. }
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. /// <param name="name"></param>
  42. /// <returns></returns>
  43. static public BaseFont GetFont(string name)
  44. {
  45. BaseFont font;
  46. if (name.StartsWith(UIPackage.URL_PREFIX))
  47. {
  48. font = UIPackage.GetItemAssetByURL(name) as BaseFont;
  49. if (font != null)
  50. return font;
  51. }
  52. if (sFontFactory.TryGetValue(name, out font))
  53. return font;
  54. object asset = Resources.Load(name);
  55. if (asset == null)
  56. asset = Resources.Load("Fonts/" + name);
  57. //Try to use new API in Uinty5 to load
  58. if (asset == null)
  59. {
  60. if (name.IndexOf(",") != -1)
  61. {
  62. string[] arr = name.Split(',');
  63. int cnt = arr.Length;
  64. for (int i = 0; i < cnt; i++)
  65. arr[i] = arr[i].Trim();
  66. asset = Font.CreateDynamicFontFromOSFont(arr, 16);
  67. }
  68. else
  69. asset = Font.CreateDynamicFontFromOSFont(name, 16);
  70. }
  71. if (asset == null)
  72. return Fallback(name);
  73. if (asset is Font)
  74. {
  75. font = new DynamicFont();
  76. font.name = name;
  77. sFontFactory.Add(name, font);
  78. ((DynamicFont)font).nativeFont = (Font)asset;
  79. }
  80. #if FAIRYGUI_TMPRO
  81. else if (asset is TMPro.TMP_FontAsset)
  82. {
  83. font = new TMPFont();
  84. font.name = name;
  85. sFontFactory.Add(name, font);
  86. ((TMPFont)font).fontAsset = (TMPro.TMP_FontAsset)asset;
  87. }
  88. #endif
  89. else
  90. {
  91. if (asset.GetType().Name.Contains("TMP_FontAsset"))
  92. Debug.LogWarning("To enable TextMeshPro support, add script define symbol: FAIRYGUI_TMPRO");
  93. return Fallback(name);
  94. }
  95. return font;
  96. }
  97. static BaseFont Fallback(string name)
  98. {
  99. if (name != UIConfig.defaultFont)
  100. {
  101. BaseFont ff;
  102. if (sFontFactory.TryGetValue(UIConfig.defaultFont, out ff))
  103. {
  104. sFontFactory[name] = ff;
  105. return ff;
  106. }
  107. }
  108. Font asset = (Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
  109. if (asset == null)
  110. throw new Exception("Failed to load font '" + name + "'");
  111. BaseFont font = new DynamicFont();
  112. font.name = name;
  113. ((DynamicFont)font).nativeFont = asset;
  114. sFontFactory.Add(name, font);
  115. return font;
  116. }
  117. /// <summary>
  118. ///
  119. /// </summary>
  120. static public void Clear()
  121. {
  122. foreach (KeyValuePair<string, BaseFont> kv in sFontFactory)
  123. kv.Value.Dispose();
  124. sFontFactory.Clear();
  125. }
  126. }
  127. }