Exemplo n.º 1
0
        private List <TMP_Sprite> CreateSpriteInfoList(TexturePacker.SpriteDataObject spriteDataObject)
        {
            List <TexturePacker.SpriteData> frames = spriteDataObject.frames;
            List <TMP_Sprite> list = new List <TMP_Sprite>();

            for (int i = 0; i < frames.Count; i++)
            {
                TMP_Sprite tMP_Sprite = new TMP_Sprite();
                tMP_Sprite.id       = i;
                tMP_Sprite.name     = Path.GetFileNameWithoutExtension(frames[i].filename);
                tMP_Sprite.hashCode = TMP_TextUtilities.GetSimpleHashCode(tMP_Sprite.name);
                int num  = 0;
                int num2 = tMP_Sprite.name.IndexOf('-');
                num                 = (tMP_Sprite.unicode = ((num2 == -1) ? TMP_TextUtilities.StringToInt(tMP_Sprite.name) : TMP_TextUtilities.StringToInt(tMP_Sprite.name.Substring(num2 + 1))));
                tMP_Sprite.x        = frames[i].frame.x;
                tMP_Sprite.y        = (float)m_SpriteAtlas.height - (frames[i].frame.y + frames[i].frame.h);
                tMP_Sprite.width    = frames[i].frame.w;
                tMP_Sprite.height   = frames[i].frame.h;
                tMP_Sprite.pivot    = frames[i].pivot;
                tMP_Sprite.xAdvance = tMP_Sprite.width;
                tMP_Sprite.scale    = 1f;
                tMP_Sprite.xOffset  = 0f - tMP_Sprite.width * tMP_Sprite.pivot.x;
                tMP_Sprite.yOffset  = tMP_Sprite.height - tMP_Sprite.height * tMP_Sprite.pivot.y;
                list.Add(tMP_Sprite);
            }
            return(list);
        }
        private IEnumerator DoSpriteAnimationInternal(int currentCharacter, TMP_SpriteAsset spriteAsset, int start, int end, int framerate)
        {
            if (m_TextComponent == null)
            {
                yield break;
            }
            yield return(null);

            int currentFrame = start;

            if (end > spriteAsset.spriteInfoList.Count)
            {
                end = spriteAsset.spriteInfoList.Count - 1;
            }
            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        elapsedTime   = 0f;
            float        targetTime    = 1f / (float)Mathf.Abs(framerate);

            while (true)
            {
                if (elapsedTime > targetTime)
                {
                    elapsedTime = 0f;
                    TMP_Sprite tMP_Sprite = spriteAsset.spriteInfoList[currentFrame];
                    Vector3[]  vertices   = meshInfo.vertices;
                    Vector2    vector     = new Vector2(charInfo.origin, charInfo.baseLine);
                    float      num        = charInfo.fontAsset.fontInfo.Ascender / tMP_Sprite.height * tMP_Sprite.scale * charInfo.scale;
                    Vector3    vector2    = new Vector3(vector.x + tMP_Sprite.xOffset * num, vector.y + (tMP_Sprite.yOffset - tMP_Sprite.height) * num);
                    Vector3    vector3    = new Vector3(vector2.x, vector.y + tMP_Sprite.yOffset * num);
                    Vector3    vector4    = new Vector3(vector.x + (tMP_Sprite.xOffset + tMP_Sprite.width) * num, vector3.y);
                    Vector3    vector5    = new Vector3(vector4.x, vector2.y);
                    vertices[vertexIndex]     = vector2;
                    vertices[vertexIndex + 1] = vector3;
                    vertices[vertexIndex + 2] = vector4;
                    vertices[vertexIndex + 3] = vector5;
                    Vector2[] uvs     = meshInfo.uvs0;
                    Vector2   vector6 = new Vector2(tMP_Sprite.x / (float)spriteAsset.spriteSheet.width, tMP_Sprite.y / (float)spriteAsset.spriteSheet.height);
                    Vector2   vector7 = new Vector2(vector6.x, (tMP_Sprite.y + tMP_Sprite.height) / (float)spriteAsset.spriteSheet.height);
                    Vector2   vector8 = new Vector2((tMP_Sprite.x + tMP_Sprite.width) / (float)spriteAsset.spriteSheet.width, vector7.y);
                    Vector2   vector9 = new Vector2(vector8.x, vector6.y);
                    uvs[vertexIndex]       = vector6;
                    uvs[vertexIndex + 1]   = vector7;
                    uvs[vertexIndex + 2]   = vector8;
                    uvs[vertexIndex + 3]   = vector9;
                    meshInfo.mesh.vertices = vertices;
                    meshInfo.mesh.uv       = uvs;
                    m_TextComponent.UpdateGeometry(meshInfo.mesh, materialIndex);
                    currentFrame = ((framerate > 0) ? ((currentFrame >= end) ? start : (currentFrame + 1)) : ((currentFrame <= start) ? end : (currentFrame - 1)));
                }
                elapsedTime += Time.deltaTime;
                yield return(null);
            }
        }
Exemplo n.º 3
0
        private IEnumerator DoSpriteAnimationInternal(int currentCharacter, TMP_SpriteAsset spriteAsset, int start, int end, int framerate)
        {
            if ((Object)(object)m_TextComponent == null)
            {
                yield break;
            }
            yield return(null);

            int currentFrame = start;

            if (end > spriteAsset.spriteInfoList.Count)
            {
                end = spriteAsset.spriteInfoList.Count - 1;
            }
            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        elapsedTime   = 0f;
            float        targetTime    = 1f / (float)Mathf.Abs(framerate);

            while (true)
            {
                if (elapsedTime > targetTime)
                {
                    elapsedTime = 0f;
                    TMP_Sprite sprite      = spriteAsset.spriteInfoList[currentFrame];
                    Vector3[]  vertices    = meshInfo.vertices;
                    Vector2    origin      = new Vector2(charInfo.origin, charInfo.baseLine);
                    float      spriteScale = charInfo.fontAsset.fontInfo.Ascender / sprite.height * sprite.scale * charInfo.scale;
                    Vector3    bl          = new Vector3(origin.x + sprite.xOffset * spriteScale, origin.y + (sprite.yOffset - sprite.height) * spriteScale);
                    Vector3    tl          = new Vector3(bl.x, origin.y + sprite.yOffset * spriteScale);
                    Vector3    tr          = new Vector3(origin.x + (sprite.xOffset + sprite.width) * spriteScale, tl.y);
                    Vector3    br          = new Vector3(tr.x, bl.y);
                    vertices[vertexIndex]     = bl;
                    vertices[vertexIndex + 1] = tl;
                    vertices[vertexIndex + 2] = tr;
                    vertices[vertexIndex + 3] = br;
                    Vector2[] uvs0 = meshInfo.uvs0;
                    Vector2   uv0  = new Vector2(sprite.x / (float)spriteAsset.spriteSheet.width, sprite.y / (float)spriteAsset.spriteSheet.height);
                    Vector2   uv1  = new Vector2(uv0.x, (sprite.y + sprite.height) / (float)spriteAsset.spriteSheet.height);
                    Vector2   uv2  = new Vector2((sprite.x + sprite.width) / (float)spriteAsset.spriteSheet.width, uv1.y);
                    Vector2   uv3  = new Vector2(uv2.x, uv0.y);
                    uvs0[vertexIndex]      = uv0;
                    uvs0[vertexIndex + 1]  = uv1;
                    uvs0[vertexIndex + 2]  = uv2;
                    uvs0[vertexIndex + 3]  = uv3;
                    meshInfo.mesh.vertices = vertices;
                    meshInfo.mesh.uv       = uvs0;
                    m_TextComponent.UpdateGeometry(meshInfo.mesh, materialIndex);
                    currentFrame = ((framerate > 0) ? ((currentFrame >= end) ? start : (currentFrame + 1)) : ((currentFrame <= start) ? end : (currentFrame - 1)));
                }
                elapsedTime += Time.deltaTime;
                yield return(null);
            }
        }
        /// <summary>
        ///
        /// </summary>
        List <TMP_Sprite> CreateSpriteInfoList(TexturePacker.SpriteDataObject spriteDataObject)
        {
            List <TexturePacker.SpriteData> importedSprites = spriteDataObject.frames;

            List <TMP_Sprite> spriteInfoList = new List <TMP_Sprite>();

            for (int i = 0; i < importedSprites.Count; i++)
            {
                TMP_Sprite sprite = new TMP_Sprite();

                sprite.id       = i;
                sprite.name     = Path.GetFileNameWithoutExtension(importedSprites[i].filename) ?? "";
                sprite.hashCode = TMP_TextUtilities.GetSimpleHashCode(sprite.name);

                // Attempt to extract Unicode value from name
                int unicode;
                int indexOfSeperator = sprite.name.IndexOf('-');
                if (indexOfSeperator != -1)
                {
                    unicode = TMP_TextUtilities.StringToInt(sprite.name.Substring(indexOfSeperator + 1));
                }
                else
                {
                    unicode = TMP_TextUtilities.StringToInt(sprite.name);
                }

                sprite.unicode = unicode;

                sprite.x      = importedSprites[i].frame.x;
                sprite.y      = m_SpriteAtlas.height - (importedSprites[i].frame.y + importedSprites[i].frame.h);
                sprite.width  = importedSprites[i].frame.w;
                sprite.height = importedSprites[i].frame.h;

                //Calculate sprite pivot position
                sprite.pivot = importedSprites[i].pivot;

                // Properties the can be modified
                sprite.xAdvance = sprite.width;
                sprite.scale    = 1.0f;
                sprite.xOffset  = 0 - (sprite.width * sprite.pivot.x);
                sprite.yOffset  = sprite.height - (sprite.height * sprite.pivot.y);

                spriteInfoList.Add(sprite);
            }

            return(spriteInfoList);
        }
Exemplo n.º 5
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
        }
Exemplo n.º 6
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.spriteInfoList.Count)
            {
                end = spriteAsset.spriteInfoList.Count - 1;
            }

            // Get a reference to the geometry of the current character.
            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 elapsedTime = 0;
            float targetTime  = 1f / Mathf.Abs(framerate);

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

                    // Get a reference to the current sprite
                    TMP_Sprite sprite = spriteAsset.spriteInfoList[currentFrame];

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

                    Vector2 origin      = new Vector2(charInfo.origin, charInfo.baseLine);
                    float   spriteScale = charInfo.fontAsset.fontInfo.Ascender / sprite.height * sprite.scale * charInfo.scale;

                    Vector3 bl = new Vector3(origin.x + sprite.xOffset * spriteScale, origin.y + (sprite.yOffset - sprite.height) * spriteScale);
                    Vector3 tl = new Vector3(bl.x, origin.y + sprite.yOffset * spriteScale);
                    Vector3 tr = new Vector3(origin.x + (sprite.xOffset + sprite.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(sprite.x / spriteAsset.spriteSheet.width, sprite.y / spriteAsset.spriteSheet.height);
                    Vector2 uv1 = new Vector2(uv0.x, (sprite.y + sprite.height) / spriteAsset.spriteSheet.height);
                    Vector2 uv2 = new Vector2((sprite.x + sprite.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);
            }
        }