C# 通过添加透明像素调整精灵的大小

C# 通过添加透明像素调整精灵的大小,c#,unity3d,textures,sprite,C#,Unity3d,Textures,Sprite,我希望这样做的原因是Unity有一个很好的DTX5格式,可以将文件大小减少很多。但为了达到这个目的,我需要一个大小为4倍的雪碧,无论是高度还是宽度。 因此,我想我创建了一个具有所需大小的新纹理,将其像素与原始像素一起加载,并从中生成一个精灵,保存为资源。 问题是,当保存纹理时,我会得到大小合适的相同纹理,而保存精灵时则不起作用。它吐出了一些东西,但这根本不是我所需要的 代码如下: using System.Collections; using System.Collections.Generic

我希望这样做的原因是Unity有一个很好的DTX5格式,可以将文件大小减少很多。但为了达到这个目的,我需要一个大小为4倍的雪碧,无论是高度还是宽度。 因此,我想我创建了一个具有所需大小的新纹理,将其像素与原始像素一起加载,并从中生成一个精灵,保存为
资源
。 问题是,当保存纹理时,我会得到大小合适的相同纹理,而保存精灵时则不起作用。它吐出了一些东西,但这根本不是我所需要的

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResizeSprites
{
    public void Resize(Sprite sprite)
    {
        int _hei, _wid;
        //getting the closest higher values that are a multiple of 4. 
        for (_hei = sprite.texture.height; _hei % 4 != 0; _hei++) ;
        for (_wid = sprite.texture.width; _wid % 4 != 0; _wid++) ;
        //creating the new texture. 
        Texture2D tex = new Texture2D(_wid, _hei,TextureFormat.RGBA32,false);
        //tex.alphaIsTransparency = true;
        //tex.EncodeToPNG();

       //giving the new texture the "improper" ratio sprite texture's pixel info
        //pixel by pixel.
        for (int wid = 0; wid < sprite.texture.width; wid++)
        {
            for (int hei = 0; hei < sprite.texture.height; hei++)
            {
                tex.SetPixel(wid, hei, sprite.texture.GetPixel(wid, hei));
            }
        }
        //saving the asset. the save works, was used for both meshes as well as textures. 
        Sprite n_spr = Sprite.Create(tex,
            new Rect(0, 0, tex.width, tex.height),
            new Vector2(0.5f, 0.5f), 100.0f);
        AssetSaver.CreateAsset(n_spr, sprite.name + "_dtx5");
    }

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类大小调整精灵
{
公共空间大小调整(精灵精灵)
{
内平,内平,;
//获取最接近的较高值(4的倍数)。
对于(_-hei=sprite.texture.height;_-hei%4!=0;_-hei++);
对于(_wid=sprite.texture.width;_wid%4!=0;_wid++);
//创建新纹理。
Texture2D tex=新的Texture2D(_-wid,_-hei,TextureFormat.RGBA32,false);
//tex.alphasistransparency=真;
//tex.EncodeToPNG();
//为新纹理提供“不正确”比例的精灵纹理的像素信息
//逐像素。
对于(int-wid=0;wid
以下是我的结果:

第一个是原始的精灵,第二个是我得到的


编辑:即使我不保存我的创建,只需将其实例化为一个
游戏对象
,结果仍然是一样的

你真的不需要所有这些代码。
Texture2D
有一个调整大小的功能,所以只需从精灵中拉出
Texture2D
,然后调用re-szie函数来重新调整大小。就这样

大概是这样的:

public void Resize(Sprite sprite)
{
    Texture2D tex = sprite.texture;

    tex.Resize(100, 100, TextureFormat.RGBA32, false);

    Sprite n_spr = Sprite.Create(tex,
        new Rect(0, 0, tex.width, tex.height),
        new Vector2(0.5f, 0.5f), 100.0f);

    AssetSaver.CreateAsset(n_spr, sprite.name + "_dtx5");
}

至于你原来的问题,那是因为你没有调用这个函数。每次修改像素时,都应该调用该函数。最后,始终使用
GetPixels32
而不是
GetPixel
GetPixels
。原因是
GetPixels32
比函数的其余部分速度更快

public void Resize(Sprite sprite)
{
    int _hei, _wid;
    //getting the closest higher values that are a multiple of 4. 
    for (_hei = sprite.texture.height; _hei % 4 != 0; _hei++) ;
    for (_wid = sprite.texture.width; _wid % 4 != 0; _wid++) ;
    //creating the new texture. 
    Texture2D tex = new Texture2D(_wid, _hei, TextureFormat.RGBA32, false);
    //tex.alphaIsTransparency = true;
    //tex.EncodeToPNG();

    //giving the new texture the "improper" ratio sprite texture's pixel info
    //pixel by pixel.

    Color32[] color = sprite.texture.GetPixels32();

    tex.SetPixels32(color);

    tex.Apply();

    //saving the asset. the save works, was used for both meshes as well as textures. 
    Sprite n_spr = Sprite.Create(tex,
        new Rect(0, 0, tex.width, tex.height),
        new Vector2(0.5f, 0.5f), 100.0f);
    AssetSaver.CreateAsset(n_spr, sprite.name + "_dtx5");
}

您是否尝试在for循环之后调用
Texture2D.Apply()
?另外,查看
GetPixels()
SetPixels()
。它们接受
x
y
宽度
高度
,并将在不需要循环的情况下传输颜色数据。您是否考虑过使用外部编辑器?@agiro Yes。如果右键单击现有文件“在资源管理器中显示”,它将弹出一个直接指向资源的文件浏览器窗口。您可以像编辑任何其他文件一样在那里编辑纹理。@Draco18s在将我的“无洞”精灵与未照亮的网格交换后,我剩下的精灵很少了。这些很容易手工编辑,我打算这样做。在编写代码的几天里,我忘记了最简单的解决方案:D谢谢你指出。谢谢你的回复。第一个解决方案确实创建了一个精灵,但它是完全透明的:/第二个解决方案也这样做了:D现在,使用我的旧解决方案和
Apply
方法,效果很好。所有的问题是,虽然我得到了一个精灵作为资源,但我不能修改纹理类型,精灵模式,因此没有导入设置,我可以强制使用DTX5。如何保存它以便我可以修改它们?总之,你完美地回答了我的问题。根据分析器,对于运行时性能,我可以在文件大小上节省至少10%,而不会出现任何明显的质量变化。谢谢你的帮助!没问题。还有
TextureFormat.DXT5
。您可以使用它而不是
TextureFormat.RGBA32
。不过我确实试过了。我在创建新纹理后使用了它,然后再次应用。希望它能这样工作,看起来很好。