Skip to content

格式

typescript
Texture2D.SetPixels(x, y, blockWidth, blockHeight, colors, mipLevel)

所属类: Texture2D

描述

设置纹理上特定区域的像素颜色。

它可以用于设置纹理中特定区域的颜色信息,使您能够在运行时更新纹理的像素数据。

参数

参数名类型描述
xnumberx轴坐标
ynumbery轴坐标
blockWidthnumber待设置区域的宽度
blockHeightnumber待设置区域的高度
colorsColor[]待设置到区域的颜色数组。
mipLevelnumbermip贴图级别

返回值

类型描述
void

代码示例

typescript
    let tex = Resources.Load<Texture2D>(Texture2D, "texture2dID");
    if (tex == null) {
        throw new Exception("failed to load texture2d: " + "texture2dID");
    }

    let len = tex.width * tex.height;
    let color_arr: Color[] = new Array(len);
    for (let i = 0; i < color_arr.length; i++) {
        color_arr[i] = Color.green;
    }
    tex.SetPixels(0, 0, tex.width, tex.height, color_arr, 0);