前提
Enumで選択したタイプで継承先のクラスを切り替えようとしています。
オブジェクトにHogeをアタッチし、HogeTypeを変えることでinspectorに表示される変数は変えることはできました。
実行前にHogeType.AでhogeA1に数値を設定しましたが、実行するとcustomHogeに新規でCustomHogeAが割り振られPlay前の値がリセットされてしまいます。
実現したいこと
実行前に設定した値がリセットされないようにしたい
該当のソースコード
using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif public class Hoge : MonoBehaviour { public enum HogeType { A, B } public HogeType hogeType; public CustomHoge customHoge; } public class CustomHoge { } public class CustomHogeA : CustomHoge { public int hogeA1; public bool hogeA2; } public class CustomHogeB : CustomHoge { public float hogeB1; public bool hogeB2; } #if UNITY_EDITOR [CustomEditor(typeof(Hoge))] public class ItemObjectInspector : Editor { public override void OnInspectorGUI() { DrawDefaultInspector(); serializedObject.Update(); Hoge myTarget = (Hoge)target; if (myTarget.customHoge != null) { if (myTarget.hogeType == Hoge.HogeType.A) { if (!(myTarget.customHoge is CustomHogeA)) { Debug.Log("New CustomHogeA"); myTarget.customHoge = new CustomHogeA(); } if (myTarget.customHoge is CustomHogeA) { EditorGUILayout.LabelField("CustomHogeA"); CustomHogeA hoge = (CustomHogeA)myTarget.customHoge; hoge.hogeA1 = EditorGUILayout.IntField("A1", hoge.hogeA1); } } else if (myTarget.hogeType == Hoge.HogeType.B) { if (!(myTarget.customHoge is CustomHogeB)) { Debug.Log("New CustomHogeB"); myTarget.customHoge = new CustomHogeB(); } if (myTarget.customHoge is CustomHogeB) { EditorGUILayout.LabelField("CustomHogeB"); CustomHogeB hoge = (CustomHogeB)myTarget.customHoge; hoge.hogeB1 = EditorGUILayout.FloatField("B1", hoge.hogeB1); } } } else { EditorGUILayout.LabelField("CustomHogeがない"); myTarget.customHoge = new CustomHoge(); } serializedObject.ApplyModifiedProperties(); } } #endif
試したこと
serializedObject.Update();
serializedObject.ApplyModifiedProperties();
補足情報(FW/ツールのバージョンなど)
Unity 2020.3.10f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/12/23 01:52
2022/12/23 03:30
2022/12/24 00:44