Regarding ScriptableObjects, Inheritance and custom editors
I've got a custom editor used to create ScriptableObjects of type Item.
It works, but now I want to change it so that I can create objects inherited from Item (Potion, Key, etc).
Right now what I do is something along the lines of -
Item itemData;
...
itemData = (Item)ScriptableObject.CreateInstance(typeof(Item));
...
// Get input and assign itemData.ID, .Name, .Type, .isStackable etc accordingly
...
// when a "Create" button is clicked, create the .asset with this data
string dataPath = "Assets/Data/Items/" + itemData.itemName + ".asset";
AssetDatabase.CreateAsset(itemData, dataPath);
EditorUtility.SetDirty(itemData);
AssetDatabase.Refresh();
This works well. Now what I want to do is first get the type of item to be created (Potion, Key, etc) and tune the editor based on that.
what I was wondering is if it's a good idea to use a struct
that holds every kind of field any Item might have, and change its fields from the input. Then when creating the asset, using some Init() function on the asset to set the relevant values? Something like
AssetDatabase.CreateAsset(Object, dataPath); // `Object` would be switched based on the type
Object.Init(myStruct); // Set the relevant fields on the item.
Is this a smart approach? Would this even work?
Is there a better and more logical way to do it?
Cheers.
Your answer
Follow this Question
Related Questions
Serialize child ScriptableObject asset values in parent ScriptableObject asset. 0 Answers
Custom Editor Window Not Saving Scriptable Object 1 Answer
Inheritence problems with raycast 1 Answer
Multiple custom editors for same type (or should I re-implement as UI?) 0 Answers
Popup to choose which child class I want in Custom Editor Inspector 0 Answers