Unity - スクリプティング API: MaterialPropertyBlock.SetColor ­
言語: 日本語
  • C#
  • JS
  • Boo

スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

MaterialPropertyBlock.SetColor

public void SetColor(int nameID, Color value);

Description

color型のプロパティを設定します

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

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Mesh mesh;
    public Material material;
    private MaterialPropertyBlock block;
    void Start() {
        block = new MaterialPropertyBlock();
    }
    void Update() {
        block.SetColor("_Color", Color.red);
        Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);
        block.SetColor("_Color", Color.green);
        Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
        block.SetColor("_Color", Color.blue);
        Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
    }
}

/nameID/ でこの関数を呼び出すほうが高速になります。もし、繰り返し同じ名前のプロパティを追加する場合は、 Shader.PropertyToIDを使用してプロパティ名のユニークIDを取得し、SetColorにIDを渡します。

using UnityEngine;
using System.Collections;

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() {
        block.SetColor(colorID, Color.red);
        Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);
        block.SetColor(colorID, Color.green);
        Graphics.DrawMesh(mesh, new Vector3(5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
        block.SetColor(colorID, Color.blue);
        Graphics.DrawMesh(mesh, new Vector3(-5, 0, 0), Quaternion.identity, material, 0, null, 0, block);
    }
}