![](/img/trans.png)
[英]Unity3D looking for setPixels example to paint with texture in C#
[英]Unity - Texture2D is blank after SetPixels
Texture2D crop = new Texture2D(size, size);
crop.SetPixels(texture.GetPixels(x, y, size, size));
crop.Apply();
Texture2D texture
不是一个空白的黑色屏幕(它是我试图裁剪的彩色图像),但是在这块代码crop
之后只是一个黑色纹理。 执行代码时不会抛出任何错误。 变量值如下:
x = 80;
y = 0;
size = 480;
texture.width = 640;
texture.height = 480;
此代码用于将图像裁剪为正方形。
完整的代码是这样的:
WebCamTexture texture = new WebCamTexture(device.name);
texture.Play();
int x, y, size;
if (texture.width > texture.height)
{
y = 0;
x = texture.width / 2 - texture.height / 2;
size = texture.height;
}
else if (texture.height > texture.width)
{
x = 0;
y = texture.height / 2 - texture.width / 2;
size = texture.width;
}
else
{
x = 0;
y = 0;
size = texture.width;
}
Texture2D crop = new Texture2D(size, size);
crop.SetPixels(texture.GetPixels(x, y, size, size));
crop.Apply();
在我看来,您正在创建一个新纹理,但从未设置数据引用。 这需要通过加载它或单独设置每个像素来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.