TMP_StyleSheet.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace TMPro
  5. {
  6. [Serializable]
  7. public class TMP_StyleSheet : ScriptableObject
  8. {
  9. private static TMP_StyleSheet s_Instance;
  10. [SerializeField]
  11. private List<TMP_Style> m_StyleList = new List<TMP_Style>(1);
  12. private Dictionary<int, TMP_Style> m_StyleDictionary = new Dictionary<int, TMP_Style>();
  13. /// <summary>
  14. /// Get a singleton instance of the TMP_StyleSheet
  15. /// </summary>
  16. public static TMP_StyleSheet instance
  17. {
  18. get
  19. {
  20. if (s_Instance == null)
  21. {
  22. s_Instance = TMP_Settings.defaultStyleSheet;
  23. if (s_Instance == null)
  24. s_Instance = Resources.Load<TMP_StyleSheet>("Style Sheets/Default Style Sheet");
  25. if (s_Instance == null) return null;
  26. // Load the style dictionary.
  27. s_Instance.LoadStyleDictionaryInternal();
  28. }
  29. return s_Instance;
  30. }
  31. }
  32. /// <summary>
  33. /// Static Function to load the Default Style Sheet.
  34. /// </summary>
  35. /// <returns></returns>
  36. public static TMP_StyleSheet LoadDefaultStyleSheet()
  37. {
  38. return instance;
  39. }
  40. /// <summary>
  41. /// Function to retrieve the Style matching the HashCode.
  42. /// </summary>
  43. /// <param name="hashCode"></param>
  44. /// <returns></returns>
  45. public static TMP_Style GetStyle(int hashCode)
  46. {
  47. return instance.GetStyleInternal(hashCode);
  48. }
  49. /// <summary>
  50. /// Internal method to retrieve the Style matching the Hashcode.
  51. /// </summary>
  52. /// <param name="hashCode"></param>
  53. /// <returns></returns>
  54. private TMP_Style GetStyleInternal(int hashCode)
  55. {
  56. if (m_StyleDictionary.TryGetValue(hashCode, out TMP_Style style))
  57. {
  58. return style;
  59. }
  60. return null;
  61. }
  62. public void UpdateStyleDictionaryKey(int old_key, int new_key)
  63. {
  64. if (m_StyleDictionary.ContainsKey(old_key))
  65. {
  66. TMP_Style style = m_StyleDictionary[old_key];
  67. m_StyleDictionary.Add(new_key, style);
  68. m_StyleDictionary.Remove(old_key);
  69. }
  70. }
  71. /// <summary>
  72. /// Function to update the internal reference to a newly assigned style sheet in the TMP Settings.
  73. /// </summary>
  74. public static void UpdateStyleSheet()
  75. {
  76. // Reset instance
  77. s_Instance = null;
  78. RefreshStyles();
  79. }
  80. /// <summary>
  81. /// Function to refresh the Style Dictionary.
  82. /// </summary>
  83. public static void RefreshStyles()
  84. {
  85. instance.LoadStyleDictionaryInternal();
  86. }
  87. /// <summary>
  88. ///
  89. /// </summary>
  90. private void LoadStyleDictionaryInternal()
  91. {
  92. m_StyleDictionary.Clear();
  93. // Read Styles from style list and store them into dictionary for faster access.
  94. for (int i = 0; i < m_StyleList.Count; i++)
  95. {
  96. m_StyleList[i].RefreshStyle();
  97. if (!m_StyleDictionary.ContainsKey(m_StyleList[i].hashCode))
  98. m_StyleDictionary.Add(m_StyleList[i].hashCode, m_StyleList[i]);
  99. }
  100. }
  101. }
  102. }