简体   繁体   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();

The Texture2D texture is not a blank black screen (it's a colorful image which I'm trying to crop into), but after this chunk of code crop is just a black texture. Texture2D texture不是一个空白的黑色屏幕(它是我试图裁剪的彩色图像),但是在这块代码crop之后只是一个黑色纹理。 No errors are thrown when the code is excecuted.执行代码时不会抛出任何错误。 The variables values are the following:变量值如下:

x = 80;
y = 0;
size = 480;
texture.width = 640;
texture.height = 480;

This code is for cropping an image down to a square.此代码用于将图像裁剪为正方形。

The full code is this:完整的代码是这样的:

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();

The way I see it you are creating a new texture but never setting a reference of data.在我看来,您正在创建一个新纹理,但从未设置数据引用。 This needs to be done by either loading it or setting each pixel individually.这需要通过加载它或单独设置每个像素来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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