2

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?

1 Answer 1

5

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();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

You actually pointed me in a very good direction, however do you have suggestions for loading the scriptableobject asset files that I create back into a list when I open the editor window?
Well there are a lot of different options but I will update my answer with an example of loading by a custom editor script for the scriptable object itself.
Ive updated the response to include the load button pattern. There are other ways of loading this info but this is the method I prefer because it is easy to implement and allows you to directly capture the instance of scriptable object which was selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.