この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
wiki:unity:tips:080 [2015/02/14 12:24] step |
— (現在) | ||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | ====== 080 スクリプトからShaderの種類を変更する ====== | ||
- | ===== 概要 ===== | ||
- | Unity におけるシェーダは、マテリアルを通して実装されています。 | ||
- | マテリアルをゲームオブジェクトにアタッチすることで見た目を変化させたりできます。 | ||
- | |||
- | {{:wiki:unity:tips:unity_shader_sel.png?200|}} | ||
- | |||
- | Unityに標準搭載のシェーダは、マテリアルの設置画面から選択できます。 | ||
- | |||
- | スクリプトからシェーダを入れ替えるには以下の様にします。 | ||
- | <code csharp> | ||
- | gameObject.renderer.material.shader = Shader.Find( "Self-Illumin/Diffuse" ); | ||
- | </code> | ||
- | この状態で再生ボタンを押すとシェーダーが変更され、見た目が変わると思います。 | ||
- | |||
- | |||
- | ==== Androidなどの端末に出力すると上手く表示されない ==== | ||
- | ~Shader.Find 関数でシェーダを取得し設定しているのだから問題ない様に見えます。 | ||
- | しかし端末に転送するとShader.Findで返ってくる値はnullです。なぜ? | ||
- | |||
- | これは、内臓シェーダが全てパッケージに含まれる訳ではないためです。Unityでは、ビルド時に必要なファイルをかき集めてパッケージを作成しますが、スクリプトで文字列指定されているファイルまでは流石に見ません。なので、"Self-Illumin/Diffuse"シェーダは使われていないものとしてパッケージが作成されたわけです。それで、これを解決するにはどうすれば良いのか?それは冒頭にも述べた様にシェーダはマテリアルを通して実装されています。従って、シェーダ用のマテリアルデータを用意してあげれば良いのです。 | ||
- | |||
- | {{:wiki:unity:tips:unity_shader_sel_2.png?200|}} | ||
- | |||
- | マテリアルを複数個作りResourcesフォルダに入れます。 | ||
- | シェーダの設定を別々にしておきます。 | ||
- | |||
- | <code csharp> | ||
- | Material diffuseBlockMaterial = Resources.Load<Material>( "diffuseBlockMaterial" ); | ||
- | Material illuminBlockMaterial = Resources.Load<Material>( "Self-IlluminBlockMaterial" ); | ||
- | </code> | ||
- | マテリアルを事前にロードしておいて... | ||
- | |||
- | <code csharp> | ||
- | gameObject.renderer.material.shader = illuminBlockMaterial.shader; | ||
- | </code> | ||
- | 必要なタイミングで入れ替えます。これできちんと反映される様になりました。 | ||
- | |||
- | ちなみに、現時点(2014/07/18)でUnity内臓のシェーダはこれだけあります。 | ||
- | <code> | ||
- | "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" | ||
- | </code> |