I’m working on a Unity project which relies fairly heavily upon procedural content generation. To create levels, I instantiate a random selection of prefabs. Each prefab contains a number of empty GameObjects which act as spawn points for procedurally generated content. To maintain game state, each of my MonoBehaviour scripts is given it’s own ISerializable counterpart (roughly following the Momento pattern). These counterpart scripts are serialised down to a binary flat file when a user saves his/her game.
This seems to be working well for the most part, but I’m now at the point where I would like to use the serialised game state data to recreate my procedurally generated GameObjects when a game is reloaded. To do this, I store the seed value passed to Unity’s Random Number Generator (RNG) when the level was first created. I reseed the RNG with that value, and let my system recreate it’s procedural content.
The issue I’m running into (and the one I hope more experienced users can help me with) is that after my procedural GameObjects are recreated, I then need a reliable way of tying my unserialised game state classes back to their GameObject counterparts.
My current thinking is that for each procedural GameObject I create, I should set it’s name to a random integer ID that I also store in the associated ISerializable subclass. This way, when reseeding the RNG, my GameObjects are recreated with identical names. Provided this works, it would then just be a case of walking the scene hierarchy and tying together my ISerializable subclasses with their matching GameObjects.
Is this a terrible idea, or am I overlooking a more Unity-friendly way of doing this?