通过添加透明像素调整精灵的大小
Resizing a sprite by adding transparent pixels
我希望这样做的原因是 Unity 有一个很好的 DTX5 格式,可以大大减少文件大小。但要做到这一点,我需要一个精灵的大小——高度和宽度——都是 4 的倍数。
所以我想我创建一个具有所需大小的新纹理,用原始像素加载它的像素并从中制作一个精灵,我将其保存为 asset
。
问题是,虽然保存 texture 有效,但我得到了具有适当尺寸的相同纹理,保存 sprite 不起作用。它会吐出一些东西,但这还远不符合我的需要。
代码如下:
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");
}
}
这是我的结果:
第一个是原图,第二个是给我的。
编辑: 即使我不保存我的创作,只是将它实例化为一个GameObject
,结果还是一样丑。
你真的不需要所有这些代码。Texture2D
有一个调整大小的函数,所以只需从 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
function. Each time you modify the pixels, you are supposed to call the Apply
函数。最后,始终使用 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");
}
我希望这样做的原因是 Unity 有一个很好的 DTX5 格式,可以大大减少文件大小。但要做到这一点,我需要一个精灵的大小——高度和宽度——都是 4 的倍数。
所以我想我创建一个具有所需大小的新纹理,用原始像素加载它的像素并从中制作一个精灵,我将其保存为 asset
。
问题是,虽然保存 texture 有效,但我得到了具有适当尺寸的相同纹理,保存 sprite 不起作用。它会吐出一些东西,但这还远不符合我的需要。
代码如下:
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");
}
}
这是我的结果:
第一个是原图,第二个是给我的。
编辑: 即使我不保存我的创作,只是将它实例化为一个GameObject
,结果还是一样丑。
你真的不需要所有这些代码。Texture2D
有一个调整大小的函数,所以只需从 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
function. Each time you modify the pixels, you are supposed to call the Apply
函数。最后,始终使用 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");
}