Hue Preserving Color Blending
TMP_SpriteAsset.cs
1 using UnityEngine;
2 using System.Collections.Generic;
3 
4 
5 namespace TMPro
6 {
7 
8  public class TMP_SpriteAsset : TMP_Asset
9  {
10  internal Dictionary<int, int> m_UnicodeLookup;
11  internal Dictionary<int, int> m_NameLookup;
12 
17  {
18  get
19  {
20  if (m_defaultSpriteAsset == null)
21  {
22  m_defaultSpriteAsset = Resources.Load<TMP_SpriteAsset>("Sprite Assets/Default Sprite Asset");
23  }
24 
25  return m_defaultSpriteAsset;
26  }
27  }
28  public static TMP_SpriteAsset m_defaultSpriteAsset;
29 
30 
31  // The texture which contains the sprites.
32  public Texture spriteSheet;
33 
34  // List which contains the SpriteInfo for the sprites contained in the sprite sheet.
35  public List<TMP_Sprite> spriteInfoList;
36 
40  //private Dictionary<int, int> m_SpriteUnicodeLookup;
41 
42 
46  [SerializeField]
47  public List<TMP_SpriteAsset> fallbackSpriteAssets;
48 
49 
50  //private bool isEditingAsset;
51 
52  void OnEnable()
53  {
54  // Make sure we have a valid material.
55  //if (this.material == null && !isEditingAsset)
56  // this.material = GetDefaultSpriteMaterial();
57  }
58 
59 
60 #if UNITY_EDITOR
61  void OnValidate()
65  {
67 
68  TMPro_EventManager.ON_SPRITE_ASSET_PROPERTY_CHANGED(true, this);
69  }
70 #endif
71 
72 
78  {
79  //isEditingAsset = true;
80  ShaderUtilities.GetShaderPropertyIDs();
81 
82  // Add a new material
83  Shader shader = Shader.Find("TextMeshPro/Sprite");
84  Material tempMaterial = new Material(shader);
85  tempMaterial.SetTexture(ShaderUtilities.ID_MainTex, spriteSheet);
86  tempMaterial.hideFlags = HideFlags.HideInHierarchy;
87 
88 #if UNITY_EDITOR
89  UnityEditor.AssetDatabase.AddObjectToAsset(tempMaterial, this);
90  UnityEditor.AssetDatabase.ImportAsset(UnityEditor.AssetDatabase.GetAssetPath(this));
91 #endif
92  //isEditingAsset = false;
93 
94  return tempMaterial;
95  }
96 
97 
102  public void UpdateLookupTables()
103  {
104  if (m_NameLookup == null) m_NameLookup = new Dictionary<int, int>();
105  m_NameLookup.Clear();
106 
107  if (m_UnicodeLookup == null) m_UnicodeLookup = new Dictionary<int, int>();
108  m_UnicodeLookup.Clear();
109 
110  for (int i = 0; i < spriteInfoList.Count; i++)
111  {
112  int nameHashCode = spriteInfoList[i].hashCode;
113 
114  if (m_NameLookup.ContainsKey(nameHashCode) == false)
115  m_NameLookup.Add(nameHashCode, i);
116 
117  int unicode = spriteInfoList[i].unicode;
118 
119  if (m_UnicodeLookup.ContainsKey(unicode) == false)
120  m_UnicodeLookup.Add(unicode, i);
121  }
122  }
123 
124 
131  {
132  if (m_NameLookup == null)
134 
135  int index = 0;
136  if (m_NameLookup.TryGetValue(hashCode, out index))
137  return index;
138 
139  return -1;
140  }
141 
142 
148  public int GetSpriteIndexFromUnicode (int unicode)
149  {
150  if (m_UnicodeLookup == null)
152 
153  int index = 0;
154  if (m_UnicodeLookup.TryGetValue(unicode, out index))
155  return index;
156 
157  return -1;
158  }
159 
160 
166  public int GetSpriteIndexFromName (string name)
167  {
168  if (m_NameLookup == null)
170 
171  int hashCode = TMP_TextUtilities.GetSimpleHashCode(name);
172 
174  }
175 
176 
180  private static List<int> k_searchedSpriteAssets;
181 
189  public static TMP_SpriteAsset SearchForSpriteByUnicode(TMP_SpriteAsset spriteAsset, int unicode, bool includeFallbacks, out int spriteIndex)
190  {
191  // Check to make sure sprite asset is not null
192  if (spriteAsset == null) { spriteIndex = -1; return null; }
193 
194  // Get sprite index for the given unicode
195  spriteIndex = spriteAsset.GetSpriteIndexFromUnicode(unicode);
196  if (spriteIndex != -1)
197  return spriteAsset;
198 
199  // Initialize list to track instance of Sprite Assets that have already been searched.
200  if (k_searchedSpriteAssets == null)
201  k_searchedSpriteAssets = new List<int>();
202 
203  k_searchedSpriteAssets.Clear();
204 
205  // Get instance ID of sprite asset and add to list.
206  int id = spriteAsset.GetInstanceID();
207  k_searchedSpriteAssets.Add(id);
208 
209  // Search potential fallback sprite assets if includeFallbacks is true.
210  if (includeFallbacks && spriteAsset.fallbackSpriteAssets != null && spriteAsset.fallbackSpriteAssets.Count > 0)
211  return SearchForSpriteByUnicodeInternal(spriteAsset.fallbackSpriteAssets, unicode, includeFallbacks, out spriteIndex);
212 
213  // Search default sprite asset potentially assigned in the TMP Settings.
214  if (includeFallbacks && TMP_Settings.defaultSpriteAsset != null)
215  return SearchForSpriteByUnicodeInternal(TMP_Settings.defaultSpriteAsset, unicode, includeFallbacks, out spriteIndex);
216 
217  spriteIndex = -1;
218  return null;
219  }
220 
221 
230  private static TMP_SpriteAsset SearchForSpriteByUnicodeInternal(List<TMP_SpriteAsset> spriteAssets, int unicode, bool includeFallbacks, out int spriteIndex)
231  {
232  for (int i = 0; i < spriteAssets.Count; i++)
233  {
234  TMP_SpriteAsset temp = spriteAssets[i];
235  if (temp == null) continue;
236 
237  int id = temp.GetInstanceID();
238 
239  // Skip over the fallback sprite asset if it has already been searched.
240  if (k_searchedSpriteAssets.Contains(id)) continue;
241 
242  // Add to list of font assets already searched.
243  k_searchedSpriteAssets.Add(id);
244 
245  temp = SearchForSpriteByUnicodeInternal(temp, unicode, includeFallbacks, out spriteIndex);
246 
247  if (temp != null)
248  return temp;
249  }
250 
251  spriteIndex = -1;
252  return null;
253  }
254 
255 
264  private static TMP_SpriteAsset SearchForSpriteByUnicodeInternal(TMP_SpriteAsset spriteAsset, int unicode, bool includeFallbacks, out int spriteIndex)
265  {
266  // Get sprite index for the given unicode
267  spriteIndex = spriteAsset.GetSpriteIndexFromUnicode(unicode);
268  if (spriteIndex != -1)
269  return spriteAsset;
270 
271  if (includeFallbacks && spriteAsset.fallbackSpriteAssets != null && spriteAsset.fallbackSpriteAssets.Count > 0)
272  return SearchForSpriteByUnicodeInternal(spriteAsset.fallbackSpriteAssets, unicode, includeFallbacks, out spriteIndex);
273 
274  spriteIndex = -1;
275  return null;
276  }
277 
278 
287  public static TMP_SpriteAsset SearchForSpriteByHashCode(TMP_SpriteAsset spriteAsset, int hashCode, bool includeFallbacks, out int spriteIndex)
288  {
289  // Make sure sprite asset is not null
290  if (spriteAsset == null) { spriteIndex = -1; return null; }
291 
292  spriteIndex = spriteAsset.GetSpriteIndexFromHashcode(hashCode);
293  if (spriteIndex != -1)
294  return spriteAsset;
295 
296  // Initialize list to track instance of Sprite Assets that have already been searched.
297  if (k_searchedSpriteAssets == null)
298  k_searchedSpriteAssets = new List<int>();
299 
300  k_searchedSpriteAssets.Clear();
301 
302  int id = spriteAsset.GetInstanceID();
303  // Add to list of font assets already searched.
304  k_searchedSpriteAssets.Add(id);
305 
306  if (includeFallbacks && spriteAsset.fallbackSpriteAssets != null && spriteAsset.fallbackSpriteAssets.Count > 0)
307  return SearchForSpriteByHashCodeInternal(spriteAsset.fallbackSpriteAssets, hashCode, includeFallbacks, out spriteIndex);
308 
309  // Search default sprite asset potentially assigned in the TMP Settings.
310  if (includeFallbacks && TMP_Settings.defaultSpriteAsset != null)
311  return SearchForSpriteByHashCodeInternal(TMP_Settings.defaultSpriteAsset, hashCode, includeFallbacks, out spriteIndex);
312 
313  spriteIndex = -1;
314  return null;
315  }
316 
317 
326  private static TMP_SpriteAsset SearchForSpriteByHashCodeInternal(List<TMP_SpriteAsset> spriteAssets, int hashCode, bool searchFallbacks, out int spriteIndex)
327  {
328  // Search through the list of sprite assets
329  for (int i = 0; i < spriteAssets.Count; i++)
330  {
331  TMP_SpriteAsset temp = spriteAssets[i];
332  if (temp == null) continue;
333 
334  int id = temp.GetInstanceID();
335 
336  // Skip over the fallback sprite asset if it has already been searched.
337  if (k_searchedSpriteAssets.Contains(id)) continue;
338 
339  // Add to list of font assets already searched.
340  k_searchedSpriteAssets.Add(id);
341 
342  temp = SearchForSpriteByHashCodeInternal(temp, hashCode, searchFallbacks, out spriteIndex);
343 
344  if (temp != null)
345  return temp;
346  }
347 
348  spriteIndex = -1;
349  return null;
350  }
351 
352 
361  private static TMP_SpriteAsset SearchForSpriteByHashCodeInternal(TMP_SpriteAsset spriteAsset, int hashCode, bool searchFallbacks, out int spriteIndex)
362  {
363  // Get the sprite for the given hash code.
364  spriteIndex = spriteAsset.GetSpriteIndexFromHashcode(hashCode);
365  if (spriteIndex != -1)
366  return spriteAsset;
367 
368  if (searchFallbacks && spriteAsset.fallbackSpriteAssets != null && spriteAsset.fallbackSpriteAssets.Count > 0)
369  return SearchForSpriteByHashCodeInternal(spriteAsset.fallbackSpriteAssets, hashCode, searchFallbacks, out spriteIndex);
370 
371  spriteIndex = -1;
372  return null;
373  }
374 
375  }
376 }
int hashCode
HashCode based on the name of the asset.
Definition: TMP_Asset.cs:13
static TMP_SpriteAsset SearchForSpriteByHashCodeInternal(List< TMP_SpriteAsset > spriteAssets, int hashCode, bool searchFallbacks, out int spriteIndex)
Search through the given list of sprite assets and fallbacks for a sprite whose hash code value of it...
int GetSpriteIndexFromHashcode(int hashCode)
Function which returns the sprite index using the hashcode of the name
static TMP_SpriteAsset SearchForSpriteByUnicodeInternal(List< TMP_SpriteAsset > spriteAssets, int unicode, bool includeFallbacks, out int spriteIndex)
Search through the given list of sprite assets and fallbacks for a sprite whose unicode value matches...
static TMP_SpriteAsset SearchForSpriteByHashCodeInternal(TMP_SpriteAsset spriteAsset, int hashCode, bool searchFallbacks, out int spriteIndex)
Search through the given sprite asset and fallbacks for a sprite whose hash code value of its name ma...
void UpdateLookupTables()
Function to update the sprite name and unicode lookup tables. This function should be called when a s...
static TMP_SpriteAsset SearchForSpriteByUnicode(TMP_SpriteAsset spriteAsset, int unicode, bool includeFallbacks, out int spriteIndex)
Search through the given sprite asset and its fallbacks for the specified sprite matching the given u...
int GetSpriteIndexFromUnicode(int unicode)
Returns the index of the sprite for the given unicode value.
static TMP_SpriteAsset SearchForSpriteByHashCode(TMP_SpriteAsset spriteAsset, int hashCode, bool includeFallbacks, out int spriteIndex)
Search the given sprite asset and fallbacks for a sprite whose hash code value of its name matches th...
Material GetDefaultSpriteMaterial()
Create a material for the sprite asset.
static List< int > k_searchedSpriteAssets
Used to keep track of which Sprite Assets have been searched.
int GetSpriteIndexFromName(string name)
Returns the index of the sprite for the given name.
List< TMP_SpriteAsset > fallbackSpriteAssets
Dictionary used to lookup the index of a given sprite based on a Unicode value.
static TMP_SpriteAsset SearchForSpriteByUnicodeInternal(TMP_SpriteAsset spriteAsset, int unicode, bool includeFallbacks, out int spriteIndex)
Search the given sprite asset and fallbacks for a sprite whose unicode value matches the target unico...
static TMP_SpriteAsset defaultSpriteAsset
The Default Sprite Asset to be used by default.
static TMP_SpriteAsset defaultSpriteAsset
Static reference to the default font asset included with TextMesh Pro.