TMP_StyleSheet.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace TMPro
  5. {
  6. [Serializable][ExcludeFromPresetAttribute]
  7. public class TMP_StyleSheet : ScriptableObject
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. internal List<TMP_Style> styles
  13. {
  14. get { return m_StyleList; }
  15. }
  16. [SerializeField]
  17. private List<TMP_Style> m_StyleList = new List<TMP_Style>(1);
  18. private Dictionary<int, TMP_Style> m_StyleLookupDictionary;
  19. private void Reset()
  20. {
  21. LoadStyleDictionaryInternal();
  22. }
  23. /// <summary>
  24. /// Get the Style for the given hash code value.
  25. /// </summary>
  26. /// <param name="hashCode">Hash code of the style.</param>
  27. /// <returns>The style matching the hash code.</returns>
  28. public TMP_Style GetStyle(int hashCode)
  29. {
  30. if (m_StyleLookupDictionary == null)
  31. LoadStyleDictionaryInternal();
  32. TMP_Style style;
  33. if (m_StyleLookupDictionary.TryGetValue(hashCode, out style))
  34. return style;
  35. return null;
  36. }
  37. /// <summary>
  38. /// Get the Style for the given name.
  39. /// </summary>
  40. /// <param name="name">The name of the style.</param>
  41. /// <returns>The style if found.</returns>
  42. public TMP_Style GetStyle(string name)
  43. {
  44. if (m_StyleLookupDictionary == null)
  45. LoadStyleDictionaryInternal();
  46. int hashCode = TMP_TextParsingUtilities.GetHashCode(name);
  47. TMP_Style style;
  48. if (m_StyleLookupDictionary.TryGetValue(hashCode, out style))
  49. return style;
  50. return null;
  51. }
  52. /// <summary>
  53. /// Function to refresh the Style Dictionary.
  54. /// </summary>
  55. public void RefreshStyles()
  56. {
  57. LoadStyleDictionaryInternal();
  58. }
  59. /// <summary>
  60. ///
  61. /// </summary>
  62. private void LoadStyleDictionaryInternal()
  63. {
  64. if (m_StyleLookupDictionary == null)
  65. m_StyleLookupDictionary = new Dictionary<int, TMP_Style>();
  66. else
  67. m_StyleLookupDictionary.Clear();
  68. // Read Styles from style list and store them into dictionary for faster access.
  69. for (int i = 0; i < m_StyleList.Count; i++)
  70. {
  71. m_StyleList[i].RefreshStyle();
  72. if (!m_StyleLookupDictionary.ContainsKey(m_StyleList[i].hashCode))
  73. m_StyleLookupDictionary.Add(m_StyleList[i].hashCode, m_StyleList[i]);
  74. }
  75. // Add Normal Style if it does not already exists
  76. int normalStyleHashCode = TMP_TextParsingUtilities.GetHashCode("Normal");
  77. if (!m_StyleLookupDictionary.ContainsKey(normalStyleHashCode))
  78. {
  79. TMP_Style style = new TMP_Style("Normal", string.Empty, string.Empty);
  80. m_StyleList.Add(style);
  81. m_StyleLookupDictionary.Add(normalStyleHashCode, style);
  82. }
  83. #if UNITY_EDITOR
  84. //// Event to update objects when styles are changed in the editor.
  85. TMPro_EventManager.ON_TEXT_STYLE_PROPERTY_CHANGED(true);
  86. #endif
  87. }
  88. }
  89. }