Warning: Declaration of action_plugin_linebreak::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /home/stepism/www/ue4/wiki/lib/plugins/linebreak/action.php on line 41

Warning: Declaration of action_plugin_markdownextra::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /home/stepism/www/ue4/wiki/lib/plugins/markdownextra/action.php on line 16

Warning: Declaration of action_plugin_syntaxhighlighter3_action::register(Doku_Event_Handler &$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/action/action.php on line 28
080 スクリプトからShaderの種類を変更する [stepism@UE4メモ]

ユーザ用ツール

サイト用ツール


wiki:unity:tips:080

以前のリビジョンの文書です —-


Warning: Declaration of syntax_plugin_linebreak::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/stepism/www/ue4/wiki/lib/plugins/linebreak/syntax.php on line 52

Warning: Declaration of syntax_plugin_linebreak::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/stepism/www/ue4/wiki/lib/plugins/linebreak/syntax.php on line 74

Warning: Declaration of syntax_plugin_markdownextra::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/stepism/www/ue4/wiki/lib/plugins/markdownextra/syntax.php on line 38

Warning: Declaration of syntax_plugin_markdownextra::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/stepism/www/ue4/wiki/lib/plugins/markdownextra/syntax.php on line 47

Warning: Declaration of syntax_plugin_syntaxhighlighter3_syntax::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/syntax/syntax.php on line 53

Warning: Declaration of syntax_plugin_syntaxhighlighter3_syntax::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/stepism/www/ue4/wiki/lib/plugins/syntaxhighlighter3/syntax/syntax.php on line 82

080 スクリプトからShaderの種類を変更する

概要

Unity におけるシェーダは、マテリアルを通して実装されています。
マテリアルをゲームオブジェクトにアタッチすることで見た目を変化させたりできます。

Unityに標準搭載のシェーダは、マテリアルの設置画面から選択できます。

スクリプトからシェーダを入れ替えるには以下の様にします。

gameObject.renderer.material.shader = Shader.Find( "Self-Illumin/Diffuse" ); 

この状態で再生ボタンを押すとシェーダーが変更され、見た目が変わると思います。

Androidなどの端末に出力すると上手く表示されない

~Shader.Find 関数でシェーダを取得し設定しているのだから問題ない様に見えます。
しかし端末に転送するとShader.Findで返ってくる値はnullです。なぜ?

これは、内臓シェーダが全てパッケージに含まれる訳ではないためです。Unityでは、ビルド時に必要なファイルをかき集めてパッケージを作成しますが、スクリプトで文字列指定されているファイルまでは流石に見ません。なので、“Self-Illumin/Diffuse”シェーダは使われていないものとしてパッケージが作成されたわけです。それで、これを解決するにはどうすれば良いのか?それは冒頭にも述べた様にシェーダはマテリアルを通して実装されています。従って、シェーダ用のマテリアルデータを用意してあげれば良いのです。

マテリアルを複数個作りResourcesフォルダに入れます。
シェーダの設定を別々にしておきます。

Material diffuseBlockMaterial = Resources.Load<Material>( "diffuseBlockMaterial" ); 
Material illuminBlockMaterial = Resources.Load<Material>( "Self-IlluminBlockMaterial" ); 

マテリアルを事前にロードしておいて…

gameObject.renderer.material.shader = illuminBlockMaterial.shader; 

必要なタイミングで入れ替えます。これできちんと反映される様になりました。

設定からパッケージに取り込むシェーダーを設定できる

エディターの設定そ行えば、シェーダーファイルをResources以下に用意する必要は無いみたいです。

[Edit > Project Settings > Graphics]を選択します。

ここにシェーダーファイルを設定しておけば、ビルド時に取り込まれます。

Unity:ビルド時にシェーダーを必ず含める方法 : Nanashi-soft

wiki/unity/tips/080.1424417828.txt.gz · 最終更新: 2015/02/20 07:37 by step