返回介绍

MaterialPropertyBlock.SetColor 设置颜色

发布于 2019-12-18 15:37:59 字数 6459 浏览 1043 评论 0 收藏 0

JavaScript => public function SetColor(name: string, value: Color): void;
JavaScript => public function SetColor(nameID: int, value: Color): void;
C# => public void SetColor(string name, Color value);
C# => public void SetColor(int nameID, Color value);

Description 描述

Set a color property.

设置颜色属性。

Adds a property to the block. If a color property with the given name already exists, the old value is replaced.

添加一个属性到块。如果是一个给定名称颜色属性已经纯在,旧值会被替换。

JavaScript:

#pragma strict
// Draws 3 meshes with the same material but with different colors.
public var mesh: Mesh;
public var material: Material;
private var block: MaterialPropertyBlock;
function Start() {
	block = new MaterialPropertyBlock();
}
function Update() {
	// red mesh
	block.SetColor("_Color", Color.red);
	Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);
	// green mesh
	block.SetColor("_Color", Color.green);
	Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
	// blue mesh
	block.SetColor("_Color", Color.blue);
	Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
}

C#:

using UnityEngine;
 
// Draws 3 meshes with the same material but with different colors.
public class ExampleClass : MonoBehaviour
{
    public Mesh mesh;
    public Material material;
    private MaterialPropertyBlock block;
 
    void Start()
    {
        block = new MaterialPropertyBlock();
    }
    void Update()
    {
        // red mesh
        block.SetColor("_Color", Color.red);
        Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);
 
        // green mesh
        block.SetColor("_Color", Color.green);
        Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
 
        // blue mesh
        block.SetColor("_Color", Color.blue);
        Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
    }
}

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

该函数变量采用nameID方式更快,如果要重复改变具有相同名称的属性,使用Shader.PropertyToID来获取该名称的唯一标识,传递标识到SetColor。

JavaScript:

#pragma strict
// Draws 3 meshes with the same material but with different colors.
public var mesh: Mesh;
public var material: Material;
private var block: MaterialPropertyBlock;
private var colorID: int;
function Start() {
	block = new MaterialPropertyBlock();
	colorID = Shader.PropertyToID("_Color");
}
function Update() {
	// red mesh
	block.SetColor(colorID, Color.red);
	Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);
	// green mesh
	block.SetColor(colorID, Color.green);
	Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
	// blue mesh
	block.SetColor(colorID, Color.blue);
	Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
}

C#:

using UnityEngine;
 
// Draws 3 meshes with the same material but with different colors.
public class ExampleClass : MonoBehaviour
{
    public Mesh mesh;
    public Material material;
    private MaterialPropertyBlock block;
    private int colorID;
 
    void Start()
    {
        block = new MaterialPropertyBlock();
        colorID = Shader.PropertyToID("_Color");
    }
 
    void Update()
    {
        // red mesh
        block.SetColor(colorID, Color.red);
        Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);
 
        // green mesh
        block.SetColor(colorID, Color.green);
        Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
 
        // blue mesh
        block.SetColor(colorID, Color.blue);
        Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
    }
}

See Also: SetFloat, SetVector, SetMatrix, SetTexture.

MaterialPropertyBlock

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文