マテリアルの Inspector ウィンドウに表示されるパラメーターはすべて、スクリプトからアクセスすることが可能で、ランタイムにマテリアルを変化させたりアニメーション化したりすることができます。
これにより、ゲームプレイ中にマテリアルの数値を変更したり、色を変更したり、テクスチャを動的に交換したりすることができます。これを行うために最も一般的に使用されるメソッドには、以下のようなものがあります。
メソッド名 | 用途 |
---|---|
SetColor | マテリアルの色を変えます (例えば、アルベド色) |
SetFloat | 浮動小数点の値を設定します (例えば、法線マップ乗数) |
SetInteger | マテリアルに整数値を設定します |
SetTexture | マテリアルに新しいテクスチャを指定します |
スクリプトを通じてマテリアルを操作するために使う全メソッドは、Material クラス スクリプトリファレンスで参照できます。
1 つ注意が必要なのは、これらのメソッドはマテリアルの 現在のシェーダーオブジェクトに使用可能なプロパティのみを設定する という事です。これは、テクスチャを使わないシェーダーがある場合や、シェーダーがまったくバインドされていない場合、SetTextureを呼び出しても何の効果がない、という事です。これは、後でテクスチャを必要とするシェーダーを設定する場合にも当てはまります。このため、何かプロパティを設定する前に必要なシェーダーをセットすることを推奨します。ただし、シェーダーを設定した後、あるシェーダーから同じテクスチャまたはプロパティを使用する別のシェーダーに切り替えることができ、値は保持されます。
これらのメソッドは、古いシェーダーのようなすべての簡易なシェーダーや、 スタンダードシェーダー以外のビルトインシェーダー (例えば、パーティクル、スプライト、UI、unlit シェーダー) に対して、期待通りに機能します。ただし、スタンダードシェーダーを使用するマテリアルの場合は、マテリアルを完全に変更するには、その前に必要な追加の要件がいくつかあります。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.