Hue Preserving Color Blending
TMP_StyleSheet.cs
1 using UnityEngine;
2 using System;
3 using System.Collections.Generic;
4 
5 
6 namespace TMPro
7 {
8 
9  [Serializable]
10  public class TMP_StyleSheet : ScriptableObject
11  {
12  private static TMP_StyleSheet s_Instance;
13 
14  [SerializeField]
15  private List<TMP_Style> m_StyleList = new List<TMP_Style>(1);
16  private Dictionary<int, TMP_Style> m_StyleDictionary = new Dictionary<int, TMP_Style>();
17 
18 
22  public static TMP_StyleSheet instance
23  {
24  get
25  {
26  if (s_Instance == null)
27  {
28  s_Instance = TMP_Settings.defaultStyleSheet;
29 
30  if (s_Instance == null)
31  s_Instance = Resources.Load<TMP_StyleSheet>("Style Sheets/TMP Default Style Sheet");
32 
33  if (s_Instance == null) return null;
34 
35  // Load the style dictionary.
36  s_Instance.LoadStyleDictionaryInternal();
37  }
38 
39  return s_Instance;
40  }
41  }
42 
43 
49  {
50  return instance;
51  }
52 
53 
59  public static TMP_Style GetStyle(int hashCode)
60  {
61  return instance.GetStyleInternal(hashCode);
62  }
63 
64 
70  private TMP_Style GetStyleInternal(int hashCode)
71  {
72  TMP_Style style;
73  if (m_StyleDictionary.TryGetValue(hashCode, out style))
74  {
75  return style;
76  }
77 
78  return null;
79  }
80 
81 
82  public void UpdateStyleDictionaryKey(int old_key, int new_key)
83  {
84  if (m_StyleDictionary.ContainsKey(old_key))
85  {
86  TMP_Style style = m_StyleDictionary[old_key];
87  m_StyleDictionary.Add(new_key, style);
88  m_StyleDictionary.Remove(old_key);
89  }
90  }
91 
92 
96  public static void UpdateStyleSheet()
97  {
98  // Reset instance
99  s_Instance = null;
100 
101  RefreshStyles();
102  }
103 
104 
108  public static void RefreshStyles()
109  {
111  }
112 
113 
118  {
119  m_StyleDictionary.Clear();
120 
121  // Read Styles from style list and store them into dictionary for faster access.
122  for (int i = 0; i < m_StyleList.Count; i++)
123  {
124  m_StyleList[i].RefreshStyle();
125 
126  if (!m_StyleDictionary.ContainsKey(m_StyleList[i].hashCode))
127  m_StyleDictionary.Add(m_StyleList[i].hashCode, m_StyleList[i]);
128  }
129  }
130  }
131 
132 }
static TMP_StyleSheet defaultStyleSheet
The Default Style Sheet used by the text objects.
TMP_Style GetStyleInternal(int hashCode)
Internal method to retrieve the Style matching the Hashcode.
static TMP_StyleSheet instance
Get a singleton instance of the TMP_StyleSheet
static void RefreshStyles()
Function to refresh the Style Dictionary.
static void UpdateStyleSheet()
Function to update the internal reference to a newly assigned style sheet in the TMP Settings.
void LoadStyleDictionaryInternal()
static TMP_StyleSheet LoadDefaultStyleSheet()
Static Function to load the Default Style Sheet.
static TMP_Style GetStyle(int hashCode)
Function to retrieve the Style matching the HashCode.