x | 要设置的像素的 x 坐标。范围为 0 至(纹理宽度 - 1)。 |
y | 要设置的像素的 y 坐标。范围为 0 至(纹理高度 - 1)。 |
color | 要设置的颜色。 |
mip | 要写入到的 Mipmap 级别。范围为 0 至纹理的 Texture.mipmapCount。默认值为 0 。 |
face | 要写入到的 CubemapFace。 |
设置坐标 (x
,y
) 的像素颜色。
此方法为 CPU 内存中的纹理设置像素数据。 Texture.isReadable 必须为 true
,并且必须在 SetPixel
后调用 Apply,才能将已更改的像素上载至 GPU。
Apply 是一项高开销操作,因为它会复制纹理中的所有像素,即使你只更改了部分像素也如此,因此在调用它之前尽可能多地更改像素。SetPixel
可能比一些其他纹理方法慢,因为它会将 Color 结构转换为纹理所使用的格式。若要更快速地设置像素数据,请改用 SetPixelData。
某个面的左下角为 (0, 0)。如果像素坐标超出纹理的尺寸,Unity 会根据纹理的 TextureWrapMode 将其裁剪或重复。
如果你需要获取大块像素,则使用 SetPixels 可能会更快。
你可以使用 SetPixel
和以下 纹理格式
对于所有其他格式,Unity 会忽略 SetPixel
。
其他资源:SetPixels、SetPixelData、GetPixel、Apply。
using UnityEngine;
public class Example : MonoBehaviour { public Cubemap cubeMap;
void Start() { // Set the pixel at (0,0) of the +X face red. cubeMap.SetPixel(CubemapFace.PositiveX, 0, 0, Color.red); cubeMap.Apply(); // Apply the stuff done to the Cubemap. } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.