创建大小为 Sprite

Creating a Sprite in size

我有一个画廊场景,我想从 Persistence 路径加载 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;
}

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

从它创建 Sprite
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 已压缩。要调整它的大小,您可能必须创建它的副本。请参阅我的 post 中的 #1 解决方案,了解如何执行此操作。