ユーザ用ツール

サイト用ツール

wiki:unity:tips:080

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


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;

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

ちなみに、現時点(2014/07/18)でUnity内臓のシェーダはこれだけあります。

 "Bumped Diffuse"
 "Bumped Specular"
 "Decal"
 "Diffuse"
 "Diffuse Detail"
 "Parallax Diffuse"
 "Parallax Specular"
 "Specular"
 "VertexLit"
 
 "FX/Flare"
 
 "GUI/Text Shader"
 
 "Mobile/Bumped Diffuse"
 "Mobile/Bumped Specular"
 "Mobile/Bumped Specular (1 Directional Light)"
 "Mobile/Diffuse"
 "Mobile/Particles/Additive"
 "Mobile/Particles/Alpha Blended"
 "Mobile/Particles/Multiply"
 "Mobile/Particles/VertexLit Blended"
 "Mobile/Skybox"
 "Mobile/Unlit (Supports Lightmap
 "Mobile/VertexLit (Only Directional Lights)"
 
 "Nature/Terrain/Bumped Specular"
 "Nature/Terrain/Diffuse"
 "Nature/Tree Creator Bark"
 "Nature/Tree Creator Leaves"
 "Nature/Tree Creator Leaves Fast"
 "Nature/Tree Soft Occlusion Bark"
 "Nature/Tree Soft Occlusion Leaves"
 
 "Particles/Additive"
 "Particles/Additive (Soft)"
 "Particles/Alpha Blended"
 "Particles/Alpha Blended Premultiply"
 "Particles/Multiply"
 "Particles/Multiply (Double)"
 "Particles/VertexLit Blended"
 "Particles/~Additive-Multiply"
 
 "Reflective/Bumped Diffuse"
 "Reflective/Bumped Specular"
 "Reflective/Bumped Unlit"
 "Reflective/Bumped VertexLit"
 "Reflective/Diffuse"
 "Reflective/Parallax Diffuse"
 "Reflective/Parallax Specular"
 "Reflective/Specular"
 "Reflective/VertexLit"
 
 "RenderFX/Skybox"
 "RenderFX/Skybox Cubed"
 
 "Self-Illumin/Bumped Diffuse"
 "Self-Illumin/Bumped Specular"
 "Self-Illumin/Diffuse"
 "Self-Illumin/Parallax Diffuse"
 "Self-Illumin/Parallax Specular"
 "Self-Illumin/Specular"
 "Self-Illumin/VertexLit"
 
 "Sprites/Default"
 "Sprites/Diffuse"
 
 "Transparent/Bumped Diffuse"
 "Transparent/Bumped Specular"
 "Transparent/Cutout/Bumped Diffuse"
 "Transparent/Cutout/Bumped Specular"
 "Transparent/Cutout/Diffuse"
 "Transparent/Cutout/Soft Edge Unlit"
 "Transparent/Cutout/Specular"
 "Transparent/Cutout/VertexLit"
 "Transparent/Diffuse"
 "Transparent/Parallax Diffuse"
 "Transparent/Parallax Specular"
 "Transparent/Specular"
 "Transparent/VertexLit"
 
 "Unlit/Texture"
 "Unlit/Transparent"
 "Unlit/Transparent Cutout"
Permalink wiki/unity/tips/080.1423916360.txt.gz · 最終更新: 2015/02/14 12:19 by step

oeffentlich