10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Unity]Scriptで複製したオブジェクトのマテリアルを別々に変更する

Posted at

同じマテリアルが当たっているオブジェクトを複製して色を変えた時に色がシンクロしてしまうのが嫌だったので、マテリアルをスクリプトで複製して当てなおす処理で回避しました。

#コード

//===色変更処理===
//targetA : コピー元
//targetB : コピーしたオブジェクト
//setColor : 設定したい色
	//変更したい色
	Color setColor = new Color(0f, 0f, 0f);

	//Aをコピー
	GameObject targetB = Instantiate(targetA);

	//対象のシェーダー情報を取得
        Shader sh = targetObjA.GetComponent<MeshRenderer>().material.shader;

	//取得したシェーダーを元に新しいマテリアルを作成
        Material mat = new Material(sh);
	
	//作成したマテリアルの色を変更
        mat.color = setColor;
	
	//対象オブジェクトに割り当てる
        targetObjB.GetComponent<MeshRenderer>().material = mat;
10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?