Skip to content

Format

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

class: Texture2D

Description

Set the pixel colors of a specific area on a texture.

It can be used to set the color information of a specific area in a texture, allowing you to update the pixel data of the texture at runtime.

Parameters

param_nametypedescription
xnumberThe x-axis coordinate.
ynumberThe y-axis coordinate.
blockWidthnumberThe width of the area to be set.
blockHeightnumberThe height of the area to be set.
colorsColor[]The array of colors to be set to the area.
mipLevelnumberThe mip map level.

Return

typedescription
void

Code Example

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

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