/// <summary> /// Search through the given list of sprite assets and fallbacks for a sprite whose hash code value of its name matches the target hash code. /// </summary> /// <param name="spriteAssets"></param> /// <param name="hashCode"></param> /// <param name="searchFallbacks"></param> /// <param name="spriteIndex"></param> /// <returns></returns> private static SpriteAsset SearchForSpriteByHashCodeInternal(List <SpriteAsset> spriteAssets, int hashCode, bool searchFallbacks, out int spriteIndex) { // Search through the list of sprite assets for (int i = 0; i < spriteAssets.Count; i++) { SpriteAsset temp = spriteAssets[i]; if (temp == null) { continue; } int id = temp.instanceID; // Skip sprite asset if it has already been searched. if (k_searchedSpriteAssets.Add(id) == false) { continue; } temp = SearchForSpriteByHashCodeInternal(temp, hashCode, searchFallbacks, out spriteIndex); if (temp != null) { return(temp); } } spriteIndex = -1; return(null); }
/// <summary> /// Search through the given list of sprite assets and fallbacks for a sprite whose unicode value matches the target unicode. /// </summary> /// <param name="spriteAssets"></param> /// <param name="unicode"></param> /// <param name="includeFallbacks"></param> /// <param name="spriteIndex"></param> /// <returns></returns> private static SpriteAsset SearchForSpriteByUnicodeInternal(List <SpriteAsset> spriteAssets, uint unicode, bool includeFallbacks, out int spriteIndex) { for (int i = 0; i < spriteAssets.Count; i++) { SpriteAsset temp = spriteAssets[i]; if (temp == null) { continue; } int id = temp.GetInstanceID(); // Skip sprite asset if it has already been searched. if (k_searchedSpriteAssets.Add(id) == false) { continue; } temp = SearchForSpriteByUnicodeInternal(temp, unicode, includeFallbacks, out spriteIndex); if (temp != null) { return(temp); } } spriteIndex = -1; return(null); }
/// <summary> /// /// </summary> /// <param name="material"></param> /// <param name="spriteAsset"></param> /// <param name="materialReferences"></param> /// <param name="materialReferenceIndexLookup"></param> /// <returns></returns> public static int AddMaterialReference(Material material, SpriteAsset spriteAsset, ref MaterialReference[] materialReferences, Dictionary <int, int> materialReferenceIndexLookup) { int materialId = material.GetInstanceID(); int index; if (materialReferenceIndexLookup.TryGetValue(materialId, out index)) { return(index); } index = materialReferenceIndexLookup.Count; // Add new reference index materialReferenceIndexLookup[materialId] = index; if (index >= materialReferences.Length) { Array.Resize(ref materialReferences, Mathf.NextPowerOfTwo(index + 1)); } materialReferences[index].index = index; materialReferences[index].fontAsset = materialReferences[0].fontAsset; materialReferences[index].spriteAsset = spriteAsset; materialReferences[index].material = material; materialReferences[index].isDefaultMaterial = true; materialReferences[index].referenceCount = 0; return(index); }
/// <summary> /// Constructor for new sprite character. /// </summary> /// <param name="unicode">Unicode value of the sprite character.</param> /// <param name="spriteAsset">Sprite Asset used by this sprite character.</param> /// <param name="glyph">Glyph used by the sprite character.</param> public SpriteCharacter(uint unicode, SpriteAsset spriteAsset, SpriteGlyph glyph) { m_ElementType = TextElementType.Sprite; this.unicode = unicode; this.textAsset = spriteAsset; this.glyph = glyph; this.glyphIndex = glyph.index; this.scale = 1.0f; }
/// <summary> /// Constructor for new Material Reference. /// </summary> /// <param name="index"></param> /// <param name="fontAsset"></param> /// <param name="spriteAsset"></param> /// <param name="material"></param> /// <param name="padding"></param> public MaterialReference(int index, FontAsset fontAsset, SpriteAsset spriteAsset, Material material, float padding) { this.index = index; this.fontAsset = fontAsset; this.spriteAsset = spriteAsset; this.material = material; isDefaultMaterial = material.GetInstanceID() == fontAsset.material.GetInstanceID(); isFallbackMaterial = false; fallbackMaterial = null; this.padding = padding; referenceCount = 0; }
/// <summary> /// Internal method to add a new sprite asset to the dictionary. /// </summary> /// <param name="spriteAsset"></param> void AddSpriteAssetInternal(SpriteAsset spriteAsset) { if (m_SpriteAssetReferenceLookup.ContainsKey(spriteAsset.hashCode)) { return; } // Add reference to sprite asset. m_SpriteAssetReferenceLookup.Add(spriteAsset.hashCode, spriteAsset); // Adding reference to the sprite asset material as well m_FontMaterialReferenceLookup.Add(spriteAsset.hashCode, spriteAsset.material); }
/// <summary> /// Internal method to add a new sprite asset to the dictionary. /// </summary> /// <param name="hashCode"></param> /// <param name="spriteAsset"></param> void AddSpriteAssetInternal(int hashCode, SpriteAsset spriteAsset) { if (m_SpriteAssetReferenceLookup.ContainsKey(hashCode)) { return; } // Add reference to Sprite Asset. m_SpriteAssetReferenceLookup.Add(hashCode, spriteAsset); // Add reference to Sprite Asset using the asset hashcode. m_FontMaterialReferenceLookup.Add(hashCode, spriteAsset.material); }
/// <summary> /// /// </summary> /// <param name="unicode"></param> /// <param name="spriteAsset"></param> /// <param name="includeFallbacks"></param> /// <returns></returns> static SpriteCharacter GetSpriteCharacterFromSpriteAsset_Internal(uint unicode, SpriteAsset spriteAsset, bool includeFallbacks) { SpriteCharacter spriteCharacter; // Search sprite asset for potential sprite character for the given unicode value if (spriteAsset.spriteCharacterLookupTable.TryGetValue(unicode, out spriteCharacter)) { return(spriteCharacter); } if (includeFallbacks) { List <SpriteAsset> fallbackSpriteAsset = spriteAsset.fallbackSpriteAssets; if (fallbackSpriteAsset != null && fallbackSpriteAsset.Count > 0) { int fallbackCount = fallbackSpriteAsset.Count; for (int i = 0; i < fallbackCount; i++) { SpriteAsset temp = fallbackSpriteAsset[i]; if (temp == null) { continue; } int id = temp.instanceID; // Try adding asset to search list. If already present skip to the next one otherwise check if it contains the requested character. if (k_SearchedAssets.Add(id) == false) { continue; } spriteCharacter = GetSpriteCharacterFromSpriteAsset_Internal(unicode, temp, true); if (spriteCharacter != null) { return(spriteCharacter); } } } } return(null); }
/// <summary> /// Search through the given sprite asset and fallbacks for a sprite whose hash code value of its name matches the target hash code. /// </summary> /// <param name="spriteAsset"></param> /// <param name="hashCode"></param> /// <param name="searchFallbacks"></param> /// <param name="spriteIndex"></param> /// <returns></returns> private static SpriteAsset SearchForSpriteByHashCodeInternal(SpriteAsset spriteAsset, int hashCode, bool searchFallbacks, out int spriteIndex) { // Get the sprite for the given hash code. spriteIndex = spriteAsset.GetSpriteIndexFromHashcode(hashCode); if (spriteIndex != -1) { return(spriteAsset); } if (searchFallbacks && spriteAsset.fallbackSpriteAssets != null && spriteAsset.fallbackSpriteAssets.Count > 0) { return(SearchForSpriteByHashCodeInternal(spriteAsset.fallbackSpriteAssets, hashCode, true, out spriteIndex)); } spriteIndex = -1; return(null); }
/// <summary> /// Search the given sprite asset and fallbacks for a sprite whose unicode value matches the target unicode. /// </summary> /// <param name="spriteAsset"></param> /// <param name="unicode"></param> /// <param name="includeFallbacks"></param> /// <param name="spriteIndex"></param> /// <returns></returns> private static SpriteAsset SearchForSpriteByUnicodeInternal(SpriteAsset spriteAsset, uint unicode, bool includeFallbacks, out int spriteIndex) { // Get sprite index for the given unicode spriteIndex = spriteAsset.GetSpriteIndexFromUnicode(unicode); if (spriteIndex != -1) { return(spriteAsset); } if (includeFallbacks && spriteAsset.fallbackSpriteAssets != null && spriteAsset.fallbackSpriteAssets.Count > 0) { return(SearchForSpriteByUnicodeInternal(spriteAsset.fallbackSpriteAssets, unicode, true, out spriteIndex)); } spriteIndex = -1; return(null); }
/// <summary> /// Search through the given sprite asset and its fallbacks for the specified sprite matching the given unicode character. /// </summary> /// <param name="spriteAsset">The sprite asset asset to search for the given unicode.</param> /// <param name="unicode">The unicode character to find.</param> /// <param name="includeFallbacks">Include fallback sprite assets in the search?</param> /// <param name="spriteIndex">The index of the sprite in the sprite asset (if found)</param> /// <returns></returns> public static SpriteAsset SearchForSpriteByUnicode(SpriteAsset spriteAsset, uint unicode, bool includeFallbacks, out int spriteIndex) { // Check to make sure sprite asset is not null if (spriteAsset == null) { spriteIndex = -1; return(null); } // Get sprite index for the given unicode spriteIndex = spriteAsset.GetSpriteIndexFromUnicode(unicode); if (spriteIndex != -1) { return(spriteAsset); } // Initialize list to track instance of Sprite Assets that have already been searched. if (k_searchedSpriteAssets == null) { k_searchedSpriteAssets = new HashSet <int>(); } else { k_searchedSpriteAssets.Clear(); } // Get instance ID of sprite asset and add to list. int id = spriteAsset.GetInstanceID(); k_searchedSpriteAssets.Add(id); // Search potential fallback sprite assets if includeFallbacks is true. if (includeFallbacks && spriteAsset.fallbackSpriteAssets != null && spriteAsset.fallbackSpriteAssets.Count > 0) { return(SearchForSpriteByUnicodeInternal(spriteAsset.fallbackSpriteAssets, unicode, true, out spriteIndex)); } // Search default sprite asset potentially assigned in the TMP Settings. //if (includeFallbacks && TMP_Settings.defaultSpriteAsset != null) // return SearchForSpriteByUnicodeInternal(TMP_Settings.defaultSpriteAsset, unicode, true, out spriteIndex); spriteIndex = -1; return(null); }
/// <summary> /// Add new Sprite Asset to dictionary. /// </summary> /// <param name="hashCode"></param> /// <param name="spriteAsset"></param> public static void AddSpriteAsset(int hashCode, SpriteAsset spriteAsset) { instance.AddSpriteAssetInternal(hashCode, spriteAsset); }
/// <summary> /// Function to check if the sprite asset is already referenced. /// </summary> /// <param name="sprite"></param> /// <returns></returns> public bool Contains(SpriteAsset sprite) { return(m_FontAssetReferenceLookup.ContainsKey(sprite.hashCode)); }
/// <summary> /// Search the given sprite asset and fallbacks for a sprite whose hash code value of its name matches the target hash code. /// </summary> /// <param name="spriteAsset">The Sprite Asset to search for the given sprite whose name matches the hashcode value</param> /// <param name="hashCode">The hash code value matching the name of the sprite</param> /// <param name="includeFallbacks">Include fallback sprite assets in the search</param> /// <param name="spriteIndex">The index of the sprite matching the provided hash code</param> /// <returns>The Sprite Asset that contains the sprite</returns> public static SpriteAsset SearchForSpriteByHashCode(SpriteAsset spriteAsset, int hashCode, bool includeFallbacks, out int spriteIndex, TextSettings textSettings = null) { // Make sure sprite asset is not null if (spriteAsset == null) { spriteIndex = -1; return(null); } spriteIndex = spriteAsset.GetSpriteIndexFromHashcode(hashCode); if (spriteIndex != -1) { return(spriteAsset); } // Initialize or clear list to Sprite Assets that have already been searched. if (k_searchedSpriteAssets == null) { k_searchedSpriteAssets = new HashSet <int>(); } else { k_searchedSpriteAssets.Clear(); } int id = spriteAsset.instanceID; // Add to list of font assets already searched. k_searchedSpriteAssets.Add(id); SpriteAsset tempSpriteAsset; // Search potential local fallbacks assigned to the sprite asset. if (includeFallbacks && spriteAsset.fallbackSpriteAssets != null && spriteAsset.fallbackSpriteAssets.Count > 0) { tempSpriteAsset = SearchForSpriteByHashCodeInternal(spriteAsset.fallbackSpriteAssets, hashCode, true, out spriteIndex); if (spriteIndex != -1) { return(tempSpriteAsset); } } // Early exist if text settings is null if (textSettings == null) { spriteIndex = -1; return(null); } // Search default sprite asset potentially assigned in the Text Settings. if (includeFallbacks && textSettings.defaultSpriteAsset != null) { tempSpriteAsset = SearchForSpriteByHashCodeInternal(textSettings.defaultSpriteAsset, hashCode, true, out spriteIndex); if (spriteIndex != -1) { return(tempSpriteAsset); } } // Clear search list since we are now looking for the missing sprite character. k_searchedSpriteAssets.Clear(); uint missingSpriteCharacterUnicode = textSettings.missingSpriteCharacterUnicode; // Get sprite index for the given unicode spriteIndex = spriteAsset.GetSpriteIndexFromUnicode(missingSpriteCharacterUnicode); if (spriteIndex != -1) { return(spriteAsset); } // Add current sprite asset to list of assets already searched. k_searchedSpriteAssets.Add(id); // Search for the missing sprite character in the local sprite asset and potential fallbacks. if (includeFallbacks && spriteAsset.fallbackSpriteAssets != null && spriteAsset.fallbackSpriteAssets.Count > 0) { tempSpriteAsset = SearchForSpriteByUnicodeInternal(spriteAsset.fallbackSpriteAssets, missingSpriteCharacterUnicode, true, out spriteIndex); if (spriteIndex != -1) { return(tempSpriteAsset); } } // Search for the missing sprite character in the default sprite asset and potential fallbacks. if (includeFallbacks && textSettings.defaultSpriteAsset != null) { tempSpriteAsset = SearchForSpriteByUnicodeInternal(textSettings.defaultSpriteAsset, missingSpriteCharacterUnicode, true, out spriteIndex); if (spriteIndex != -1) { return(tempSpriteAsset); } } spriteIndex = -1; return(null); }
/// <summary> /// Function returning the Sprite Asset corresponding to the provided hash code. /// </summary> /// <param name="hashCode"></param> /// <param name="spriteAsset"></param> /// <returns></returns> public static bool TryGetSpriteAsset(int hashCode, out SpriteAsset spriteAsset) { return(instance.TryGetSpriteAssetInternal(hashCode, out spriteAsset)); }
/// <summary> /// Add new Sprite Asset to dictionary. /// </summary> /// <param name="spriteAsset"></param> public static void AddSpriteAsset(SpriteAsset spriteAsset) { instance.AddSpriteAssetInternal(spriteAsset); }
/// <summary> /// Internal function returning the Sprite Asset corresponding to the provided hash code. /// </summary> /// <param name="hashCode"></param> /// <param name="spriteAsset"></param> /// <returns></returns> bool TryGetSpriteAssetInternal(int hashCode, out SpriteAsset spriteAsset) { spriteAsset = null; return(m_SpriteAssetReferenceLookup.TryGetValue(hashCode, out spriteAsset)); }