miplevel | 要获取的mipmap 级别。范围为 0 至纹理的 Texture.mipmapCount。默认值为 0 。 |
Color[] 包含像素颜色的数组。
以 颜色结构的形式获取 MIP 贴图级别像素颜色数据。
此方法从 CPU 内存中获取纹理的像素数据。 Texture.isReadable 必须为 true
。
阵列包含像素,从纹理正面开始,逐片切片。每一层水平包含像素行,从纹理的左下角开始。数组大小是 MIP 贴图级别的宽 × 高 × 深度。
每个像素都是一个 颜色 结构。 GetPixels
可能比其他一些纹理方法慢,因为它将纹理使用的格式转换成 颜色。 GetPixels
还需要解压压缩纹理,并使用内存来存储解压后的区域。若要更快地获取像素数据,请使用 GetPixelData。
一次调用 GetPixels
通常比多次调用 GetPixel 的速度更快,尤其对于大纹理。
如果 GetPixels
失败,Unity 将抛出异常。如果数组包含太多数据,GetPixels
可能会失败。对于非常大的纹理,请使用 GetPixelData。
你不能使用 GetPixel
和使用 Crunch 纹理压缩的纹理。请改用 GetPixels32。
using UnityEngine;
public class Texture3DExample : MonoBehaviour { public Texture3D source; public Texture3D destination;
void Start() { // Get a copy of the color data from the source Texture3D, in high-precision float format. // Each element in the array represents the color data for an individual pixel. int sourceMipLevel = 0; Color[] pixels = source.GetPixels(sourceMipLevel);
// If required, manipulate the pixels before applying them to the destination texture. // This example code reverses the array, which rotates the image 180 degrees. System.Array.Reverse(pixels, 0, pixels.Length);
// Set the pixels of the destination Texture3D. int destinationMipLevel = 0; destination.SetPixels(pixels, destinationMipLevel);
// Apply changes to the destination Texture3D, which uploads its data to the GPU. destination.Apply(); } }
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.