Texture2D.Apply 应用

function Apply (updateMipmaps : bool = true) : void

Description描述

Actually apply all previous SetPixel and SetPixels changes.

实际应用前面的SetPixel和Setpixels的更改

If updateMipmaps is true, the mipmap levels are recalculated as well, using the base level as a source. Usually you want to use true in all cases except when you've modified the mip levels yourself using SetPixels .

如果updateMipmaps 结果为真,这个mipmap的级别将进行重新计算,并使用基本级别作为源,通常你会希望在所有情况下为真,除非你使用SetPixels修改了mip级别

This is a potentially expensive operation, so you'll want to change as many pixels as possible between Apply calls.

这是一个代价很高的操作,因此你要在调用 Apply前尽可能多的更改像素

The texture has to have Is Readable flag set in the import settings.

纹理必须在导入设置内设置为可读

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Start() {
		Texture2D texture = new Texture2D(128, 128);
		renderer.material.mainTexture = texture;
		int y = 0;
		while (y < texture.height) {
			int x = 0;
			while (x < texture.width) {
				Color color = x & yColor.whiteColor.gray;
				texture.SetPixel(x, y, color);
				++x;
			}
			++y;
		}
		texture.Apply();
	}
}
function Start () {
   // Create a new texture and assign it to the renderer's material
   //创建一个新纹理并且把它赋予给一个渲染材质
   var texture = new Texture2D(128, 128);
   renderer.material.mainTexture = texture;
   
   // Fill the texture with Sierpinski's fractal pattern!
   // 使用Sierpinski's 分形填充纹理
   for (var y : int = 0; y < texture.height; ++y) {
     for (var x : int = 0; x < texture.width; ++x) {
        var color = (x&y) ? Color.white : Color.gray ;
        texture.SetPixel (x, y, color);
     }
   }
   // Apply all SetPixel calls
   //应用全部的像素调用
   texture.Apply();
}

参见: SetPixel, SetPixels 函数.

最后修改:2010年12月18日 Saturday 0:50

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。