前提・実現したいこと
UnityのScriptableObjectにて、インスペクタ上からの変更が再起動時にリセット(文字列は空文字列、intは0)されてしまいます。調べたところ、変更をEditorUtility.SetDirty()、AssetDatabase.SaveAssets()を使って保存する必要があるとあったので試しましたが保存されません。
該当のソースコード
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System; 5using UnityEngine.Events; 6#if UNITY_EDITOR 7using UnityEditor; 8#endif 9 10[CreateAssetMenu(menuName = "MyScriptable/Create WeaponData")] 11public class WeaponDataStatic : ScriptableObject 12{ 13 [HideInInspector] private string m_weaponName; 14 [HideInInspector] private WeaponTypeEnum m_type; 15 [HideInInspector] private int m_attack; 16 [HideInInspector] private int m_defense; 17 [HideInInspector] private int m_maxHealth; 18 [HideInInspector] private int m_critical; 19 [HideInInspector] private bool m_designatedSub; 20 [HideInInspector] private WeaponSkillActive m_activeSkill; 21 [HideInInspector] private WeaponSkillPassive m_passiveSkill; 22 23 //public WeaponDataStatic data = new WeaponDataStatic(); 24 25 public string weaponName { get { return m_weaponName; } } 26 public WeaponTypeEnum type { get { return m_type; } } 27 public int attack { get { return m_attack; } } 28 public int defense { get { return m_defense; } } 29 public int maxHealth { get { return m_maxHealth; } } 30 public int critical { get { return m_critical; } } 31 public bool designatedSub { get { return m_designatedSub; } } 32 public WeaponSkillActive activeSkill { get { return m_activeSkill; } } 33 public WeaponSkillPassive passiveSkill { get { return m_passiveSkill; } } 34 35#if UNITY_EDITOR 36 [CustomEditor(typeof(WeaponDataStatic))] 37 public class WeaponDataEditor : Editor 38 { 39 public override void OnInspectorGUI() 40 { 41 base.OnInspectorGUI(); 42 43 WeaponDataStatic data = target as WeaponDataStatic; 44 45 data.m_weaponName = EditorGUILayout.TextField("Name", data.weaponName); 46 data.m_type = (WeaponTypeEnum)EditorGUILayout.EnumPopup("Type", data.type); 47 data.m_attack = EditorGUILayout.IntField("Attack", data.attack); 48 data.m_defense = EditorGUILayout.IntField("Defense", data.defense); 49 data.m_maxHealth = EditorGUILayout.IntField("Max Health", data.maxHealth); 50 data.m_critical = EditorGUILayout.IntField("Critical", data.critical); 51 data.m_designatedSub = EditorGUILayout.Toggle("Designated Sub", data.designatedSub); 52 53 data.m_activeSkill = (WeaponSkillActive)EditorGUILayout.ObjectField("Active Skill", data.activeSkill, typeof(WeaponSkillActive), false); 54 if (data.activeSkill) 55 { 56 EditorGUI.indentLevel++; 57 EditorGUI.BeginDisabledGroup(true); 58 EditorGUILayout.TextField("Active Skill Name", data.activeSkill.activeSkillName); 59 EditorGUILayout.TextField("Cool Time", data.activeSkill.coolTime.ToString()); 60 EditorGUI.EndDisabledGroup(); 61 EditorGUI.indentLevel--; 62 } 63 data.m_passiveSkill = (WeaponSkillPassive)EditorGUILayout.ObjectField("Passive Skill", data.passiveSkill, typeof(WeaponSkillPassive), false); 64 if (data.passiveSkill) 65 { 66 EditorGUI.indentLevel++; 67 EditorGUI.BeginDisabledGroup(true); 68 EditorGUILayout.TextField("Passive Skill Name", data.passiveSkill.passiveSkillName); 69 EditorGUI.EndDisabledGroup(); 70 EditorGUI.indentLevel--; 71 } 72 73 EditorUtility.SetDirty(data); 74 AssetDatabase.SaveAssets(); 75 } 76 } 77#endif 78} 79
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。