【问题标题】:Creating a Sprite in size创建尺寸大小的 Sprite
【发布时间】:2019-01-13 15:01:31
【问题描述】:

我有一个画廊场景,我想从持久性路径加载 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# unity3d textures sprite


    【解决方案1】:

    加载纹理后,进行更改的正确位置是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 已压缩。要调整它的大小,您可能必须创建它的副本。请参阅我的other 帖子中的#1 解决方案,了解如何执行此操作。

    【讨论】:

    • 我添加了“texture.Resize(texture.width/2, texture.height/2);” ,我得到一个“空”文件,我可以在检查器中看到调整大小有效,但文件全是 alpha。可能是什么问题?
    • 不确定但尝试提供纹理格式和mipmap。使用texture.Resize(60, 60, TextureFormat.ETC2_RGB, false);
    • 我试过了,但没有运气。我正在使用 Alpha 图像解决的任何调整大小。
    • 在创建纹理和调整纹理大小的位置启用 mipmap
    • 当我添加纹理格式时,我得到“无法调整为压缩纹理格式”。启用 mipmap 时,我得到“无法创建 2D 纹理 id=568 宽度=1 高度=1 mips=3 dxgifmt=28 [D3D 错误为 80070057]”编辑:更改纹理格式 tp ARGB32 后,错误消失了,但我仍然具有全 alpha 图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2018-08-10
    • 2020-01-24
    • 1970-01-01
    相关资源
    最近更新 更多