首页 > 解决方案 > 创建尺寸大小的 Sprite

问题描述

我有一个画廊场景,我想从 Persistence path 加载 PNG。

问题是我希望它们作为缩略图,它们不需要我加载完整大小的文件。

如何定义精灵的比例?创建精灵的相关行:

  Sprite sp1 = Sprite.Create(texture1, new Rect(0, 0, texture1.width, texture1.height), new Vector2(0.5f, 0.5f), 100, 0, SpriteMeshType.FullRect);

以及纹理创建代码:

 Texture2D takeScreenShotImage(string filePath)
{
    Texture2D texture = null;
    byte[] fileBytes;
    if (File.Exists(filePath))
    {
        fileBytes = File.ReadAllBytes(filePath);
        texture = new Texture2D(1, 1, TextureFormat.ETC2_RGB, false);

        texture.LoadImage(fileBytes);
    }
    return texture;
}

标签: c#unity3dtexturessprite

解决方案


进行更改的正确位置是在Texture2D加载纹理之后。如果您真的想将它们作为缩略图加载并且希望尺寸更小以节省内存,请Texture2D.Resize在加载纹理后使用该功能调整其大小。60的高度和宽度应该没问题。然后,您可以使用Sprite.Create.

Texture2D takeScreenShotImage(string filePath)
{
    Texture2D texture = null;
    byte[] fileBytes;
    if (File.Exists(filePath))
    {
        fileBytes = File.ReadAllBytes(filePath);
        texture = new Texture2D(1, 1, TextureFormat.ETC2_RGB, false);

        texture.LoadImage(fileBytes);
    }

    //RE-SIZE THE Texture2D
    texture.Resize(60, 60);
    texture.Apply();

    return texture;
}

注意TextureFormat.ETC2_RGB是压缩的。要调整它的大小,您可能必须创建它的副本。请参阅我的另一篇文章中的#1 解决方案,了解如何执行此操作。


推荐阅读