Version: 2020.2

Texture3D.SetPixelData

切换到手册
public void SetPixelData (T[] data, int mipLevel, int sourceDataStartIndex);
public void SetPixelData (NativeArray<T> data, int mipLevel, int sourceDataStartIndex);

参数

data 用于初始化纹理像素的数据数组。
mipLevel 要填充的 Mip 级别。
sourceDataStartIndex 要从其开始复制的源数组索引(默认值为 0)。

描述

用原始预格式化数据设置像素值。

此函数使用原始数据填充一个 Mip 级别的纹理像素内存。如果要将压缩或其他非颜色纹理格式数据加载到纹理中,此函数很有用。

传递的数据应具有所需的大小,以便根据其宽度、高度、深度和数据格式填充整个纹理 Mip 级别。否则,Unity 将抛出异常。

Note that not all platforms support compressed 3D Texture data so please consult SystemInfo.supportsCompressed3DTextures if you are using compressed formats.

设置图像数据后,调用 Apply 会将其实际上传到 GPU。

See Also: SetPixels, GetPixelData, Apply, SystemInfo.supportsCompressed3DTextures.

using UnityEngine;

public class ExampleScript : MonoBehaviour { public Texture3D texture; public void Start() { texture = new Texture3D(2, 2, 2, TextureFormat.RGB24, true); var data = new byte[] { 255, 0, 0, // red 0, 255, 0, // green 0, 0, 255, // blue 255, 235, 4, // yellow 128, 0, 0, // dark red 0, 128, 0, // dark green 0, 0, 128, // dark blue 128, 117, 4, // dark yellow }; texture.SetPixelData(data, 0); texture.filterMode = FilterMode.Point; texture.Apply(updateMipmaps: false); } }