コード例 #1
0
        /// <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);
            }
        }
コード例 #2
0
        /// <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
        }
コード例 #3
0
        IEnumerator DoSpriteAnimationInternal(int currentCharacter, TMP_SpriteAsset spriteAsset, int start, int end, int framerate)
        {
            if (m_TextComponent == null)
            {
                yield break;
            }

            // We yield otherwise this gets called before the sprite has rendered.
            yield return(null);

            int currentFrame = start;

            // Make sure end frame does not exceed the number of sprites in the sprite asset.
            if (end > spriteAsset.spriteCharacterTable.Count)
            {
                end = spriteAsset.spriteCharacterTable.Count - 1;
            }

            // Get a reference to the current character's info
            TMP_CharacterInfo charInfo = m_TextComponent.textInfo.characterInfo[currentCharacter];

            int materialIndex = charInfo.materialReferenceIndex;
            int vertexIndex   = charInfo.vertexIndex;

            TMP_MeshInfo meshInfo = m_TextComponent.textInfo.meshInfo[materialIndex];

            float baseSpriteScale = spriteAsset.spriteCharacterTable[start].scale * spriteAsset.spriteCharacterTable[start].glyph.scale;

            float elapsedTime = 0;
            float targetTime  = 1f / Mathf.Abs(framerate);

            while (true)
            {
                if (elapsedTime > targetTime)
                {
                    elapsedTime = 0;

                    // Return if sprite was truncated or replaced by the Ellipsis character.
                    char character = m_TextComponent.textInfo.characterInfo[currentCharacter].character;
                    if (character == 0x03 || character == 0x2026)
                    {
                        m_animations.Remove(currentCharacter);
                        yield break;
                    }

                    // Get a reference to the current sprite
                    TMP_SpriteCharacter spriteCharacter = spriteAsset.spriteCharacterTable[currentFrame];

                    // Update the vertices for the new sprite
                    Vector3[] vertices = meshInfo.vertices;

                    Vector2 origin = new Vector2(charInfo.origin, charInfo.baseLine);

                    float spriteScale = charInfo.scale / baseSpriteScale * spriteCharacter.scale * spriteCharacter.glyph.scale;

                    Vector3 bl = new Vector3(origin.x + spriteCharacter.glyph.metrics.horizontalBearingX * spriteScale, origin.y + (spriteCharacter.glyph.metrics.horizontalBearingY - spriteCharacter.glyph.metrics.height) * spriteScale);
                    Vector3 tl = new Vector3(bl.x, origin.y + spriteCharacter.glyph.metrics.horizontalBearingY * spriteScale);
                    Vector3 tr = new Vector3(origin.x + (spriteCharacter.glyph.metrics.horizontalBearingX + spriteCharacter.glyph.metrics.width) * spriteScale, tl.y);
                    Vector3 br = new Vector3(tr.x, bl.y);

                    vertices[vertexIndex + 0] = bl;
                    vertices[vertexIndex + 1] = tl;
                    vertices[vertexIndex + 2] = tr;
                    vertices[vertexIndex + 3] = br;

                    // Update the UV to point to the new sprite
                    Vector2[] uvs0 = meshInfo.uvs0;

                    Vector2 uv0 = new Vector2((float)spriteCharacter.glyph.glyphRect.x / spriteAsset.spriteSheet.width, (float)spriteCharacter.glyph.glyphRect.y / spriteAsset.spriteSheet.height);
                    Vector2 uv1 = new Vector2(uv0.x, (float)(spriteCharacter.glyph.glyphRect.y + spriteCharacter.glyph.glyphRect.height) / spriteAsset.spriteSheet.height);
                    Vector2 uv2 = new Vector2((float)(spriteCharacter.glyph.glyphRect.x + spriteCharacter.glyph.glyphRect.width) / spriteAsset.spriteSheet.width, uv1.y);
                    Vector2 uv3 = new Vector2(uv2.x, uv0.y);

                    uvs0[vertexIndex + 0] = uv0;
                    uvs0[vertexIndex + 1] = uv1;
                    uvs0[vertexIndex + 2] = uv2;
                    uvs0[vertexIndex + 3] = uv3;

                    // Update the modified vertex attributes
                    meshInfo.mesh.vertices = vertices;
                    meshInfo.mesh.uv       = uvs0;
                    m_TextComponent.UpdateGeometry(meshInfo.mesh, materialIndex);


                    if (framerate > 0)
                    {
                        if (currentFrame < end)
                        {
                            currentFrame += 1;
                        }
                        else
                        {
                            currentFrame = start;
                        }
                    }
                    else
                    {
                        if (currentFrame > start)
                        {
                            currentFrame -= 1;
                        }
                        else
                        {
                            currentFrame = end;
                        }
                    }
                }

                elapsedTime += Time.deltaTime;

                yield return(null);
            }
        }
コード例 #4
0
        /// <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;
        }