/// <summary> /// /// </summary> /// <param name="spriteDataObject"></param> /// <param name="spriteCharacterTable"></param> /// <param name="spriteGlyphTable"></param> private static void PopulateSpriteTables(TexturePacker_JsonArray.SpriteDataObject spriteDataObject, List <TMP_SpriteCharacter> spriteCharacterTable, List <TMP_SpriteGlyph> spriteGlyphTable) { List <TexturePacker_JsonArray.Frame> importedSprites = spriteDataObject.frames; float atlasHeight = spriteDataObject.meta.size.h; for (int i = 0; i < importedSprites.Count; i++) { TexturePacker_JsonArray.Frame spriteData = importedSprites[i]; TMP_SpriteGlyph spriteGlyph = new TMP_SpriteGlyph(); spriteGlyph.index = (uint)i; spriteGlyph.metrics = new GlyphMetrics((int)spriteData.frame.w, (int)spriteData.frame.h, -spriteData.frame.w * spriteData.pivot.x, spriteData.frame.h * spriteData.pivot.y, (int)spriteData.frame.w); spriteGlyph.glyphRect = new GlyphRect((int)spriteData.frame.x, (int)(atlasHeight - spriteData.frame.h - spriteData.frame.y), (int)spriteData.frame.w, (int)spriteData.frame.h); spriteGlyph.scale = 1.0f; spriteGlyphTable.Add(spriteGlyph); TMP_SpriteCharacter spriteCharacter = new TMP_SpriteCharacter(0, spriteGlyph); spriteCharacter.name = spriteData.filename.Split('.')[0]; spriteCharacter.scale = 1.0f; spriteCharacterTable.Add(spriteCharacter); } }
/// <summary> /// Constructor for new sprite character. /// </summary> /// <param name="unicode">Unicode value of the sprite character.</param> /// <param name="glyph">Glyph used by the sprite character.</param> public TMP_SpriteCharacter(uint unicode, TMP_SpriteGlyph glyph) { m_ElementType = TextElementType.Sprite; this.unicode = unicode; this.glyphIndex = glyph.index; this.glyph = glyph; this.scale = 1.0f; }
/// <summary> /// Internal method used to upgrade sprite asset. /// </summary> private void UpgradeSpriteAsset() { m_Version = "1.1.0"; Debug.Log("Upgrading sprite asset [" + this.name + "] to version " + m_Version + ".", this); // Convert legacy glyph and character tables to new format m_SpriteCharacterTable.Clear(); m_SpriteGlyphTable.Clear(); for (int i = 0; i < spriteInfoList.Count; i++) { TMP_Sprite oldSprite = spriteInfoList[i]; TMP_SpriteGlyph spriteGlyph = new TMP_SpriteGlyph(); spriteGlyph.index = (uint)i; spriteGlyph.sprite = oldSprite.sprite; spriteGlyph.metrics = new GlyphMetrics(oldSprite.width, oldSprite.height, oldSprite.xOffset, oldSprite.yOffset, oldSprite.xAdvance); spriteGlyph.glyphRect = new GlyphRect((int)oldSprite.x, (int)oldSprite.y, (int)oldSprite.width, (int)oldSprite.height); spriteGlyph.scale = 1.0f; spriteGlyph.atlasIndex = 0; m_SpriteGlyphTable.Add(spriteGlyph); TMP_SpriteCharacter spriteCharacter = new TMP_SpriteCharacter(); spriteCharacter.glyph = spriteGlyph; spriteCharacter.unicode = oldSprite.unicode == 0x0 ? 0xFFFE : (uint)oldSprite.unicode; spriteCharacter.name = oldSprite.name; spriteCharacter.scale = oldSprite.scale; m_SpriteCharacterTable.Add(spriteCharacter); } // Clear legacy glyph info list. //spriteInfoList.Clear(); UpdateLookupTables(); #if UNITY_EDITOR UnityEditor.EditorUtility.SetDirty(this); UnityEditor.AssetDatabase.SaveAssets(); #endif }
/// <summary> /// Function to update the sprite name and unicode lookup tables. /// This function should be called when a sprite's name or unicode value changes or when a new sprite is added. /// </summary> public void UpdateLookupTables() { //Debug.Log("Updating [" + this.name + "] Lookup tables."); // Check version number of sprite asset to see if it needs to be upgraded. if (this.material != null && string.IsNullOrEmpty(m_Version)) { UpgradeSpriteAsset(); } // Initialize / Clear glyph index lookup dictionary. if (m_GlyphIndexLookup == null) { m_GlyphIndexLookup = new Dictionary <uint, int>(); } else { m_GlyphIndexLookup.Clear(); } // if (m_SpriteGlyphLookup == null) { m_SpriteGlyphLookup = new Dictionary <uint, TMP_SpriteGlyph>(); } else { m_SpriteGlyphLookup.Clear(); } // Initialize SpriteGlyphLookup for (int i = 0; i < m_SpriteGlyphTable.Count; i++) { TMP_SpriteGlyph spriteGlyph = m_SpriteGlyphTable[i]; uint glyphIndex = spriteGlyph.index; if (m_GlyphIndexLookup.ContainsKey(glyphIndex) == false) { m_GlyphIndexLookup.Add(glyphIndex, i); } if (m_SpriteGlyphLookup.ContainsKey(glyphIndex) == false) { m_SpriteGlyphLookup.Add(glyphIndex, spriteGlyph); } } // Initialize name lookup if (m_NameLookup == null) { m_NameLookup = new Dictionary <int, int>(); } else { m_NameLookup.Clear(); } // Initialize character lookup if (m_SpriteCharacterLookup == null) { m_SpriteCharacterLookup = new Dictionary <uint, TMP_SpriteCharacter>(); } else { m_SpriteCharacterLookup.Clear(); } // Populate Sprite Character lookup tables for (int i = 0; i < m_SpriteCharacterTable.Count; i++) { TMP_SpriteCharacter spriteCharacter = m_SpriteCharacterTable[i]; // Make sure sprite character is valid if (spriteCharacter == null) { continue; } uint glyphIndex = spriteCharacter.glyphIndex; // Lookup the glyph for this character if (m_SpriteGlyphLookup.ContainsKey(glyphIndex) == false) { continue; } // Assign glyph and text asset to this character spriteCharacter.glyph = m_SpriteGlyphLookup[glyphIndex]; spriteCharacter.textAsset = this; int nameHashCode = m_SpriteCharacterTable[i].hashCode; if (m_NameLookup.ContainsKey(nameHashCode) == false) { m_NameLookup.Add(nameHashCode, i); } uint unicode = m_SpriteCharacterTable[i].unicode; if (unicode != 0xFFFE && m_SpriteCharacterLookup.ContainsKey(unicode) == false) { m_SpriteCharacterLookup.Add(unicode, spriteCharacter); } } m_IsSpriteAssetLookupTablesDirty = false; }