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_name | type | description |
|---|---|---|
| x | number | The x-axis coordinate. |
| y | number | The y-axis coordinate. |
| blockWidth | number | The width of the area to be set. |
| blockHeight | number | The height of the area to be set. |
| colors | Color[] | The array of colors to be set to the area. |
| mipLevel | number | The mip map level. |
Return
| type | description |
|---|---|
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);