/// <summary>
 /// Get original sprite size from GuiAtlas.
 /// </summary>
 /// <returns>The original size.</returns>
 public Vector2i GetOriginalSize()
 {
     if (SpriteAtlas != null && !string.IsNullOrEmpty(SpriteName))
     {
         var sprData = SpriteAtlas.GetSpriteData(SpriteName);
         return(new Vector2i(
                    (int)(sprData.CornerW * SpriteAtlas.ColorTexture.width),
                    (int)(sprData.CornerH * SpriteAtlas.ColorTexture.height)));
     }
     return(Vector2i.zero);
 }
        /// <summary>
        /// Force revalidate visuals.
        /// </summary>
        /// <returns>true, if visuals were updated.</returns>
        /// <param name="changes">What should be revalidate.</param>
        public override bool UpdateVisuals(GuiDirtyType changes)
        {
            if (!base.UpdateVisuals(changes))
            {
                return(false);
            }

            if ((changes & (GuiDirtyType.Geometry | GuiDirtyType.Panel)) != GuiDirtyType.None)
            {
                if (SpriteAtlas != null && SpriteAtlas.ColorTexture != null)
                {
                    _meshRenderer.sharedMaterial = Panel.GetMaterial(SpriteAtlas);
                    if ((changes & GuiDirtyType.Geometry) != GuiDirtyType.None)
                    {
                        var w       = _isSpriteFlippedHorizontal ? -Width : Width;
                        var h       = _isSpriteFlippedVertical ? -Height : Height;
                        var sprData = SpriteAtlas.GetSpriteData(SpriteName);
                        switch (SpriteType)
                        {
                        case GuiSpriteType.RoundFilled:
                            GuiMeshTools.FillRoundFilledSprite(_meshFilter.sharedMesh, w, h, FillValue, Color, sprData);
                            break;

                        case GuiSpriteType.Sliced:
                        case GuiSpriteType.TiledHorizontal:
                        case GuiSpriteType.TiledVertical:
                        case GuiSpriteType.TiledBoth:
                            var texSize    = new Vector2(SpriteAtlas.ColorTexture.width, SpriteAtlas.ColorTexture.height);
                            var isHorTiled = SpriteType == GuiSpriteType.TiledBoth || SpriteType == GuiSpriteType.TiledHorizontal;
                            var isVerTiled = SpriteType == GuiSpriteType.TiledBoth || SpriteType == GuiSpriteType.TiledVertical;
                            GuiMeshTools.FillSlicedTiledSprite(
                                _meshFilter.sharedMesh, w, h, Color, sprData,
                                texSize, isHorTiled, isVerTiled, IsSpriteCenterFilled);
                            break;

                        default:
                            GuiMeshTools.FillSimpleSprite(_meshFilter.sharedMesh, w, h, Color, sprData);
                            break;
                        }
                    }
                }
                else
                {
                    _meshFilter.sharedMesh.Clear(true);
                }
            }
            return(true);
        }
        /// <summary>
        /// Align tiled sprites for 100% filling internal part.
        /// </summary>
        public void AlignTiledSizeToOriginal()
        {
            if (SpriteAtlas != null && !string.IsNullOrEmpty(SpriteName))
            {
                var sprData = SpriteAtlas.GetSpriteData(SpriteName);
                int srcWidthBorder;
                int srcWidthCenter;
                int srcHeightBorder;
                int srcHeightCenter;
                switch (SpriteType)
                {
                case GuiSpriteType.TiledHorizontal:
                    srcWidthBorder = (int)((sprData.BorderL + sprData.BorderR) * SpriteAtlas.ColorTexture.width);
                    srcWidthCenter = (int)(sprData.CenterWidth * SpriteAtlas.ColorTexture.width);
                    Width          = Mathf.RoundToInt((Width - srcWidthBorder) / (float)srcWidthCenter) * srcWidthCenter + srcWidthBorder;
                    break;

                case GuiSpriteType.TiledVertical:
                    srcHeightBorder = (int)((sprData.BorderT + sprData.BorderB) * SpriteAtlas.ColorTexture.height);
                    srcHeightCenter = (int)(sprData.CenterHeight * SpriteAtlas.ColorTexture.height);
                    Height          = Mathf.RoundToInt((Height - srcHeightBorder) / (float)srcHeightCenter) * srcHeightCenter + srcHeightBorder;
                    break;

                case GuiSpriteType.TiledBoth:
                    srcWidthBorder  = (int)((sprData.BorderL + sprData.BorderR) * SpriteAtlas.ColorTexture.width);
                    srcHeightBorder = (int)((sprData.BorderT + sprData.BorderB) * SpriteAtlas.ColorTexture.height);
                    srcWidthCenter  = (int)(sprData.CenterWidth * SpriteAtlas.ColorTexture.width);
                    srcHeightCenter = (int)(sprData.CenterHeight * SpriteAtlas.ColorTexture.height);
                    Width           = Mathf.RoundToInt((Width - srcWidthBorder) / (float)srcWidthCenter) * srcWidthCenter + srcWidthBorder;
                    Height          = Mathf.RoundToInt((Height - srcHeightBorder) / (float)srcHeightCenter) * srcHeightCenter + srcHeightBorder;
                    break;

                default:
                    var srcWidth  = (int)(sprData.CornerW * SpriteAtlas.ColorTexture.width);
                    var srcHeight = (int)(sprData.CornerH * SpriteAtlas.ColorTexture.height);
                    Width  = Mathf.RoundToInt(Width / (float)srcWidth) * srcWidth;
                    Height = Mathf.RoundToInt(Height / (float)srcHeight) * srcHeight;
                    break;
                }
            }
        }