Texture2D.SetPixels32 设置32位像素颜色列表

function SetPixels32 (colors : Color32[], miplevel : int = 0) : void


Set a block of pixel colors.


This function takes a Color32 array and changes the pixel colors of the whole mip level of the texture. Call Apply to actually upload the changed pixels to the graphics card.

此函数采用一个Color32数组,并改变纹理整个mip level的像素颜色。调用Apply来实际上传改变的像素到显卡。

The colors array is a flattened 2D array, where pixels are laid out left to right, bottom to top (i.e. row after row). Array size must be at least width by height of the mip level used. The default mip level is zero (the base texture) in which case the size is just the size of the texture. In general case, mip level size is mipWidth=max(1,width>>miplevel) and similarly for height.

返回的数字是二维数组,像素布局从左到右,从底到顶(即,一行行),数组的大小必须是使用mip level的宽乘高。默认的mip level为0(基本纹理),这时候数组的大小是纹理的大小。在一般情况下,mip level大小是mipWidth=max(1,width>>miplevel) ,高度也同样。

This function works only on ARGB32 texture formats. For other formats SetPixels32 is ignored. The texture also has to have Is Readable flag set in the import settings.

这个函数仅适用于ARGB32纹理格式。对于其他格式SetPixels32将忽略。该纹理在导入设置,也有Is Readable可读标识设置。

Using SetPixels32 is faster than calling SetPixels.


参见:SetPixels, GetPixels32, GetPixels, Apply, mipmapCount.

// This script will tint texture's mip levels in different colors

// (1st level red, 2nd green, 3rd blue). You can use it to see

// which mip levels are actually used and how.

//此脚本将着色纹理的mip levels不同的颜色(第一级红色,第二级绿色,第三级蓝色)

//可以用它来看看哪个mip levels 实际使用水平以及如何使用。

function Start () {

	// duplicate the original texture and assign to the material


	var texture : Texture2D = Instantiate(renderer.material.mainTexture);

	renderer.material.mainTexture = texture;

	// colors used to tint the first 3 mip levels

	//颜色用来着色前3个 mip levels

	var colors = new Color32[3];

	colors[0] = Color.red;

	colors[1] = Color.green;

	colors[2] = Color.blue;

	var mipCount = Mathf.Min( 3, texture.mipmapCount );

	// tint each mip level

	//着色每个 mip levels

	for( var mip = 0; mip < mipCount; ++mip ) {

		var cols = texture.GetPixels32( mip );

		for( var i = 0; i < cols.Length; ++i ) {

			cols[i] = Color32.Lerp( cols[i], colors[mip], 0.33 );


		texture.SetPixels32( cols, mip );


	// actually apply all SetPixels32, don't recalculate mip levels

	//实际应用所有SetPixels32,不重新计算mip levels

	texture.Apply( false );

Page last updated: 2011-9-18