Exemplo n.º 1
0
    static public void ChangeSpriteAspectSnap(UI2DSprite ui_sprite2d, Sprite sprite, Vector2 raw_size)
    {
        if (sprite == null)
        {
            return;
        }

        ui_sprite2d.sprite2D = sprite;

        float ratio = sprite.rect.width / sprite.rect.height;

        ui_sprite2d.SetDimensions(Mathf.RoundToInt(ratio * raw_size.y), Mathf.RoundToInt(raw_size.y));

        if (raw_size.x < ui_sprite2d.width)
        {
            ui_sprite2d.SetDimensions(Mathf.RoundToInt(raw_size.x), Mathf.RoundToInt(raw_size.x / ratio));
        }
        ui_sprite2d.Update();
    }