ランタイムにシェーダーキーワードを有効または無効にすることができます。シェーダーキーワードを有効または無効にすると、Unity はレンダリングに適切なシェーダーバリアントを使用します。
ランタイムにシェーダーバリアントを変更すると、パフォーマンスに影響を与えることがあります。キーワードの変更により、そのバリアントを初めて使用する必要がある場合、グラフィックスドライバーがシェーダープログラムを準備する間に不具合が発生する可能性があります。これは、大規模または複雑なシェーダーの場合や、グローバルなキーワードの状態変更が複数のシェーダーに影響する場合に、特に問題となります。これを避けるためには、シェーダーのロードと事前準備の方法でキーワードバリアントを考慮するようにしてください。詳細については、シェーダーのロード を参照してください。
Unity では、 ローカルシェーダーキーワード と グローバルシェーダーのキーワード があります。
シェーダーキーワードを宣言する際に、そのスコープを設定します。詳しくは、シェーダーキーワード: グローバルスコープとローカルスコープ を参照してください。
グローバルシェーダーキーワードを有効または無効にするには、Shader.EnableKeyword またはShader.DisableKeyword を使用します。
コマンドバッファでグローバルキーワードを有効または無効にするには、CommandBuffer.EnableKeyword 、または CommandBuffer.DisableKeyword を使用します。
状態を確認するには、[Shader.IsKeywordEnabled] を使用します。
グラフィックスシェーダーのローカルシェーダーキーワードを有効または無効にするには、Material.EnableKeyword または Material.DisableKeyword を使用します。状態を確認するには、Material.IsKeywordEnabled を使用します。
コンピュートシェーダーのローカルシェーダーキーワードを有効または無効にするには、ComputeShader.EnableKeyword または ComputeShader.DisableKeyword を使用します。状態を確認するには、ComputeShader.IsKeywordEnabled を使用します。
シェーダーをオーサリングする際には、キーワードをセットで宣言します。セットには、互いに排他的なキーワードが含まれます。
ランタイムでは、Unity はこれらのセットの概念を持ちません。Unity では、任意のキーワードを個別に有効または無効にすることができ、あるキーワードを有効または無効にしても、他のキーワードの状態には影響しません。つまり、同じセットから複数のキーワードを有効にしたり、セット内のすべてのキーワードを無効にしたりすることが可能です。
セット内の複数のキーワードが有効になっている場合や、セット内のキーワードが 1 つも有効になっていない場合、Unity は “十分に適合する” と考えられる (1 つの) バリアントを選択します。正確に何が起こるかについては保証されておらず、意図しない結果になる可能性があります。キーワードの状態を慎重に管理して、このような状況を避けることが最善の策です。
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.