Version: 2022.1
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. Texture.isReadable must be true.

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(); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961