Room system at runtime

Hello,

I want to implement a ‘room’ system inside the scene.

How it work : a scene may be splitted into rooms, and only one room can be loaded a time.
In the editor, I have a root GameObject that contains the script that load the desired room as a Prefab.
So far, Prefabs are one of the easiest and convinient way to load/unload things at runtime from my research. I want to avoid having too much GameObject loaded, and SetActive(false) on everything I want to ‘not load’ still gets run by Unity regarding RAM usage(but no Update() ).
I also have some sort of door transition to change the current loaded room.
So this is my setup right now, it works perfectly.

Problem : Since my script accepts a prefab depedency (I should drag&drop each room prefab into the list of GameObject of my root GameObject), I CAN’T have a global scene preview in the editor with all the rooms and it really hurts to edit my room independently without seeing how it goes in the scene. Like if I want to have options that are related to the scene (let’s say a custom SkyBox) and that should be apparent in the rooms, then I actually CAN’T because I can only edit the Prefab independently.

Any tips ? :slight_smile:

First question… is the RAM usage actually a performance concern? This feels like it could be speculative optimisation. There needs to be an actual problem you’re trying to solve before you try and implement a system like this.

1 Like

Hello !

Yeah, kind of the be honest.

If I have a scene splitted into 6 rooms, and each rooms has 1000 GameObjects in it, for a total of 6000 GameObjects, I find the idea of having 5000 GameObjects in the ram for absolutly no reason really overkill, specially if the player never goes in these rooms and never loads these GameObject.
The same example can apply to all meshes, loading everything at once would be way to heavy for what I have.

The main problem I’m trying to solve is to have one scene, that have settings, and these settings apllies to every room in the scene, and get the maximum amount of resource for one and only one room.
Another problem I’m trying to solve is mostly that each Scene in my game has an Id, which I use to set flag.
Every scene in my game can have flags, triggered by GameObjects, this is the way I handle some sort of saved actions on each scene. Having scene replacing room completely change my system and its pros.

I’m aiming for an optimized game, like I want it to be playable on console like Switch for example, I really need to be careful about these kind of things.

Also, lets say I use the SetActive(false) on every GameObject that shouldnt be loaded : what if a GameObject trigger something in its Start() method, or Awake() method (these does run even if not active right?), like a Cutscene or something, then it’s a problem.

I mean, isn’t there a simple solution to this? This kind of pattern occurs pretty often isnt it?

I target PC and we have scenes with tens of thousands of gameobjects and most have static collision. As long as oclussion culling works so that you dont overdraw they have minimal impact.

But at some point you need to start looking at streaming. But thats alot higher than 6k GO.

Edit: on mobile VRAM can be a problem with too many loaded textures, more so if you use baked lighting. But you need to profile.

You need to actually profile this rather than just go on feeling alone. Humans are terrible at measuring this. Computers are precise. Use the profiler/memory profiler to see if you’re actually hitting a memory usage outside your allowance.

In any case you can’t do this with prefabs on their own. You would either need to use additive scene loading, or systems like addressables. But don’t waste time you don’t need to. Profile first.

1 Like

Thanks for your replies!

I’ll go with my root GameObject settings everything to SetActive(false) for the thing I don’t need to be here, and then profiling to see the difference. I didn’t notice but the SetActive(false) pattern is some sort of pulling pattern, which is neat for room transition (avoid spikes of resource usage and framedrops).

Oclussion culling is being setup, this is part of the plan. :slight_smile:

If the profiling doesnt look bad, I’ll keep this solution. My RAM limit is 4Gb, right now I should be good for what I have.

You could make an editor script that “fast forwards” through your world to the room of interest. You’ll then be able to edit your room prefab. I believe it’s possible to also modify and save a prefab while the game is paused and so you could possibly do the ‘fast forward’ while playing the game.

If you want convenience you can always make some tooling, like preview scene with all the rooms loaded in editor, then you can bake their layout into a ScriptableObject and use streaming in another real game scene.
Regarding skybox, you can change it in runtime and make individual one per room. And again, tool for previewing it in preview scene.

1 Like