源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

126 行
3.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using System.Collections;
  7. namespace LuaFramework
  8. {
  9. public class DynamicFontTextureRebuildTracker : MonoBehaviour
  10. {
  11. private class FontUpdateNode
  12. {
  13. private bool m_FontTextureRebuilt = false;
  14. private Font m_FontRebuilt = null;
  15. public FontUpdateNode(Font font)
  16. {
  17. m_FontRebuilt = font;
  18. Validate();
  19. }
  20. public void Validate()
  21. {
  22. if (null == m_FontRebuilt)
  23. {
  24. m_FontTextureRebuilt = false;
  25. return;
  26. }
  27. m_FontTextureRebuilt = true;
  28. }
  29. public void Invalidate()
  30. {
  31. m_FontTextureRebuilt = false;
  32. }
  33. public bool NeedUpdate
  34. {
  35. get { return m_FontTextureRebuilt && (null != m_FontRebuilt); }
  36. }
  37. public Font font
  38. {
  39. get { return m_FontRebuilt; }
  40. }
  41. }
  42. private System.Reflection.MethodInfo m_RebuildForFont = null;
  43. private List<FontUpdateNode> m_FontUpdateList = new List<FontUpdateNode>();
  44. private static DynamicFontTextureRebuildTracker m_Instance = null;
  45. void Awake()
  46. {
  47. if (null != m_Instance)
  48. {
  49. LogManager.LogError("There is only one DynamicFontTextureRebuildTracker instance allowed!");
  50. Destroy(gameObject);
  51. return;
  52. }
  53. m_Instance = this;
  54. }
  55. // Use this for initialization
  56. void Start()
  57. {
  58. Font.textureRebuilt += OnFontTextureRebuilt;
  59. System.Type fontUpdateTrackerType = typeof(UnityEngine.UI.FontUpdateTracker);
  60. m_RebuildForFont = fontUpdateTrackerType.GetMethod("RebuildForFont", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
  61. Debug.Log("Get RebuildForFont method is: " + m_RebuildForFont);
  62. }
  63. // Update is called once per frame
  64. void LateUpdate()
  65. {
  66. if (null == m_RebuildForFont)
  67. {
  68. return;
  69. }
  70. for (int i = 0; i < m_FontUpdateList.Count; i++)
  71. {
  72. FontUpdateNode node = m_FontUpdateList[i];
  73. if (node.NeedUpdate)
  74. {
  75. Font font = node.font;
  76. m_RebuildForFont.Invoke(null, new object[] { font });
  77. // Log rebuild.
  78. // Texture fontTexture = font.material.mainTexture;
  79. // Debug.Log(string.Format("Texture of dynamic font \"{0}\" is enlarged to {1}x{2}.", font.name, fontTexture.width, fontTexture.height));
  80. node.Invalidate();
  81. }
  82. }
  83. }
  84. void OnDestroy()
  85. {
  86. Font.textureRebuilt -= OnFontTextureRebuilt;
  87. }
  88. private void OnFontTextureRebuilt(Font font)
  89. {
  90. bool findThisFont = false;
  91. for (int i = 0; i < m_FontUpdateList.Count; i++)
  92. {
  93. FontUpdateNode node = m_FontUpdateList[i];
  94. if (node.font == font)
  95. {
  96. node.Validate();
  97. findThisFont = true;
  98. break;
  99. }
  100. }
  101. if (!findThisFont)
  102. {
  103. m_FontUpdateList.Add(new FontUpdateNode(font));
  104. }
  105. }
  106. }
  107. }