Fat*_*tie 9 unity-game-engine unity3d-ui
令人惊讶的团结,多年来只有这样简单地缩放实际PNG是使用非常真棒库http://wiki.unity3d.com/index.php/TextureScale
以下示例
如何使用Unity5功能缩放PNG?现在必须有一种新的UI等方式.
因此,缩放实际像素(例如in Color[])或字面上的PNG文件,可能是从网上下载的.
(顺便说一下,如果你是Unity新手,那么这个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
}
Run Code Online (Sandbox Code Playgroud)
只是BTW它发生在我身上我可能只是在谈论缩小这里(因为你经常要发布图像,动态创建或其他任何东西.)我猜,通常不需要扩大规模大小的图像; 质量毫无意义.
赏金即将来临!
小智 4
如果您可以接受拉伸缩放,实际上有更简单的方法,即使用临时 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
777 次 |
| 最近记录: |