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

时间:2017-07-12 20:09:03

标签: c# unity3d textures sprite

我希望这样做的原因是Unity有一个很好的DTX5格式,可以大大减少文件大小。但要想到这一点,我需要一个大小为高度和宽度的精灵 - 为4的倍数。 所以我想我创建了一个具有所需大小的新纹理,用原始像素加载其像素并从中创建一个精灵,我保存为asset。 问题是,在保存纹理的同时,我会使用适当的大小获得相同的纹理,从而节省精灵不起作用。它会吐出一些东西,但这甚至不能满足我的需求。

以下是代码:

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");
    }

}

以下是我的结果:

original sprite

the sad outcome

第一个是原始精灵,第二个是我给的。

编辑:即使我不保存我的创作,只需将其实例化为GameObject,结果仍然是相同的丑陋。

1 个答案:

答案 0 :(得分:2)

你真的不需要所有这些代码。Texture2D有一个resize函数,所以只需从Sprite中提取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");
}

至于你原来的问题,那是因为你没有调用Apply功能。每次修改像素时,都应该调用Apply函数。最后,始终使用GetPixels32而不是GetPixelGetPixels。原因是因为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");
}