Fish-Net: Networking Evolved
  • Introduction
    • Getting Started
    • Showcase
      • Upcoming Releases
    • Legal Restrictions
    • Pro, Projects, and Support
    • Business Support
    • Branding
  • Manual
    • General
      • Unity Compatibility
      • Changelog
        • Development Road Map
        • Major Version Update Solutions
      • Development
      • Performance
        • Benchmark Setup
        • Fish-Networking Vs Mirror
      • Terminology
        • Miscellaneous
        • Communicating
        • Server, Client, Host
      • Transports
      • Add-ons
        • Edgegap and other Hosting
        • Fish-Network-Discovery
      • Feature Comparison
      • Upgrading To Fish-Networking
    • Guides
      • Frequently Asked Questions (FAQ)
      • Creating Bug Reports
        • Report Example
      • Technical Limitations
      • Third-Party
      • Getting Started
        • Commonly Used Guides
        • Ownership - Moving Soon
        • Step-by-Step
          • Getting Connected
          • Preparing Your Player
      • Components
        • Managers
          • NetworkManager
          • TimeManager
          • PredictionManager
          • ServerManager
          • ClientManager
          • SceneManager
          • TransportManager
            • IntermediateLayer
          • StatisticsManager
          • ObserverManager
            • HashGrid
          • RollbackManager (Pro Feature)
        • Transports
          • FishyWebRTC
          • Bayou
          • FishyEOS (Epic Online Services)
          • FishyFacepunch (Steam)
          • FishyRealtime
          • FishySteamworks (Steam)
          • FishyUnityTransport
          • Multipass
          • Tugboat
          • Yak (Pro Feature)
        • Prediction
          • Network Collider
            • NetworkCollision
            • NetworkCollision2D
            • NetworkTrigger
            • NetworkTrigger2D
          • OfflineRigidbody
          • PredictedOwner
          • PredictedSpawn
        • Utilities
          • Tick Smoothers
            • NetworkTickSmoother
            • OfflineTickSmoother
          • MonoTickSmoother [Obsolete]
          • DetachableNetworkTickSmoother [Obsolete]
          • BandwidthDisplay
          • DefaultScene
          • PingDisplay
        • Authenticator
        • ColliderRollback
        • NetworkAnimator
        • NetworkBehaviour
        • NetworkTransform
        • NetworkObject
        • NetworkObserver
      • InstanceFinder
      • Ownership
        • Using Ownership To Read Values
      • Spawning and Despawning
        • Predicted Spawning
        • Nested NetworkObjects
        • Object Pooling
      • Components
      • NetworkBehaviour
      • NetworkObjects
      • Attributes, Quality of Life
      • Remote Procedure Calls
        • Broadcast
      • SyncTypes
        • Customizing Behavior
        • SyncVar
        • SyncList
        • SyncHashSet
        • SyncDictionary
        • SyncTimer
        • SyncStopwatch
        • Custom SyncType
      • Observers
        • Modifying Conditions
        • Custom Conditions
      • Automatic Serializers
      • Custom Serializers
        • Interface Serializers
        • Inheritance Serializers
      • Addressables
      • Scene Management
        • Scene Events
        • Scene Data
          • SceneLookupData
          • SceneLoadData
          • SceneUnloadData
        • Loading Scenes
        • Unloading Scenes
        • Scene Stacking
        • Scene Caching
        • Scene Visibility
        • Persisting NetworkObjects
        • Custom Scene Processors
          • Addressables
      • Transports
        • Multipass
      • Prediction
        • What Is Client-Side Prediction
        • Configuring PredictionManager
        • Configuring TimeManager
        • Configuring NetworkObject
        • Offline Rigidbodies
        • Interpolations
        • Creating Code
          • Controlling An Object
          • Non-Controlled Object
          • Understanding ReplicateState
            • Using States In Code
            • Predicting States In Code
          • Advanced Controls
        • Custom Comparers
        • PredictionRigidbody
        • Using NetworkColliders
      • Lag Compensation
        • States
        • Raycast
        • Projectiles
    • Server Hosting
      • Edgegap - Official Partner
        • Getting Started with Edgegap
      • Hathora
        • Getting Started with Hathora
      • Amazon Web Services (AWS)
        • Getting Started with AWS
    • API
Powered by GitBook
On this page
  1. Manual
  2. Guides
  3. Scene Management

Loading Scenes

PreviousSceneUnloadDataNextUnloading Scenes

Last updated 2 years ago

General

This guide will go over how to setup and load new scenes, how to load into existing scenes, how to replace scenes, and advanced info on what happens behind the "scenes" during a load.

Setup

SceneLookupData

SceneLoadData

Loading New Scenes

Scenes can be loaded globally or by a collection of client connections.

Loading new Scenes can only be done by Name, you cannot use Handle or Scene References.

Global Scenes

  • Global Scenes can be loaded by calling LoadGlobalScenes() in the SceneManager.

  • When loaded globally, scenes will be loaded for all current, and future clients.

SceneLoadData sld = new SceneLoadData("Town");
base.SceneManager.LoadGlobalScenes(sld);

Connection Scenes

Connection Scenes follow the same principle, but has a few method overloads.

  • You can load scenes for a single connection, multiple connections at once, or load scenes only on the server in preparation for connections.

  • When loading by connection only the connections specified will load the scenes.

  • You can add additional connections into a scene at any time.

SceneLoadData sld = new SceneLoadData("Main");

//Load scenes for a single connection.
NetworkConnection conn = base.Owner;
base.SceneManager.LoadConnectionScenes(conn, sld);

//Load scenes for several connections at once.
NetworkConnection[] conns = new NetworkConnection[] { connA, connB };
base.SceneManager.LoadConnectionScenes(conns, sld);

//Load scenes only on the server. This can be used to preload scenes
//that you don't want all players in.
base.SceneManager.LoadConnectionScenes(sld); 

Loading Multiple Scenes

  • Whether loading globally or by connection, you can load more than one scene in a single method call.

//Loading Multiple Connections into Multiple Scenes
string[] scenesToLoad = new string[] {"Main", "Additive"};
NetworkConnection[] conns = new NetworkConnection[] {connA, connB,connC}

SceneLoadData sld = new SceneLoadData(scenesToLoad);
base.SceneManager.LoadConnectionScenes(conns, sld);
Loading Existing Scenes

If the scene is already loaded on the server, and you want to load clients into that instance of the scene. Most likely you will want to lookup that scene by scene reference, or handle to make sure you are getting the exact scene you need.

Getting References to a Loaded Scene

Here are a few ways to get reference to the scenes that you already loaded using FishNet's SceneManager.

By Event:

// Manage your own collection of SceneRefernces/Handles
// Customize how you want to manage you scene references so its easy
// for you to find them later.
List<Scene> ScenesLoaded = new();

public void OnEnable()
{
    InstanceFinder.SceneManager.OnLoadEnd += RegisterScenes;
}

public void RegisterScenes(SceneLoadEndEventArgs args)
{
    //Only Register on Server
    if (!obj.QueueData.AsServer) return;
    
    //if you know you only loaded one scene you could just grab index [0]
    foreach(var scene in args.loadedScenes)
    {
        ScenesLoaded.Add(scene);
    }
}

public void OnDisable()
{
    InstanceFinder.SceneManager.OnLoadEnd -= RegisterScene;
}

By Connection:

//NetworkConnections have a list of Scenes they are currently in. 
int clientToLookup;
InstanceFinder.ServerManger.Clients[clientToLookup].Scenes;

By SceneManager.SceneConnnections:

// SceneManager Keeps a Dictionary of All Connection Scenes as the Key
// and the client connections that are in that scene as the value.
NetworkConnection conn;
Scene sceneNeeded;

//Get the scene you need with foreach or use Linq to filter your conditions.
foreach(var pair in SceneManager.SceneConnections)'
{
    if(pair.Value.Contains(conn))
    {
        sceneNeeded = pair.Key;
    }
}

Using Reference to Load Into Existing Instance

Use the methods above to get the reference or handle of a scene, and use that reference or handle to load a client into an existing scene.

scene sceneReference;
NetworkConnection[] conns = new(){connA,connB};

//by reference
SceneLoadData sld = new(sceneReference);
base.SceneManager.LoadConnectionScenes(conns,sld);

//by handle
SceneLoadData sld = new(sceneReference.handle);
base.SceneManager.LoadConnectionScenes(conns,sld);
Replacing Scenes

Fishnet gives the ability to replace scenes that are already loaded on the clients with the new requested scenes to load.

To Replace Scenes you will set the ReplaceScene Option in the SceneLoadData

Replaced scenes will be unloaded before the new scenes are loaded.

Replace None:

This is the default method when loading, it will ignore the replace options and load the scene in normally.

Replace All:

This will replace all scenes currently loaded in unity, even ones not managed by FishNet's SceneManager.

//Replace All Option.
SceneLoadData sld = new SceneLoadData("DungeonScene");
sld.ReplaceScenes = ReplaceOption.All;

//This will replace all Scenes loaded by FishNet or outside of FishNet like Unity,
//and load "DungeonScene"
SceneManager.LoadGlobalScenes(sld);

Replace Online Only:

This will replace only scenes managed by the SceneManager in FishNet.

//Replace Online Only Option.
SceneLoadData sld = new SceneLoadData("DungeonScene");
sld.ReplaceScenes = ReplaceOption.OnlineOnly;

//This will replace only scenes managed by the SceneManager in FishNet.
SceneManager.LoadGlobalScenes(sld);
Advanced Info

Behind the "Scenes"

Events

Loading Scenes globally or by connection will add the specified clients connection as an to the scenes if utilizing the Scene Condition, so globally will add all clients as Observers, and by connection will only add the connections specified. - See for more info.

In the Examples below, all SceneManager calls are done inside a NetworkBehaviour class, that is why you can get reference to SceneManager by doing base.SceneManager. If you would like a reference outside of a NetworkBehaviour consider using FishNets .SceneManager.

Before calling the SceneManagers Load Scene functions you will need to setup the load data to tell the how you want it to handle the scene load.

is the class used to specify what scene you want the to load. You will not create the lookup data manually and instead use the SceneLoadData constructors that will create the SceneLookupData automatically.

When loading a scene in any way, you must pass in an instance of a class into the load methods. This class provides the scene manager all of the info it needs to load the scene or scenes properly.

The constructors available for will automatically create the needed for the SceneManager to handle if you are loading a new scene, or an existing instance of one.

When loading multiple scenes in one call, the NetworkObjects you put into will be moved to the first valid scene in the list of scenes you tried to load. See Persisting NetworkObjects for more info about keeping NetworkObjects across scenes.

If you load the scene by name, it will load the connections into the first scene found with that name. If you are utilizing , then there may be multiple scenes loaded with the same name. So be alert when loading into existing scenes by name.

You can load clients into scenes that have no other clients in them if you are utilizing - the ability to keep a scene loaded with its current state on the server when all clients leave the scene.

Replacing Scenes by Default will replace scenes on both the server and clients. If you would like the server to keep the scene loaded and only replace the scene on the clients - see for more details.

The Class has very detailed XML comments on how the load process works in detail, if you need to troubleshoot the scene load process, these comments will help you understand the flow of how a scene loads.

Make sure to check out the that you can subscribe to to give better control over your game.

Observer
ObserverManagers
Scene Visibility
InstanceFinder
SceneManager
SceneLookupData
SceneManager
SceneLoadData
SceneLoadData
SceneLookupData
Scene Stacking
Scene Caching
Scene Caching
SceneManager
Scene Events
Loading Scenes Video Guide
Moved NetworkObjects