MaterialPropertyBlock.AddColor 添加颜色

function AddColor (name : string, value : Color) : void
function AddColor (nameID : int, value : Color) : void

Description描述

Add a color material property.

添加一个颜色材质属性。

For performance reasons, the property block can contain only a limited number of property values. Two 4x4 matrices, six vectors/colors or 12 floats can be stored in the block. Storage space for the values is shared, so storing one matrix leaves twice less space to store vectors and floats. When block's storage is filled up, additional Add calls will be ignored.

出于性能原因,属性块只能包含属性值的有限数量。两个4x4矩形,六个向量/颜色或存12个浮点数可以存储在这个块中,存储空间的值被共享,所以存储一个矩阵留下两倍少的空间来存储向量和浮点数。当块的存储空间填满后,额外的Add调用将被忽略。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Mesh aMesh;
	public Material aMaterial = new Material(Shader.Find("VertexLit"));
	void Update() {
		MaterialPropertyBlock materialProperty = new MaterialPropertyBlock();
		materialProperty.Clear();
		materialProperty.AddColor("_Color", Color.red);
		Graphics.DrawMesh(aMesh, new Vector3(0, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
		materialProperty.Clear();
		materialProperty.AddColor("_Color", Color.green);
		Graphics.DrawMesh(aMesh, new Vector3(5, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
		materialProperty.Clear();
		materialProperty.AddColor("_Color", Color.blue);
		Graphics.DrawMesh(aMesh, new Vector3(-5, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
	}
}
// Draws 3 meshes with the same material but with different colors.
//绘制3个网格带有相同的材质,但是具有不同的颜色

var aMesh : Mesh;
var aMaterial : Material = new Material(Shader.Find("VertexLit"));

function Update() {
	var materialProperty : MaterialPropertyBlock = new MaterialPropertyBlock();

	// red mesh 红色网格
	materialProperty.Clear();
	materialProperty.AddColor("_Color",Color.red);
	Graphics.DrawMesh(aMesh, Vector3(0,0,0), Quaternion.identity,
	aMaterial, 0, null, 0, materialProperty);

	// green mesh 绿色网格
	materialProperty.Clear();
	materialProperty.AddColor("_Color",Color.green);
	Graphics.DrawMesh(aMesh, Vector3(5,0,0), Quaternion.identity,
	aMaterial, 0, null, 0, materialProperty);

	// blue mes 蓝色网格
	materialProperty.Clear();
	materialProperty.AddColor("_Color",Color.blue);
	Graphics.DrawMesh(aMesh, Vector3(-5,0,0), Quaternion.identity,
	aMaterial, 0, null, 0, materialProperty);
}

Function variant that takes nameID is faster. If you are adding properties with the same name repeatedly, use Shader.PropertyToID to get unique identifier for the name, and pass the identifier to AddColor.

使用nameID的函数变量更快,如果你重复添加相同名称的属性,使用Shader.PropertyToID得到唯一的名称标识,并传递这个标示到AddColor。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Mesh aMesh;
	public Material aMaterial = new Material(Shader.Find("VertexLit"));
	void Update() {
		MaterialPropertyBlock materialProperty = new MaterialPropertyBlock();
		int tagID = Shader.PropertyToID("_Color");
		materialProperty.Clear();
		materialProperty.AddVector(tagID, Color.red);
		Graphics.DrawMesh(aMesh, new Vector3(0, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
		materialProperty.Clear();
		materialProperty.AddVector(tagID, Color.green);
		Graphics.DrawMesh(aMesh, new Vector3(5, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
		materialProperty.Clear();
		materialProperty.AddVector(tagID, Color.blue);
		Graphics.DrawMesh(aMesh, new Vector3(-5, 0, 0), Quaternion.identity, aMaterial, 0, null, 0, materialProperty);
	}
}
// Draws 3 meshes with the same material but with different colors.
//绘制3个网格带有相同的材质,但是具有不同的颜色
// Using the material tag ID.
//使用材质的标签ID
var aMesh : Mesh;
var aMaterial : Material = new Material(Shader.Find("VertexLit"));

function Update() {
	var materialProperty : MaterialPropertyBlock = new MaterialPropertyBlock();
	var tagID : int = Shader.PropertyToID("_Color");

	// red mesh 红色网格
	materialProperty.Clear();
	materialProperty.AddVector(tagID,Color.red);
	Graphics.DrawMesh(aMesh, Vector3(0,0,0), Quaternion.identity,
	aMaterial, 0, null, 0, materialProperty);

	// green mesh 绿色网格
	materialProperty.Clear();
	materialProperty.AddVector(tagID,Color.green);
	Graphics.DrawMesh(aMesh, Vector3(5,0,0), Quaternion.identity,
	aMaterial, 0, null, 0, materialProperty);

	// blue mesh 蓝色网格
	materialProperty.Clear();
	materialProperty.AddVector(tagID, Color.blue);
	Graphics.DrawMesh(aMesh, Vector3(-5,0,0), Quaternion.identity,
	aMaterial, 0, null, 0, materialProperty);
}
Page last updated: 2011-3-1