令人惊讶的是,多年来,简单地将实际的PNG扩展为的唯一方法是使用非常棒的库http://wiki.unity3d.com/index.php/TextureScale
下面的例子
如何使用Unity5函数对PNG进行缩放?现在必须有一种方法来使用新的UI等等。
因此,缩放实际像素(如在Color[]中)或字面上的PNG文件,可能是从网络下载的。
(顺便说一句,如果您是联合公司的新手,那么Resize调用是不相关的。它只是改变了数组的大小。)
public WebCamTexture wct;
public void UseFamousLibraryToScale()
{
// take the photo. scale down to 256
// also crop to a central-square
WebCamTexture wct;
int oldW = wct.width; // NOTE example code assumes wider than high
int oldH = wct.height;
Texture2D photo = new Texture2D(oldW, oldH,
TextureFormat.ARGB32, false);
//consider WaitForEndOfFrame() before GetPixels
photo.SetPixels( 0,0,oldW,oldH, wct.GetPixels() );
photo.Apply();
int newH = 256;
int newW = Mathf.FloorToInt(
((float)newH/(float)oldH) * oldW );
// use a famous Unity library to scale
TextureScale.Bilinear(photo, newW,newH);
// crop to central square 256.256
int startAcross = (newW - 256)/2;
Color[] pix = photo.GetPixels(startAcross,0, 256,256);
photo = new Texture2D(256,256, TextureFormat.ARGB32, false);
photo.SetPixels(pix);
photo.Apply();
demoImage.texture = photo;
// consider WriteAllBytes(
// Application.persistentDataPath+"p.png",
// photo.EncodeToPNG()); etc
}对我来说,我可能只是在说缩小(就像你经常要做的那样,发布一张图片,在飞行中创建一些东西,或者什么的)。我想,通常情况下,不需要放大一幅图像;这是毫无意义的质量。
发布于 2017-06-05 15:37:19
如果您对扩展缩放没有意见,那么使用临时的RenderTexture和Graphics.Blit实际上有更简单的方法。如果您需要它是Texture2D,临时交换RenderTexture.active并将其像素读取到Texture2D应该可以做到这一点。例如:
public Texture2D ScaleTexture(Texture src, int width, int height){
RenderTexture rt = RenderTexture.GetTemporary(width, height);
Graphics.Blit(src, rt);
RenderTexture currentActiveRT = RenderTexture.active;
RenderTexture.active = rt;
Texture2D tex = new Texture2D(rt.width,rt.height);
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
tex.Apply();
RenderTexture.ReleaseTemporary(rt);
RenderTexture.active = currentActiveRT;
return tex;
}https://stackoverflow.com/questions/35713481
复制相似问题