Well, first off if you’re using the same save utility in multiple scenes, you’d be best off with a centralized database for the data that needs to be saved. I’ll be responding using that premise, rather than the premise of doing this on a scene-by-scene basis, but the solution I suggest can be adapted to suit pretty much any situation.
As an example, you can have a Dictionary in a static class (or singleton on a DontDestroyOnLoad object / additive scene that’s never unloaded). This dictionary database would likely keep track of objects by ID (either string identifiers like “Box001” or random numerical IDs), or possibly a combination of scene ID and object ID if you want to make scene distinctions, and would track the data to be saved for the objects with those IDs in the VALUE column. This data would likely be in the form of a class you make for storing information, like SaveData in your posted example.
Dictionary<string, SaveData> database = new Dictionary<string, SaveData>();
In many solutions I’ve seen, the process of selecting which objects to save and which information to save on those objects is accomplished using a “Save Identifier” script/component, attached only on the objects in each scene that you want to keep track of the data for. You’d be able to set the IDs right there in the component once its attached to the object, and often be able to select some kind of dropdown (enum) value, or series of boolean values, for the kinds of data that need to be saved for it. Position and Rotation seem to be the ones you’re focused on in your setup up to now, so you’d have an enum option for “transform data”. When the object with that identifier is loaded in the scene, as part of its Awake process it would reach out to your database and register itself in a list of “objects to keep track of in the scene”.
Back on the database controller, you would likely load all of the data in the whole database all at once when the game is loaded / new game is started / program is launched- whenever is appropriate for your game. It would convert the data back into a usable form and place it into the Dictionary. If you need a solution for serializing Dictionaries to make them compatible with your JSON utility, there’s a good one that’s been active in the past few days in this forum, near the top of the topics list. This would serialize out to JSON and back perfectly fine, I think, and could store the database directly.
Even if you don’t use that though, you can just convert the database into a list of custom class objects that contain the database key as one of its fields along with the rest of the data.
[System.Serializable]
public class SaveDataContainer
{
public string id;
public SaveData saveData;
public SaveDataContainer(string id, SaveData saveData)
{
this.id = id;
this.saveData = saveData;
}
}
When a scene starts, the database controller can be made to LoadAllFromDatabase (or something), which would contact all of the objects in its “objects to keep track of” list and then tell them to update their information based on what’s been stored (if anything). When SaveAllToDatabase is called, it can call out to the same list and get updates on all of their information, storing it and overwriting existing data for that object in the database. When SaveDatabaseToFile is called, It would then convert the database data into JSON and write it out to the disk, and when LoadDatabaseFromFile is called, it would load the JSON data and convert it back into a dictionary.