示例#1
0
        /// <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 TMP_SpriteAsset SearchForSpriteByHashCode(TMP_SpriteAsset spriteAsset, int hashCode, bool includeFallbacks, out int spriteIndex)
        {
            // Make sure sprite asset is not null
            if (spriteAsset == null)
            {
                spriteIndex = -1; return(null);
            }

            spriteIndex = spriteAsset.GetSpriteIndexFromHashcode(hashCode);
            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 List <int>();
            }

            k_searchedSpriteAssets.Clear();

            int id = spriteAsset.GetInstanceID();

            // Add to list of font assets already searched.
            k_searchedSpriteAssets.Add(id);

            if (includeFallbacks && spriteAsset.fallbackSpriteAssets != null && spriteAsset.fallbackSpriteAssets.Count > 0)
            {
                return(SearchForSpriteByHashCodeInternal(spriteAsset.fallbackSpriteAssets, hashCode, includeFallbacks, out spriteIndex));
            }

            // Search default sprite asset potentially assigned in the TMP Settings.
            if (includeFallbacks && TMP_Settings.defaultSpriteAsset != null)
            {
                return(SearchForSpriteByHashCodeInternal(TMP_Settings.defaultSpriteAsset, hashCode, includeFallbacks, 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, TMP_SpriteAsset spriteAsset)
 {
     MaterialReferenceManager.instance.AddSpriteAssetInternal(hashCode, spriteAsset);
 }
 /// <summary>
 /// Add new Sprite Asset to dictionary.
 /// </summary>
 /// <param name="hashCode"></param>
 /// <param name="spriteAsset"></param>
 public static void AddSpriteAsset(TMP_SpriteAsset spriteAsset)
 {
     MaterialReferenceManager.instance.AddSpriteAssetInternal(spriteAsset);
 }
 /// <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 TMP_SpriteAsset spriteAsset)
 {
     return(MaterialReferenceManager.instance.TryGetSpriteAssetInternal(hashCode, out spriteAsset));
 }
示例#5
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);
            }
        }