---
description: How i went about creating our scenemanager
---

# Scene Management

## Scene Management

For our metaverse experiment we wanted to have a very streamlined way to load in other scenes that the users would explore. We wanted this to be as transition-less as possible to not break immersion. Since this is something that all players must be able to see, we needed a way to let all players know that they need to load the scene.&#x20;

To achieve this we will need some way to know when all players load the level. Most likely by receiving some kind of broadcast from the server. From there on they could be 2 possibilities. As seen before with networking it is possible to send a RPC (Remote Procedure Call) event across all of the users so that they know when to load in the scene. The idea for loading in the scene would be once a user enters a specific area this would trigger an event telling all of the other users to load in the scene.

### How can we do either option?

First of all we need to find out how we can load a scene in Unity. Unity has a Scene Management library which allows you to load in scenes. Loading a scene will give you transition, this means that the user experience some kind of loading screen in VR that will probably break immersion. Another way to go about this is taking into consideration the possibility to be able to add scenes to the current scene.

![https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html](https://3310398743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjDo00KSTVw0xTQApG4%2Fuploads%2Fn8Mr1OLaKhQoquC6iQCI%2Fimage.png?alt=media\&token=1764046f-9603-4ac2-a418-c43734cd6044)

In the unity editor we can also load in another scene into the current one also by dragging the scene you want to the hierarchy.

![Loading a scene via the editor](https://3310398743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjDo00KSTVw0xTQApG4%2Fuploads%2FnxnARoS5p7Mr5gnFiudf%2FLoad.gif?alt=media\&token=9463d586-42a4-4653-bc56-1d221151f554)

Another way which was advised by Alexander was to have all objects in the scene and enable/disable them when needed.&#x20;

This can be accomplished by disabling the scene in the editor window and re-enabling it when the scene needs to be loaded.&#x20;

It was time to test both methods and see which was the best.&#x20;

### Scene Loading

Since it was only 1 very small line of code to load in the scene additively I decided to start with that method. By placing a collider in the hallway that the user player walks through we can detect when we want to load the scenes. One thing I needed to keep in mind was that the collider needed to be a trigger, meaning that the collision box could be entered.  Another important thing to keep in mind was that we needed some kind of trigger letting us know that the level CAN be loaded. Otherwise this would be triggered on every frame the trigger is entered which could cause some problems but also for keeping track if the scene has already been loaded or not.

![Example of trigger ](https://3310398743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjDo00KSTVw0xTQApG4%2Fuploads%2FfyKn573DkJWmB3Zr8E3R%2Fimage.png?alt=media\&token=5e55cd37-39a5-4903-87dd-1a83a4997192)

![Scene loading methods used](https://3310398743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjDo00KSTVw0xTQApG4%2Fuploads%2FVCEgLTbTpzsE7c0kBPWf%2Fimage.png?alt=media\&token=5681d038-9e8a-41f7-90ef-fc3dc13ad41d)

First we check if the object that collides with the trigger has the Player tag AND that the canLoad trigger is not set to true. If so then we call the StartLoadScene method. By using the PhotonView component that is on the network manager we can call rpc events which we have made from our LoadScene method and let everyone in the room know we want to call the load scene method by using RpcRarget.All, afterwards we trigger our canLoad boolean.

It turns out that this method took quite a lot of time to load which led to massive frame drops and the user needing to wait about 5 seconds. looks like we need to try enabling the scene that is in the editor.

After adding the scene to the editor it was time to enable this instead of loading which I did by using the following bit of code in combination with the commented line from before.

![Enabling the scene instead of loading.](https://3310398743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MjDo00KSTVw0xTQApG4%2Fuploads%2FLO6IqAuWmCie2SwXtRAL%2Fimage.png?alt=media\&token=083623fe-d604-4520-94ea-1f514da7f5c9)

&#x20;The main difference here is that we use a parent GameObject that holds all the scene objects and set it to active when the user triggers the OnTriggerEnter event.&#x20;

This approach turned out to work even better than the first, however we still had a brief few seconds of frame drops when the scene was enabled. This meant that we needed to shorten the loading time perhaps by loading in parts of the scene at a time.

### Looking back

This wasn't the most efficient way off doing this as we had a big perfomance issue, but it did give us the foundations of creating the current solution which was later taken over by Tirso. This turn out to be loading in pieces of the level bit by bit by using a coroutine.&#x20;
