Reputation: 3167
I have been making a custom editor window tool to read basically a list of game items as ScriptableObjects, however I am stuck now at reading and writing new objects. I would like to create new items and add them to the list (which is stored in a scriptable object) of all items, so a new entry for a sword, gun, knife, apple etc, etc... Should the editor tool I am creating make new physical ScriptableObject asset files or should this list only be of normal data object classes?
Upvotes: 2
Views: 8525
Reputation: 136
The first part of what you are looking for is that scriptable objects will not serialize changes themselves unless we either:
A) Create a new instance of the scriptable object and store it there or
B) Update the data in the scriptable object and then mark it as "dirty" and save it.
I would suggest keeping a copy active in the editor script you are writing, and then going through a save routine at some regular interval, or when the user hits an update or save button.
Because of this, a typical pattern for saving a scriptable object might look like this:
// If for some reason there is no current object you are recording changes into
// then create a new instance to save your info into
if (saveObj == null) {
saveObj = ScriptableObject.CreateInstance<YourScriptableObjClass>();
AssetDatabase.CreateAsset(saveObj, $"Assets/YourSavePath/YourObject.asset");
}
// Next Load it up with all your data, for example through some config function
saveObj.Configure(someData, otherData, moreData);
// Now flag the object as "dirty" in the editor so it will be saved
EditorUtility.SetDirty(saveObj);
// And finally, prompt the editor database to save dirty assets, committing your changes to disk.
AssetDatabase.SaveAssets();
Please note that the above is editor only functionality. You will not be able to over-write scriptable objects at runtime this way.
Edit: For loading, you have several options. You could make a custom editor for the scriptable object which provides some kind of "open window" button or you could make a field in your editor window where you drag and drop the scriptable object you want to work with. What you do on this end greatly depends on your tool and how you want the workflow of it to play out.
For the "Scriptable Object Button", I would make a custom inspector for my scriptable object that provides a button which triggers my window to open using the object from which I clicked like so:
[CustomEditor(typeof(YourScriptableObjClass))]
public class YourScriptableObjClassEditor : Editor
{
private YourScriptableObjClass targetInfo;
public void OnEnable() {
if (targetInfo == null) {
targetInfo = target as YourScriptableObjClass;
}
}
public override void OnInspectorGUI() {
// Make a button that calls a static method to open the editor window,
// passing in the scriptable object information from which the button was pressed
if (GUILayout.Button("Open Editor Window")) {
YourEditorWindow.OpenWindow(targetInfo);
}
// Remember to display the other GUI from the object if you want to see all its normal properties
base.OnInspectorGUI();
}
}
Upvotes: 5