Unity: Handling Multiple Scenes

Previous Next Print

Before you read

  • Read Avatar Chat
  • Find the AvatarChat example files in [installation folder]/game_examples/AvatarChat
  • Find the Reversi example files in [installation folder]/game_examples/Reversi

 

Overview

In this tutorial we will explain how to design your Unity game so that multiple scenes can share the same connection to ES5.

 

There are three basic approaches to handling multiple scenes:

  • Fresh connection for each scene.
  • Single script for all ES5 api calls, used by all scenes that need ES5.
  • Single script that has a reference to ElectroServer object, used by all scenes that need ES5, which each scene having their own separate script for the ES5 api calls.

 

Fresh Connection for each Scene

 

This approach is suitable for a game where most of the user's time is spent in scenes that do not use ES5 at all: no chatting, no multiplayer functionality.  When you load a scene that will need ES5, you connect and login, and when you leave that scene you drop the connection.  Even if the next scene you load will need ES5, you still drop the connection and then make a fresh connect and login, so that there's no need to pass the connection between scenes.

 

Shared NetworkContoller Script

 

This approach is suitable for a game where there's a limited number of ES5 calls or just a few different scenes.  Attach this script to a game object in your login scene, and mark that object DontDestroyOnLoad.

 

Each scene will be able to access this script.  For example, assume that the name is NetworkController, and it is attached to a game object named _GameManager.  All scenes will be able to find the script using

 

 

The NetworkController script will have to keep track of which scene is currently loaded, in order to decide what action to take when an ES5 event occurs.  If this is done by scene scripts adding delegates to the ElectroServer instance in NetworkController, those delegates will need to be removed when the scene is unloaded.

 

Shared ElectroServer Instance

 

This approach is the most flexible, and the only one that is suitable for a game where there are many different scenes that need to make ES5 calls.

 

Look at Avatar Chat's GameManager.cs script.  This is attached to a game object in the login scene, and marked DontDestroyOnLoad.  When the Main.cs script has a successful connection, before it starts to login it stores the ElectroServer instance in GameManager.

 

Now look at the NetworkController.cs script, which is attached to a different scene.  In Start(), the instance of ElectroServer is retrieved from the GameManager script.

 

Since Avatar Chat does not have another scene, it is missing some needed extra steps.  When the user is about to leave the chat scene, NetworkController needs to remove all the delegates that it added to ElectroServer.  The next scene the client loads will have a separate script with the NetworkController functionality, which will add delegates.

 

Next look at the Reversi game example.  The SharedElectroServer.cs script is attached to a game object in the login scene, and marked DontDestroyOnLoad. Login.cs logs in and stores the ElectroServer instance in SharedElectroServer.  Both LobbyController and GameController use the same instance of ElectroServer by retrieving it from SharedElectroServer.  Notice that each of the controller scripts removes all delegates added before loading the next scene, and also sends a LeaveRoomRequest since in this game we only want the player to be in a single room at a time.