data | 用于初始化纹理像素的数据数组。 |
mipLevel | 要填充的 Mip 级别。 |
sourceDataStartIndex | 要从其开始复制的源数组索引(默认值为 0)。 |
用原始预格式化数据设置像素值。
此函数使用原始数据填充一个 Mip 级别的纹理像素内存。如果要将压缩或其他非颜色纹理格式数据加载到纹理中,此函数很有用。
传递的数据应具有所需的大小,以便根据其宽度、高度和数据格式填充整个纹理 Mip 级别。否则,Unity 将抛出异常。
设置图像数据后,调用 Apply 会将其实际上传到 GPU。
See Also: SetPixels, LoadRawTextureData, GetPixelData, ::Apply.
using UnityEngine;
public class ExampleScript : MonoBehaviour { public void Start() { var tex = new Texture2D(2, 2, TextureFormat.RGB24, true); var data = new byte[] { // mip 0: 2x2 size 255, 0, 0, // red 0, 255, 0, // green 0, 0, 255, // blue 255, 235, 4, // yellow // mip 1: 1x1 size 0, 255, 255 // cyan }; tex.SetPixelData(data, 0, 0); // mip 0 tex.SetPixelData(data, 1, 12); // mip 1 tex.filterMode = FilterMode.Point; tex.Apply(updateMipmaps: false);
GetComponent<Renderer>().material.mainTexture = tex; } }
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.