Scenes and Hierarchies
In Unity, a scene is a collection of objects. After exporting one object, exporting an array of objects was no challenge. But Unity also supports object hierarchy. An array of objects would not be enough, objects need to be able to contain other objects.
With some experience in Unity, you will know that this hierarchy only really affects the transformations. After the entities are created and the components are loaded, the hierarchy only has to exist in the transform-component.
What if we wanted to create an object in run-time? In Unity, this is solved using prefabs. I felt like this was a good approach for our engine as well. Prefabs in Unity can be used in exactly the same way as any other gameobject. All I had to do was to reuse the code for exporting individual objects, and load exported prefabs on engine startup.
Assets are files like models, textures and sounds and have to be shared between the Unity project and the engine. They could theoretically share folders, but that would get messy quite fast. An asset export process was required. All world, prefab and asset changes are first added to the Unity project before being exported to the engine. The solution was simple. Loop through all Unity assets recursively and copy to the engine folder! This means that the file path and structure are always guaranteed to match. All files aren't meant to be copied, only specific ones like models, textures and sounds. By specifying file extensions, this wasn't a problem. I also added support for folders to be ignored, which became helpful in specific cases.
One problem remained: files removed from Unity would not be removed from the engine folder. Bloat adds up fast. This was solved by cleaning the engine folders before copying, using those same filters as when copying.
- Iteration speed
A level designer has to test their work, a lot. The process of going from editor to game has to be fast. I wanted to emulate the play button in Unity, a one click experience. I added an option that, in addition to exporting prefabs and the current scene, starts the latest version of the engine.
- Scene loading optimization & automatic prefab generation
After getting this far, I realized that the json scene-files were starting to become quite big, at >50k lines for a relatively small scene. I figured that the scene could be optimized by letting objects linked to a prefab reference that prefab, and only export properties that had changed. I felt like this optimization would make a great difference, but then I realized that most objects in our Unity-scenes were not linked to any prefab. This wasn't only an optimization problem, it also meant that if a property of an object was changed, it would only be applied locally, not to the other copies of that same object. I asked our level designers. It turns out that making prefabs can be a hustle, especially when there are tens of models that first has to be unpacked. Less than 20 lines of code automated that entire process.