Scene/Memory Management Question

I have a general question about managing memory footprint and scenes.

Our client is requesting a product that could potentially have many different individual “modules”. Each modules consisting of different 2D and 3D assets. The modules are navigated to through a main menu which then loads up the selected module.

Coming from Unity, I would generally make each module its own scene and then just load them in as needed as opposed to creating everything in one huge scene.

Is this how I should handle things in Playcanvas also? Will loading up each module in its own scene help keep memory down and performance up?

I have noticed that assets have a “preload” check mark so does that mean everything is loaded at the beginning even if it is not in the opening scene? Does splitting things in to different scenes not really matter then?

Thanks,
Justin

Hi @justinISO,

So the concept of scenes in Playcanvas currently doesn’t work the same way as in Unity. A scene is basically an entity hierarchy bundled with a set of scene settings (the ones you find in your project settings).

The full list of your project assets are available for all of your project scenes. Any asset in Playcanvas can be loaded in the following ways:

  1. Before your app launches, if the preload flag is set to true.
  2. When it’s first requested by any entity component, if preload is set to false. The engine will automatically fetch and load it in place.
  3. Manually in code, by explicitly calling the relevant method of the pc.AssetRegistry:
this.app.assets.load(myAsset);

Any asset loaded in memory will stay in memory, unless you unload it in your code using the following method:

myAsset.unload();

In Playcanvas loading a new scene (entity hierarchy) doesn’t mean unloading from memory any assets used by the previous scene. Actually loading a scene is an additive step, you can have multiple scenes loaded at the same time.

That means any memory management required has to happen manually in your code:

  1. When loading a scene the required assets get loaded either automatically by the Playcanvas engine or manually in your code.

  2. When an asset is no longer needed then you should manually in code unload it from memory.

What is import here to understand is from the moment you manually unload an asset from memory if at any point it’s again required by an entity, you will have to make sure to manually load it again. You can no longer except it to be loaded by the engine automatically.

Hope this makes sense!

2 Likes

Ok yes that makes sense! Thank you so much for breaking it down for me like that.

So as a follow up, I assume loading everything in to one scene is not ideal either as I assume there are some memory limits that exist? Can you perhaps point me towards some documentation or best practices for managing a larger Playcanvas project?

Thanks again!

I’d say that splitting your project in different scenes or holding everything on a single scene usually is more of a content creation decision not so much a loading question. Since each scene is separate entity hierarchy, using a single scene you could have the different parts of your scene as a top level disabled child entity in your hierarchy. Many Playcanvas devs do this, each a common practice.

When the time comes you toggle that entity on/off, that isn’t much different than having different scenes loading wise. I’d say having different scenes helps in loading only if you have a very big number of entities (4 digit numbers) in your project, that it may take some time to load and add a performance overhead to the entity graph in the engine.

To make the answer complete, having different scenes allows you to have different project settings (e.g. skybox, lighting settings etc.) per scene which isn’t so trivial to set when using a single scene.