繁体   English   中英

Unity - SetPixels 后 Texture2D 为空白

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM