Version: 2017.2
public void Apply (bool updateMipmaps= true, bool makeNoLongerReadable= false);

参数

updateMipmaps 当设置为 true 时,将重新计算多级渐进纹理级别。
makeNoLongerReadable 当设置为 true 时,将释放纹理的系统内存副本。

描述

实际应用任何先前的 SetPixelSetPixels 更改。

如果 updateMipmaps 为 /true/,则还将重新计算多级渐进纹理级别, 并且将基本级别作为源。通常会在所有情况下都使用 /true/, 除非您使用 SetPixels 亲自修改了 Mip 级别。 默认情况下,updateMipmaps 设置为 /true/。

如果 makeNoLongerReadablemakeNoLongerReadable,则纹理将被标记为不再可读取, 并且在上传到 GPU 后内存将被释放。 默认情况下,makeNoLongerReadable 被设置为 /false/。

应用是一项成本可能很高的操作,因此您想要在 Apply 调用之间尽可能多地 更改像素。

或者,如果您不需要访问 CPU 上的像素,您可以使用 Graphics.CopyTexture 来快速复制 GPU 端的纹理数据。注意,调用 Apply 可能会撤销之前调用 Graphics.CopyTexture 的结果。

纹理必须在导入设置中设置了 Is Readable 标志。

// Create a new texture and assign it to the renderer's material
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(); } }

另请参阅:SetPixelSetPixels 函数、Graphics.CopyTexture