This document is about: PUN 2
SWITCH TO

PUN Classic (v1), PUN 2 and Bolt are in maintenance mode. PUN 2 will support Unity 2019 to 2022, but no new features will be added. Of course all your PUN & Bolt projects will continue to work and run with the known performance in the future. For any upcoming or new projects: please switch to Photon Fusion or Quantum.

3 - Game Scenes

This section covers the creation of the various scenes where players will be playing.

Each scene will be dedicated for a specific number of players, getting bigger and bigger to fit all players and give them enough space to move around.

Further down this tutorial, we'll implement the logic to load the right level based on the number of players, and for this we'll use a convention that each level will be name with the following format: "Room for X" where X will represent the number of players.

First Room Creation

  1. Create a new scene, save it and name it exactly Room for 1.
  2. Create a Cube and name it floor.
  3. Position it at 0,0,0. it's important as our logic system will spawn players above the center (0,x,0).
  4. Scale the floor to 20,1,20.

That would be sufficient here for a playable level, but some wall will keep the player within the floor area.
Simply create more cubes and position, rotate and scale them to act as walls.
Here's the position and scale of all four walls to match the floor GameObject

Room for 1: Wall 1 Transform values
Room for 1: Wall 1 Transform values
Room for 1: Wall 2 Transform values
Room for 1: Wall 2 Transform values
Room for 1: Wall 3 Transform values
Room for 1: Wall 3 Transform values
Room for 1: Wall 4 Transform values
Room for 1: Wall 4 Transform values

Don't forget the save Room For 1 Scene at this point.

Game Manager Prefab

In all cases, the minimum requirement for a User Interface is to be able to quit the room. For this, we'll need a UI Button, but we'll also need a script that will call Photon to make the local player leave the room, so let's start by creating what we'll call the Game Manager prefab, and the first task it will handle quitting the room the local Player is currently in.

  1. Create a new c# script GameManager

  2. Create an empty GameObject in the Scene, name it Game Manager

  3. Drop the GameManager script onto the GameObject Game Manager

  4. Turn Game Manager into a prefab by dragging it from the scene Hierarchy to the Assets Browser, it will turn blue in the Hierarchy.

  5. Edit GameManager Script

  6. Replace with the following

    C#

    using System;
    using System.Collections;
    
    using UnityEngine;
    using UnityEngine.SceneManagement; 
    
    using Photon.Pun;
    using Photon.Realtime;
    
    namespace Com.MyCompany.MyGame
    {
        public class GameManager : MonoBehaviourPunCallbacks 
        {
    
            #region Photon Callbacks
    
            /// <summary>
            /// Called when the local player left the room. We need to load the launcher scene.
            /// </summary>
            public override void OnLeftRoom()
            {
                SceneManager.LoadScene(0);
            }
    
            #endregion
    
            #region Public Methods
    
            public void LeaveRoom()
            {
                PhotonNetwork.LeaveRoom();
            }
    
            #endregion  
        }
    }
    
  7. Save GameManager Script

So, we've create a public method LeaveRoom(). What it does is explicitly making the local player leave the Photon Network room. We wrapped it around our own public method for abstraction.
We may want at a later stage implement more features, such as saving data, or insert a confirmation step that the user will be leaving the game, etc.

Based on our game requirements, if we are not in a room we need to show the Launcher scene, so we are going to listen to OnLeftRoom() Photon Callback and load the lobby scene Launcher, which is indexed 0 in the Build settings scene's list, that we'll setup in the Build Settings Scene List section of this page.

But why do we make a prefab out of this? Because our game requirement implies several scenes for the same game, and so we'll need to reuse this Game Manager.
In Unity the best way to reuse GameObjects is to turn them into prefabs.

Next, let's create the UI Button that will call our GameManager's LeaveRoom() method.

Quit Room Button Prefab

Again, like the Game Manager, in the perspective that we'll have many different scenes in need of this feature, it makes sense to plan ahead and make the button a prefab so that we can reuse it and modify it in one place only if we need to in the future.

  1. Make sure you are in the Scene Room for 1

  2. Create a UI Panel using the Unity menu 'GameObject/UI/Panel', name that panel Top Panel.

  3. Remove the Image and Canvas Renderer components to clear this panel.
    Keep it if you find it nicer, this is aesthetic.

  4. Set the "Anchor Presets" to top and anchor preset to stretch while holding Shift and Alt.
    RectTransform Anchors take some experience to get used to it, but it's worth it :)

    RectTransform Anchors
    RectTransform Anchors
  5. Set the RectTransform height to 50.

  6. Right click on the Panel GameObject Top Panel and add a UI/Button, name it Leave Button

  7. Select the Text Child of the Leave Button, and set it's text to Leave Game.

  8. Connect the OnClick button's event to the Game Manager instance in the hierarchy to call LeaveRoom().

Button onClick Action
Button onClick Leave Action
9. Turn `Leave button` into a prefab by dragging it from the scene Hierarchy to the Assets Browser, it will turn blue in the Hierarchy. 10. Save the scene, and save the Project

Other Rooms Creation

Now that we have one room done properly, let's duplicate it 3 times, and name them appropriately (they should already have been by Unity when you duplicated them):

Find below the changes for position, rotation and scale to speed up this repetitive process.

Room for 2

Floor Scale: 36,1,36

Room for 2: Wall 1 Transform values
Room for 2: Wall 1 Transform values
Room for 2: Wall 2 Transform values
Room for 2: Wall 2 Transform values
Room for 2: Wall 3 Transform values
Room for 2: Wall 3 Transform values
Room for 2: Wall 4 Transform values
Room for 2: Wall 4 Transform values

Room for 3

Floor Scale: 50,1,50

Room for 3: Wall 1 Transform values
Room for 3: Wall 1 Transform values
Room for 3: Wall 2 Transform values
Room for 3: Wall 2 Transform values
Room for 3: Wall 3 Transform values
Room for 3: Wall 3 Transform values
Room for 3: Wall 4 Transform values
Room for 3: Wall 4 Transform values

Room for 4

Floor Scale: 60,1,60

Room for 4: Wall 1 Transform values
Room for 4: Wall 1 Transform values
Room for 4: Wall 2 Transform values
Room for 4: Wall 2 Transform values
Room for 4: Wall 3 Transform values
Room for 4: Wall 3 Transform values
Room for 4: Wall 4 Transform values
Room for 4: Wall 4 Transform values

Build Settings Scenes List

Critical to the good operation of the Project while editing and when published, we need to add all these scenes in the Build Settings so that Unity include them when building the application.

  1. Open the Build Settings via the menu "File/Build Settings"
  2. Drag and drop all the scenes, The launcher scene must remain the first, as by default Unity will load and show to the player the first scene on that list
Build Settings: Scenes In Build
Build Settings: Scenes In Build
Now that we have your basic scenes setup, we can finally start hooking everything up. Let's do this in the next section.

Next Part.
Previous Part.

Back to top