質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

797閲覧

UnityでShaderのpropertyにcolorと言う名前があるかどうかをC#スクリプトのif文を使ってやりたい

hosituka

総合スコア15

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/08/03 00:56

編集2021/08/03 05:33

Unityで敵がfadeoutして消える仕組みを作るために最初にShaderのpropertyにcolorと言う名前があるかどうかをC#スクリプトのif文を使ってやりたいのですが下記のように試しにやってみたのですがダメでした何かやり方があった場合教えていただけるとありがたいです。

C#

1private IEnumerator teki_fadeout() 2{ 3 for (float a = 1; a == 0; a-=0.01f) 4 { 5 if( gameObject.GetComponent<Renderer>().material.color == null) 6 { 7 //colorがなかった場合の処理 8 } 9 else 10 { 11 GetComponent<Renderer>().material.color -= new Color(0,0,0,0.01f); 12 13 14 } 15 yield return new WaitForSeconds(0.05f); 16 } 17 18 Destroy(gameObject); 19}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

最初にShaderのpropertyにcolorと言う名前があるかどうかをC#スクリプトのif文を使ってやりたいのですが

Material.colorでget/setするShaderのpropertyでしょうか。
マニュアルには、以下のように書かれています。

By default, Unity considers a color with the property name name "_Color" to be the main color. Use the [MainColor] ShaderLab Properties attribute to make Unity consider a color with a different property name to be the main color.

上記の情報を元に、テストプログラムを書いてみました。

csharp

1using UnityEngine; 2using UnityEditor; 3 4/* 5 選択したマテリアルのMainColorを探すウィンドウ 6 エディタのメニューより「Test」→「Test02」で表示 7 */ 8public class Test02 : EditorWindow { 9 [MenuItem("Test/Test02")] 10 static void Open() 11 { 12 EditorWindow.GetWindow<Test02>(); 13 } 14 15 public Material material = null; 16 public string message = ""; 17 18 void OnGUI() 19 { 20 Material material = EditorGUILayout.ObjectField("Material", this.material, typeof(Material), true) as Material; 21 if (material == null) { 22 this.material = null; 23 this.message = ""; 24 } 25 else if (this.material == null || material.GetInstanceID() != this.material.GetInstanceID()) { 26 this.material = material; 27 Shader shader = this.material.shader; 28 // 最初に、デフォルトの名前を探しておく。無ければ、-1が返る。 29 int mainColorIndex = shader.FindPropertyIndex("_Color"); 30 int numProperties = shader.GetPropertyCount(); 31 // プロパティの中で、MainColorフラグを持つものを探す。 32 for (int i = 0; i < numProperties; ++i) { 33 var flags = shader.GetPropertyFlags(i); 34 if ((flags & UnityEngine.Rendering.ShaderPropertyFlags.MainColor) != 0) { 35 mainColorIndex = i; 36 break; 37 } 38 } 39 // 見つかった。 40 if (mainColorIndex >= 0) { 41 this.message = string.Format("MainColor is {0}.", shader.GetPropertyName(mainColorIndex)); 42 } 43 // 見つからなかった。 44 else { 45 this.message = "no main colors."; 46 } 47 } 48 EditorGUILayout.LabelField(this.message); 49 } 50} 51

投稿2021/08/03 13:19

編集2021/08/03 13:40
katsuko

総合スコア3471

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問