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

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

新規登録して質問してみよう
ただいま回答率
85.46%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

Q&A

解決済

1回答

2870閲覧

【Unity , C#】Inspectorに表示する自作クラスの変数を動的に変えたい

KT_DON

総合スコア2

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

0グッド

0クリップ

投稿2021/03/19 11:58

前提・実現したいこと

Inspectorに表示する自作クラスの変数を動的に変えたいです。
「列挙型の値がDAMAGEなら、変数damageValueを表示する」など

発生している問題・エラーメッセージ

MonoBehaviourを継承していないクラスのEditor拡張でエラーが出る ["UnityEngine.Object"を"クラス名"に変換できません] MonoBehaviourを継承すればエラーは解消されるが, [System.Serializable]が機能しなくなる

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5 6[System.Serializable] 7public class CardEffect 8{ 9 10 11 public enum EffectKind 12 { 13 DAMAGE, 14 OTHER 15 } 16 17 [System.Serializable] 18 public class Result 19 { 20 public EffectKind effectKind; 21 22 public int damageValue; 23 24 public TargetSearch targetSearch; 25 } 26 27 [SerializeField, Header("効果")] 28 Result result; 29}

C#

1using System.Collections; 2using UnityEngine; 3using UnityEditor; 4using System.Collections.Generic; 5 6[CustomEditor(typeof(CardEffect.Result))] 7public class CardEffectEditor : Editor 8{ 9 public override void OnInspectorGUI() 10 { 11//エラー "UnityEngine.Object"を"CardEffect.Result"に変換できません 12 CardEffect.Result result = target as CardEffect.Result; 13 } 14}

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

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

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

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

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

guest

回答1

0

ベストアンサー

Editorを継承してカスタムエディターを作るというのは、コンポーネントの領域を丸ごとカスタマイズするときに使うべきものかと思います。ですが今回表示をカスタマイズしようとなさっているCardEffect.Result型はスクリプトコンポーネントではない...つまりCardEffect.Result自体をゲームオブジェクトにアタッチして使うことは想定していないものと見受けられます。
こういったケースではPropertyDrawerを継承したクラスを作るのがいいんじゃないでしょうか。

ご質問者さんのおっしゃる「列挙型の値がDAMAGEなら、変数damageValueを表示する」というのに挑戦してみたものの、あいにくインスペクターのカスタマイズはいまだに不慣れでして、いまいちエレガントでない不細工なコードになってしまいました。あくまでも一例ということで、ご質問者さんの好みに合わせた作りにしていただくのがいいかと思います。

lang

1using System; 2using UnityEditor; 3using UnityEngine; 4 5[CustomPropertyDrawer(typeof(CardEffect.Result))] 6public class CardEffectResultDrawer : PropertyDrawer 7{ 8 private bool isExpanded; 9 10 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) 11 { 12 using (new EditorGUI.PropertyScope(position, label, property)) 13 { 14 var lineStride = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; 15 position.height = EditorGUIUtility.singleLineHeight; 16 this.isExpanded = EditorGUI.BeginFoldoutHeaderGroup(position, this.isExpanded, label); 17 if (this.isExpanded) 18 { 19 using (new EditorGUI.IndentLevelScope()) 20 { 21 position.y += lineStride; 22 var effectKindProperty = property.FindPropertyRelative("effectKind"); 23 var effectKind = (CardEffect.EffectKind)EditorGUI.EnumPopup(position, GetEffectKind(effectKindProperty)); 24 SetEffectKind(effectKindProperty, effectKind); 25 switch (effectKind) 26 { 27 case CardEffect.EffectKind.DAMAGE: 28 position.y += lineStride; 29 EditorGUI.PropertyField(position, property.FindPropertyRelative("damageValue")); 30 break; 31 } 32 } 33 } 34 EditorGUI.EndFoldoutHeaderGroup(); 35 } 36 } 37 38 public override float GetPropertyHeight(SerializedProperty property, GUIContent label) 39 { 40 var lineStride = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; 41 var lineCount = 1; 42 if (this.isExpanded) 43 { 44 lineCount++; 45 switch (GetEffectKind(property.FindPropertyRelative("effectKind"))) 46 { 47 case CardEffect.EffectKind.DAMAGE: 48 lineCount++; 49 break; 50 } 51 } 52 return Mathf.Max((lineCount * lineStride) - EditorGUIUtility.standardVerticalSpacing, 0.0f); 53 } 54 55 private static CardEffect.EffectKind GetEffectKind(SerializedProperty effectKindProperty) 56 { 57 return (CardEffect.EffectKind)Enum.Parse(typeof(CardEffect.EffectKind), effectKindProperty.enumNames[effectKindProperty.enumValueIndex]); 58 } 59 60 private static void SetEffectKind(SerializedProperty effectKindProperty, CardEffect.EffectKind effectKind) 61 { 62 effectKindProperty.enumValueIndex = Array.IndexOf(effectKindProperty.enumNames, effectKind.ToString()); 63 } 64}

図

投稿2021/03/20 03:13

Bongo

総合スコア10807

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問