Version: 2021.2
public void SetPixel (int x, int y, Color color, int mipLevel= 0);

参数

x X coordinate of the pixel to set.
y Y coordinate of the pixel to set.
color Color to set.
mipLevel Mip level to sample, must be in the range [0, mipCount[.

描述

设置坐标 (x,y) 处的像素颜色。

调用 Apply 可实际将更改的像素上传到显卡。 上传是一项成本很高的操作,因此您想要在 Apply 调用之间尽可能多地 更改像素。

如果您需要在运行时不断重新生成纹理, 则生成一个像素颜色数组并使用 SetPixels 一次性完成设置可能会更快。

This function works only on RGBA32, ARGB32, RGB24 and Alpha8 texture formats. For other formats SetPixel is ignored. The texture also has to have read/write enabled flag set in the texture import settings.

See Also: SetPixels, SetPixelData, GetPixel, Apply.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Start() { Texture2D texture = new Texture2D(128, 128); GetComponent<Renderer>().material.mainTexture = texture;

for (int y = 0; y < texture.height; y++) { for (int x = 0; x < texture.width; x++) { Color color = ((x &amp; y) != 0 ? Color.white : Color.gray); texture.SetPixel(x, y, color); } } texture.Apply(); } }