Create Spells through classes in custom editor?

Hi All

I’m trying to create a spellbook through a custom editor and having little success so far.

I have a class ASpell which contains the properties within each spell and a dictionary of spells to store all these. So far I’ve managed to create a very basic custom editor where I can populate the list though have no idea how to ensure these aren’t wiped when the game is launched.

I’ve looked at SerializeField, System.Serialize, ScriptableObject but I can’t manage to solve this, what I’d like to think, trivial problem.

If someone could give me some pointers for what I’d need in the SpellBook class script and SpellBookEditor class script, where I need to place Serialize or whatever else I need and how to create more than one of these (if necessary) without a new overwriting everything I’ve done to an old dictionary.

ASpell.cs

public class ASpell {
   public string _name;
   public int _level;

   public ASpell(string name, int level){
      _name = name;
      _level = level;

   }
}

SpellBook.cs

public class SpellBook : MonoBehaviour {
   public Dictionary<string, ASpell> spells = new Dictionary<string, ASpell>();

   public void Add(string name){
      ASpell spell = new ASpell(name, 1);
      spells.Add(name, spell);
   }
}

SpellBookEditor.cs

[CustomEditor(typeof(SpellBook))]
public class SpellBookEditor : Editor {
   private SpellBook sb;

   void OnEnable(){
      sb = (SpellBook)target;
   }

   public override void OnInspectorGUI(){
      foreach(ASpell spell in sb.spells.Values){
         s._name = GUILayout.TextArea(s._name);
      }

   }
}

Any help on this would be greatly appreciated!
Thanks in advance

Your problem is that you cannot serialize a Dictionary using Unity’s serialization. It’s a total pain. You will need to store the spells in a List and then convert them into a Dictionary for access performance when the game starts.

Also Unity will not serialize anything that doesn’t have a default constructor, so your spell also needs a parameterless constructor in addition to any parameter based ones.

ScriptableObject could be used if you wanted Spell assets which you could add in the editor, fiddle with the settings in the Inspector etc. ScriptableObjects will also be kept unique when serialized, ordinary classes will be duplicated if they appear in more than one reference (after deserialization).