This repository has been archived on 2025-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs
Unity Technologies 22d877d1b2 com.unity.netcode.gameobjects@1.0.0-pre.2
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).

## [1.0.0-pre.2] - 2020-12-20

### Added

- Associated Known Issues for the 1.0.0-pre.1 release in the changelog

### Changed

- Updated label for `1.0.0-pre.1` changelog section

## [1.0.0-pre.1] - 2020-12-20

### Added

- Added `ClientNetworkTransform` sample to the SDK package (#1168)
- Added `Bootstrap` sample to the SDK package (#1140)
- Enhanced `NetworkSceneManager` implementation with additive scene loading capabilities (#1080, #955, #913)
  - `NetworkSceneManager.OnSceneEvent` provides improved scene event notificaitons
- Enhanced `NetworkTransform` implementation with per axis/component based and threshold based state replication (#1042, #1055, #1061, #1084, #1101)
- Added a jitter-resistent `BufferedLinearInterpolator<T>` for `NetworkTransform` (#1060)
- Implemented `NetworkPrefabHandler` that provides support for object pooling and `NetworkPrefab` overrides (#1073, #1004, #977, #905,#749, #727)
- Implemented auto `NetworkObject` transform parent synchronization at runtime over the network (#855)
- Adopted Unity C# Coding Standards in the codebase with `.editorconfig` ruleset (#666, #670)
- When a client tries to spawn a `NetworkObject` an exception is thrown to indicate unsupported behavior. (#981)
- Added a `NetworkTime` and `NetworkTickSystem` which allows for improved control over time and ticks. (#845)
- Added a `OnNetworkDespawn` function to `NetworkObject` which gets called when a `NetworkObject` gets despawned and can be overriden. (#865)
- Added `SnapshotSystem` that would allow variables and spawn/despawn messages to be sent in blocks (#805, #852, #862, #963, #1012, #1013, #1021, #1040, #1062, #1064, #1083, #1091, #1111, #1129, #1166, #1192)
  - Disabled by default for now, except spawn/despawn messages
  - Will leverage unreliable messages with eventual consistency
- `NetworkBehaviour` and `NetworkObject`'s `NetworkManager` instances can now be overriden (#762)
- Added metrics reporting for the new network profiler if the Multiplayer Tools package is present (#1104, #1089, #1096, #1086, #1072, #1058, #960, #897, #891, #878)
- `NetworkBehaviour.IsSpawned` a quick (and stable) way to determine if the associated NetworkObject is spawned (#1190)
- Added `NetworkRigidbody` and `NetworkRigidbody2D` components to support networking `Rigidbody` and `Rigidbody2D` components (#1202, #1175)
- Added `NetworkObjectReference` and `NetworkBehaviourReference` structs which allow to sending `NetworkObject/Behaviours` over RPCs/`NetworkVariable`s (#1173)
- Added `NetworkAnimator` component to support networking `Animator` component (#1281, #872)

### Changed

- Bumped minimum Unity version, renamed package as "Unity Netcode for GameObjects", replaced `MLAPI` namespace and its variants with `Unity.Netcode` namespace and per asm-def variants (#1007, #1009, #1015, #1017, #1019, #1025, #1026, #1065)
  - Minimum Unity version:
    - 2019.4 → 2020.3+
  - Package rename:
    - Display name: `MLAPI Networking Library` → `Netcode for GameObjects`
    - Name: `com.unity.multiplayer.mlapi` → `com.unity.netcode.gameobjects`
    - Updated package description
  - All `MLAPI.x` namespaces are replaced with `Unity.Netcode`
    - `MLAPI.Messaging` → `Unity.Netcode`
    - `MLAPI.Connection` → `Unity.Netcode`
    - `MLAPI.Logging` → `Unity.Netcode`
    - `MLAPI.SceneManagement` → `Unity.Netcode`
    - and other `MLAPI.x` variants to `Unity.Netcode`
  - All assembly definitions are renamed with `Unity.Netcode.x` variants
    - `Unity.Multiplayer.MLAPI.Runtime` → `Unity.Netcode.Runtime`
    - `Unity.Multiplayer.MLAPI.Editor` → `Unity.Netcode.Editor`
    - and other `Unity.Multiplayer.MLAPI.x` variants to `Unity.Netcode.x` variants
- Renamed `Prototyping` namespace and assembly definition to `Components` (#1145)
- Changed `NetworkObject.Despawn(bool destroy)` API to default to `destroy = true` for better usability (#1217)
- Scene registration in `NetworkManager` is now replaced by Build Setttings → Scenes in Build List (#1080)
- `NetworkSceneManager.SwitchScene` has been replaced by `NetworkSceneManager.LoadScene` (#955)
- `NetworkManager, NetworkConfig, and NetworkSceneManager` scene registration replaced with scenes in build list (#1080)
- `GlobalObjectIdHash` replaced `PrefabHash` and `PrefabHashGenerator` for stability and consistency (#698)
- `NetworkStart` has been renamed to `OnNetworkSpawn`. (#865)
- Network variable cleanup - eliminated shared mode, variables are server-authoritative (#1059, #1074)
- `NetworkManager` and other systems are no longer singletons/statics (#696, #705, #706, #737, #738, #739, #746, #747, #763, #765, #766, #783, #784, #785, #786, #787, #788)
- Changed `INetworkSerializable.NetworkSerialize` method signature to use `BufferSerializer<T>` instead of `NetworkSerializer` (#1187)
- Changed `CustomMessagingManager`'s methods to use `FastBufferWriter` and `FastBufferReader` instead of `Stream` (#1187)
- Reduced internal runtime allocations by removing LINQ calls and replacing managed lists/arrays with native collections (#1196)

### Removed

- Removed `NetworkNavMeshAgent` (#1150)
- Removed `NetworkDictionary`, `NetworkSet` (#1149)
- Removed `NetworkVariableSettings` (#1097)
- Removed predefined `NetworkVariable<T>` types (#1093)
    - Removed `NetworkVariableBool`, `NetworkVariableByte`, `NetworkVariableSByte`, `NetworkVariableUShort`, `NetworkVariableShort`, `NetworkVariableUInt`, `NetworkVariableInt`, `NetworkVariableULong`, `NetworkVariableLong`, `NetworkVariableFloat`, `NetworkVariableDouble`, `NetworkVariableVector2`, `NetworkVariableVector3`, `NetworkVariableVector4`, `NetworkVariableColor`, `NetworkVariableColor32`, `NetworkVariableRay`, `NetworkVariableQuaternion`
- Removed `NetworkChannel` and `MultiplexTransportAdapter` (#1133)
- Removed ILPP backend for 2019.4, minimum required version is 2020.3+ (#895)
- `NetworkManager.NetworkConfig` had the following properties removed: (#1080)
  - Scene Registrations no longer exists
  - Allow Runtime Scene Changes was no longer needed and was removed
- Removed the NetworkObject.Spawn payload parameter (#1005)
- Removed `ProfilerCounter`, the original MLAPI network profiler, and the built-in network profiler module (2020.3). A replacement can now be found in the Multiplayer Tools package. (#1048)
- Removed UNet RelayTransport and related relay functionality in UNetTransport (#1081)
- Removed `UpdateStage` parameter from `ServerRpcSendParams` and `ClientRpcSendParams` (#1187)
- Removed `NetworkBuffer`, `NetworkWriter`, `NetworkReader`, `NetworkSerializer`, `PooledNetworkBuffer`, `PooledNetworkWriter`, and `PooledNetworkReader` (#1187)
- Removed `EnableNetworkVariable` in `NetworkConfig`, it is always enabled now (#1179)
- Removed `NetworkTransform`'s FixedSendsPerSecond, AssumeSyncedSends, InterpolateServer, ExtrapolatePosition, MaxSendsToExtrapolate, Channel, EnableNonProvokedResendChecks, DistanceSendrate (#1060) (#826) (#1042, #1055, #1061, #1084, #1101)
- Removed `NetworkManager`'s `StopServer()`, `StopClient()` and `StopHost()` methods and replaced with single `NetworkManager.Shutdown()` method for all (#1108)

### Fixed

- Fixed ServerRpc ownership check to `Debug.LogError` instead of `Debug.LogWarning` (#1126)
- Fixed `NetworkObject.OwnerClientId` property changing before `NetworkBehaviour.OnGainedOwnership()` callback (#1092)
- Fixed `NetworkBehaviourILPP` to iterate over all types in an assembly (#803)
- Fixed cross-asmdef RPC ILPP by importing types into external assemblies (#678)
- Fixed `NetworkManager` shutdown when quitting the application or switching scenes (#1011)
  - Now `NetworkManager` shutdowns correctly and despawns existing `NetworkObject`s
- Fixed Only one `PlayerPrefab` can be selected on `NetworkManager` inspector UI in the editor (#676)
- Fixed connection approval not being triggered for host (#675)
- Fixed various situations where messages could be processed in an invalid order, resulting in errors (#948, #1187, #1218)
- Fixed `NetworkVariable`s being default-initialized on the client instead of being initialized with the desired value (#1266)
- Improved runtime performance and reduced GC pressure (#1187)
- Fixed #915 - clients are receiving data from objects not visible to them (#1099)
- Fixed `NetworkTransform`'s "late join" issues, `NetworkTransform` now uses `NetworkVariable`s instead of RPCs (#826)
- Throw an exception for silent failure when a client tries to get another player's `PlayerObject`, it is now only allowed on the server-side (#844)

### Known Issues

- `NetworkVariable` does not serialize `INetworkSerializable` types through their `NetworkSerialize` implementation
- `NetworkObjects` marked as `DontDestroyOnLoad` are disabled during some network scene transitions
- `NetworkTransform` interpolates from the origin when switching Local Space synchronization
- Exceptions thrown in `OnNetworkSpawn` user code for an object will prevent the callback in other objects
- Cannot send an array of `INetworkSerializable` in RPCs
- ILPP generation fails with special characters in project path

## [0.2.0] - 2021-06-03

WIP version increment to pass package validation checks. Changelog & final version number TBD.

## [0.1.1] - 2021-06-01

This is hotfix v0.1.1 for the initial experimental Unity MLAPI Package.

### Changed

- Fixed issue with the Unity Registry package version missing some fixes from the v0.1.0 release.

## [0.1.0] - 2021-03-23

This is the initial experimental Unity MLAPI Package, v0.1.0.

### Added

- Refactored a new standard for Remote Procedure Call (RPC) in MLAPI which provides increased performance, significantly reduced boilerplate code, and extensibility for future-proofed code. MLAPI RPC includes `ServerRpc` and `ClientRpc` to execute logic on the server and client-side. This provides a single performant unified RPC solution, replacing MLAPI Convenience and Performance RPC (see [here](#removed-features)).
- Added standarized serialization types, including built-in and custom serialization flows. See [RFC #2](https://github.com/Unity-Technologies/com.unity.multiplayer.rfcs/blob/master/text/0002-serializable-types.md) for details.
- `INetworkSerializable` interface replaces `IBitWritable`.
- Added `NetworkSerializer`..., which is the main aggregator that implements serialization code for built-in supported types and holds `NetworkReader` and `NetworkWriter` instances internally.
- Added a Network Update Loop infrastructure that aids Netcode systems to update (such as RPC queue and transport) outside of the standard `MonoBehaviour` event cycle. See [RFC #8](https://github.com/Unity-Technologies/com.unity.multiplayer.rfcs/blob/master/text/0008-network-update-loop.md) and the following details:
  - It uses Unity's [low-level Player Loop API](https://docs.unity3d.com/ScriptReference/LowLevel.PlayerLoop.html) and allows for registering `INetworkUpdateSystem`s with `NetworkUpdate` methods to be executed at specific `NetworkUpdateStage`s, which may also be before or after `MonoBehaviour`-driven game logic execution.
  - You will typically interact with `NetworkUpdateLoop` for registration and `INetworkUpdateSystem` for implementation.
  - `NetworkVariable`s are now tick-based using the `NetworkTickSystem`, tracking time through network interactions and syncs.
- Added message batching to handle consecutive RPC requests sent to the same client. `RpcBatcher` sends batches based on requests from the `RpcQueueProcessing`, by batch size threshold or immediately.
- [GitHub 494](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/494): Added a constraint to allow one `NetworkObject` per `GameObject`, set through the `DisallowMultipleComponent` attribute.
- Integrated MLAPI with the Unity Profiler for versions 2020.2 and later:
  - Added new profiler modules for MLAPI that report important network data.
  - Attached the profiler to a remote player to view network data over the wire.
- A test project is available for building and experimenting with MLAPI features. This project is available in the MLAPI GitHub [testproject folder](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/tree/release/0.1.0/testproject).
- Added a [MLAPI Community Contributions](https://github.com/Unity-Technologies/mlapi-community-contributions/tree/master/com.mlapi.contrib.extensions) new GitHub repository to accept extensions from the MLAPI community. Current extensions include moved MLAPI features for lag compensation (useful for Server Authoritative actions) and `TrackedObject`.

### Changed

- [GitHub 520](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/520): MLAPI now uses the Unity Package Manager for installation management.
- Added functionality and usability to `NetworkVariable`, previously called `NetworkVar`. Updates enhance options and fully replace the need for `SyncedVar`s.
- [GitHub 507](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/507): Reimplemented `NetworkAnimator`, which synchronizes animation states for networked objects.
- GitHub [444](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/444) and [455](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/455): Channels are now represented as bytes instead of strings.

For users of previous versions of MLAPI, this release renames APIs due to refactoring. All obsolete marked APIs have been removed as per [GitHub 513](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/513) and [GitHub 514](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/514).

| Previous MLAPI Versions | V 0.1.0 Name |
| -- | -- |
| `NetworkingManager` | `NetworkManager` |
| `NetworkedObject` | `NetworkObject` |
| `NetworkedBehaviour` | `NetworkBehaviour` |
| `NetworkedClient` | `NetworkClient` |
| `NetworkedPrefab` | `NetworkPrefab` |
| `NetworkedVar` | `NetworkVariable` |
| `NetworkedTransform` | `NetworkTransform` |
| `NetworkedAnimator` | `NetworkAnimator` |
| `NetworkedAnimatorEditor` | `NetworkAnimatorEditor` |
| `NetworkedNavMeshAgent` | `NetworkNavMeshAgent` |
| `SpawnManager` | `NetworkSpawnManager` |
| `BitStream` | `NetworkBuffer` |
| `BitReader` | `NetworkReader` |
| `BitWriter` | `NetworkWriter` |
| `NetEventType` | `NetworkEventType` |
| `ChannelType` | `NetworkDelivery` |
| `Channel` | `NetworkChannel` |
| `Transport` | `NetworkTransport` |
| `NetworkedDictionary` | `NetworkDictionary` |
| `NetworkedList` | `NetworkList` |
| `NetworkedSet` | `NetworkSet` |
| `MLAPIConstants` | `NetworkConstants` |
| `UnetTransport` | `UNetTransport` |

### Fixed

- [GitHub 460](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/460): Fixed an issue for RPC where the host-server was not receiving RPCs from the host-client and vice versa without the loopback flag set in `NetworkingManager`.
- Fixed an issue where data in the Profiler was incorrectly aggregated and drawn, which caused the profiler data to increment indefinitely instead of resetting each frame.
- Fixed an issue the client soft-synced causing PlayMode client-only scene transition issues, caused when running the client in the editor and the host as a release build. Users may have encountered a soft sync of `NetworkedInstanceId` issues in the `SpawnManager.ClientCollectSoftSyncSceneObjectSweep` method.
- [GitHub 458](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/458): Fixed serialization issues in `NetworkList` and `NetworkDictionary` when running in Server mode.
- [GitHub 498](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/498): Fixed numerical precision issues to prevent not a number (NaN) quaternions.
- [GitHub 438](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/438): Fixed booleans by reaching or writing bytes instead of bits.
- [GitHub 519](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/519): Fixed an issue where calling `Shutdown()` before making `NetworkManager.Singleton = null` is null on `NetworkManager.OnDestroy()`.

### Removed

With a new release of MLAPI in Unity, some features have been removed:

- SyncVars have been removed from MLAPI. Use `NetworkVariable`s in place of this functionality. <!-- MTT54 -->
- [GitHub 527](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/527): Lag compensation systems and `TrackedObject` have moved to the new [MLAPI Community Contributions](https://github.com/Unity-Technologies/mlapi-community-contributions/tree/master/com.mlapi.contrib.extensions) repo.
- [GitHub 509](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/509): Encryption has been removed from MLAPI. The `Encryption` option in `NetworkConfig` on the `NetworkingManager` is not available in this release. This change will not block game creation or running. A current replacement for this functionality is not available, and may be developed in future releases. See the following changes:
    - Removed `SecuritySendFlags` from all APIs.
    - Removed encryption, cryptography, and certificate configurations from APIs including `NetworkManager` and `NetworkConfig`.
    - Removed "hail handshake", including `NetworkManager` implementation and `NetworkConstants` entries.
    - Modified `RpcQueue` and `RpcBatcher` internals to remove encryption and authentication from reading and writing.
- Removed the previous MLAPI Profiler editor window from Unity versions 2020.2 and later.
- Removed previous MLAPI Convenience and Performance RPC APIs with the new standard RPC API. See [RFC #1](https://github.com/Unity-Technologies/com.unity.multiplayer.rfcs/blob/master/text/0001-std-rpc-api.md) for details.
- [GitHub 520](https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/pull/520): Removed the MLAPI Installer.

### Known Issues

- `NetworkNavMeshAgent` does not synchronize mesh data, Agent Size, Steering, Obstacle Avoidance, or Path Finding settings. It only synchronizes the destination and velocity, not the path to the destination.
- For `RPC`, methods with a `ClientRpc` or `ServerRpc` suffix which are not marked with [ServerRpc] or [ClientRpc] will cause a compiler error.
- For `NetworkAnimator`, Animator Overrides are not supported. Triggers do not work.
- For `NetworkVariable`, the `NetworkDictionary` `List` and `Set` must use the `reliableSequenced` channel.
- `NetworkObjects`s are supported but when spawning a prefab with nested child network objects you have to manually call spawn on them
- `NetworkTransform` have the following issues:
  - Replicated objects may have jitter.
  - The owner is always authoritative about the object's position.
  - Scale is not synchronized.
- Connection Approval is not called on the host client.
- For `NamedMessages`, always use `NetworkBuffer` as the underlying stream for sending named and unnamed messages.
- For `NetworkManager`, connection management is limited. Use `IsServer`, `IsClient`, `IsConnectedClient`, or other code to check if MLAPI connected correctly.

## [0.0.1-preview.1] - 2020-12-20

This was an internally-only-used version of the Unity MLAPI Package
2020-12-20 00:00:00 +00:00

1901 lines
93 KiB
C#

using System.Collections.Generic;
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Unity.Netcode
{
/// <summary>
/// Used for local notifications of various scene events.
/// The <see cref="NetworkSceneManager.OnSceneEvent"/> of delegate type <see cref="NetworkSceneManager.SceneEventDelegate"/> uses this class to provide
/// scene event status/state.
/// </summary>
public class SceneEvent
{
/// <summary>
/// The <see cref="UnityEngine.AsyncOperation"/> returned by <see cref="SceneManager"/>
/// This is set for the following <see cref="Netcode.SceneEventType"/>s:
/// <see cref="SceneEventType.Load"/>
/// <see cref="SceneEventType.Unload"/>
/// </summary>
public AsyncOperation AsyncOperation;
/// <summary>
/// Will always be set to the current <see cref="Netcode.SceneEventType"/>
/// </summary>
public SceneEventType SceneEventType;
/// <summary>
/// If applicable, this reflects the type of scene loading or unloading that is occurring.
/// This is set for the following <see cref="Netcode.SceneEventType"/>s:
/// <see cref="SceneEventType.Load"/>
/// <see cref="SceneEventType.Unload"/>
/// <see cref="SceneEventType.LoadComplete"/>
/// <see cref="SceneEventType.UnloadComplete"/>
/// <see cref="SceneEventType.LoadEventCompleted"/>
/// <see cref="SceneEventType.UnloadEventCompleted"/>
/// </summary>
public LoadSceneMode LoadSceneMode;
/// <summary>
/// This will be set to the scene name that the event pertains to.
/// This is set for the following <see cref="Netcode.SceneEventType"/>s:
/// <see cref="SceneEventType.Load"/>
/// <see cref="SceneEventType.Unload"/>
/// <see cref="SceneEventType.LoadComplete"/>
/// <see cref="SceneEventType.UnloadComplete"/>
/// <see cref="SceneEventType.LoadEventCompleted"/>
/// <see cref="SceneEventType.UnloadEventCompleted"/>
/// </summary>
public string SceneName;
/// <summary>
/// When a scene is loaded, the Scene structure is returned.
/// This is set for the following <see cref="Netcode.SceneEventType"/>s:
/// <see cref="SceneEventType.LoadComplete"/>
/// </summary>
public Scene Scene;
/// <summary>
/// Events that always set the <see cref="ClientId"/> to the local client identifier,
/// are initiated (and processed locally) by the server-host, and sent to all clients
/// to be processed:
/// <see cref="SceneEventType.Load"/>
/// <see cref="SceneEventType.Unload"/>
/// <see cref="SceneEventType.Synchronize"/>
/// <see cref="SceneEventType.ReSynchronize"/>
///
/// Events that always set the <see cref="ClientId"/> to the local client identifier,
/// are initiated (and processed locally) by a client or server-host, and if initiated
/// by a client will always be sent to and processed on the server-host:
/// <see cref="SceneEventType.LoadComplete"/>
/// <see cref="SceneEventType.UnloadComplete"/>
/// <see cref="SceneEventType.SynchronizeComplete"/>
///
/// Events that always set the <see cref="ClientId"/> to the ServerId:
/// <see cref="SceneEventType.LoadEventCompleted"/>
/// <see cref="SceneEventType.UnloadEventCompleted"/>
/// </summary>
public ulong ClientId;
/// <summary>
/// List of clients that completed a loading or unloading event
/// This is set for the following <see cref="Netcode.SceneEventType"/>s:
/// <see cref="SceneEventType.LoadEventCompleted"/>
/// <see cref="SceneEventType.UnloadEventCompleted"/>
/// </summary>
public List<ulong> ClientsThatCompleted;
/// <summary>
/// List of clients that timed out during a loading or unloading event
/// This is set for the following <see cref="Netcode.SceneEventType"/>s:
/// <see cref="SceneEventType.LoadEventCompleted"/>
/// <see cref="SceneEventType.UnloadEventCompleted"/>
/// </summary>
public List<ulong> ClientsThatTimedOut;
}
/// <summary>
/// Main class for managing network scenes when <see cref="NetworkConfig.EnableSceneManagement"/> is enabled.
/// Uses the <see cref="SceneEventMessage"/> message to communicate <see cref="SceneEventData"/> between the server and client(s)
/// </summary>
public class NetworkSceneManager : IDisposable
{
private const NetworkDelivery k_DeliveryType = NetworkDelivery.ReliableFragmentedSequenced;
internal const int InvalidSceneNameOrPath = -1;
// Used to be able to turn re-synchronization off for future snapshot development purposes.
internal static bool DisableReSynchronization;
/// <summary>
/// Used to detect if a scene event is underway
/// Only 1 scene event can occur on the server at a time for now.
/// </summary>
private static bool s_IsSceneEventActive = false;
// TODO: Remove `m_IsRunningUnitTest` entirely after we switch to multi-process testing
// In MultiInstance tests, we cannot allow clients to load additional scenes as they're sharing the same scene space / Unity instance.
#if UNITY_INCLUDE_TESTS
private readonly bool m_IsRunningUnitTest = SceneManager.GetActiveScene().name.StartsWith("InitTestScene");
#endif
/// <summary>
/// The delegate callback definition for scene event notifications
/// For more details review over <see cref="SceneEvent"/> and <see cref="SceneEventData"/>
/// </summary>
/// <param name="sceneEvent"></param>
public delegate void SceneEventDelegate(SceneEvent sceneEvent);
/// <summary>
/// Event that will notify the local client or server of all scene events that take place
/// For more details review over <see cref="SceneEvent"/>, <see cref="SceneEventData"/>, and <see cref="SceneEventType"/>
/// Subscribe to this event to receive all <see cref="SceneEventType"/> notifications
///
/// Alternate Single Event Type Notification Registration Options
/// To receive only a specific event type notification or a limited set of notifications you can alternately subscribe to
/// each notification type individually via the following events:
/// -- <see cref="OnLoad"/> Invoked only when a <see cref="SceneEventType.Load"/> event is being processed
/// -- <see cref="OnUnload"/> Invoked only when an <see cref="SceneEventType.Unload"/> event is being processed
/// -- <see cref="OnSynchronize"/> Invoked only when a <see cref="SceneEventType.Synchronize"/> event is being processed
/// -- <see cref="OnLoadEventCompleted"/> Invoked only when a <see cref="SceneEventType.LoadEventCompleted"/> event is being processed
/// -- <see cref="OnUnloadEventCompleted"/> Invoked only when an <see cref="SceneEventType.UnloadEventCompleted"/> event is being processed
/// -- <see cref="OnLoadComplete"/> Invoked only when a <see cref="SceneEventType.LoadComplete"/> event is being processed
/// -- <see cref="OnUnloadComplete"/> Invoked only when an <see cref="SceneEventType.UnloadComplete"/> event is being processed
/// -- <see cref="OnSynchronizeComplete"/> Invoked only when a <see cref="SceneEventType.SynchronizeComplete"/> event is being processed
/// </summary>
public event SceneEventDelegate OnSceneEvent;
/// <summary>
/// Delegate declaration for the OnLoad event
/// View <see cref="SceneEventType.Load"/> for more information
/// </summary>
/// <param name="clientId">the client that is processing this event (the server will receive all of these events for every client and itself)</param>
/// <param name="sceneName">name of the scene being processed</param>
/// <param name="loadSceneMode">the LoadSceneMode mode for the scene being loaded</param>
/// <param name="asyncOperation">the associated <see cref="AsyncOperation"/> that can be used for scene loading progress</param>
public delegate void OnLoadDelegateHandler(ulong clientId, string sceneName, LoadSceneMode loadSceneMode, AsyncOperation asyncOperation);
/// <summary>
/// Delegate declaration for the OnUnload event
/// View <see cref="SceneEventType.Unload"/> for more information
/// </summary>
/// <param name="clientId">the client that is processing this event (the server will receive all of these events for every client and itself)</param>
/// <param name="sceneName">name of the scene being processed</param>
/// <param name="asyncOperation">the associated <see cref="AsyncOperation"/> that can be used for scene unloading progress</param>
public delegate void OnUnloadDelegateHandler(ulong clientId, string sceneName, AsyncOperation asyncOperation);
/// <summary>
/// Delegate declaration for the OnSynchronize event
/// View <see cref="SceneEventType.Synchronize"/> for more information
/// </summary>
/// <param name="clientId">the client that is processing this event (the server will receive all of these events for every client and itself)</param>
public delegate void OnSynchronizeDelegateHandler(ulong clientId);
/// <summary>
/// Delegate declaration for the OnLoadEventCompleted and OnUnloadEventCompleted events
/// View <see cref="SceneEventType.LoadEventCompleted"/> for more information
/// View <see cref="SceneEventType.UnloadEventCompleted"/> for more information
/// </summary>
/// <param name="sceneName">scene pertaining to this event</param>
/// <param name="loadSceneMode"><see cref="LoadSceneMode"/> of the associated event completed</param>
/// <param name="clientsCompleted">the clients that completed the loading event</param>
/// <param name="clientsTimedOut">the clients (if any) that timed out during the loading event</param>
public delegate void OnEventCompletedDelegateHandler(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut);
/// <summary>
/// Delegate declaration for the OnLoadComplete event
/// View <see cref="SceneEventType.LoadComplete"/> for more information
/// </summary>
/// <param name="clientId">the client that is processing this event (the server will receive all of these events for every client and itself)</param>
/// <param name="sceneName">the scene name pertaining to this event</param>
/// <param name="loadSceneMode">the mode the scene was loaded in</param>
public delegate void OnLoadCompleteDelegateHandler(ulong clientId, string sceneName, LoadSceneMode loadSceneMode);
/// <summary>
/// Delegate declaration for the OnUnloadComplete event
/// View <see cref="SceneEventType.UnloadComplete"/> for more information
/// </summary>
/// <param name="clientId">the client that is processing this event (the server will receive all of these events for every client and itself)</param>
/// <param name="sceneName">the scene name pertaining to this event</param>
public delegate void OnUnloadCompleteDelegateHandler(ulong clientId, string sceneName);
/// <summary>
/// Delegate declaration for the OnSynchronizeComplete event
/// View <see cref="SceneEventType.SynchronizeComplete"/> for more information
/// </summary>
/// <param name="clientId">the client that completed this event</param>
public delegate void OnSynchronizeCompleteDelegateHandler(ulong clientId);
/// <summary>
/// Invoked when a <see cref="SceneEventType.Load"/> event is started by the server
/// The server and client(s) will receive this notification
/// </summary>
public event OnLoadDelegateHandler OnLoad;
/// <summary>
/// Invoked when a <see cref="SceneEventType.Unload"/> event is started by the server
/// The server and client(s) will receive this notification
/// </summary>
public event OnUnloadDelegateHandler OnUnload;
/// <summary>
/// Invoked when a <see cref="SceneEventType.Synchronize"/> event is started by the server
/// after a client is approved for connection in order to synchronize the client with the currently loaded
/// scenes and NetworkObjects. This event signifies the beginning of the synchronization event.
/// The server and client will receive this notification
/// Note: this event is generated on a per newly connected and approved client basis
/// </summary>
public event OnSynchronizeDelegateHandler OnSynchronize;
/// <summary>
/// Invoked when a <see cref="SceneEventType.LoadEventCompleted"/> event is generated by the server.
/// This event signifies the end of an existing <see cref="SceneEventType.Load"/> event as it pertains
/// to all clients connected when the event was started. This event signifies that all clients (and server) have
/// finished the <see cref="SceneEventType.Load"/> event.
/// Note: this is useful to know when all clients have loaded the same scene (single or additive mode)
/// </summary>
public event OnEventCompletedDelegateHandler OnLoadEventCompleted;
/// <summary>
/// Invoked when a <see cref="SceneEventType.UnloadEventCompleted"/> event is generated by the server.
/// This event signifies the end of an existing <see cref="SceneEventType.Unload"/> event as it pertains
/// to all clients connected when the event was started. This event signifies that all clients (and server) have
/// finished the <see cref="SceneEventType.Unload"/> event.
/// Note: this is useful to know when all clients have unloaded a specific scene. The <see cref="LoadSceneMode"/> will
/// always be <see cref="LoadSceneMode.Additive"/> for this event
/// </summary>
public event OnEventCompletedDelegateHandler OnUnloadEventCompleted;
/// <summary>
/// Invoked when a <see cref="SceneEventType.LoadComplete"/> event is generated by a client or server.
/// The server receives this message from all clients (including itself).
/// Each client receives their own notification sent to the server.
/// </summary>
public event OnLoadCompleteDelegateHandler OnLoadComplete;
/// <summary>
/// Invoked when a <see cref="SceneEventType.UnloadComplete"/> event is generated by a client or server.
/// The server receives this message from all clients (including itself).
/// Each client receives their own notification sent to the server.
/// </summary>
public event OnUnloadCompleteDelegateHandler OnUnloadComplete;
/// <summary>
/// Invoked when a <see cref="SceneEventType.SynchronizeComplete"/> event is generated by a client.
/// The server receives this message from the client, but will never generate this event for itself.
/// Each client receives their own notification sent to the server.
/// Note: This is useful to know that a client has completed the entire connection sequence, loaded all scenes, and
/// synchronized all NetworkObjects.
/// </summary>
public event OnSynchronizeCompleteDelegateHandler OnSynchronizeComplete;
/// <summary>
/// Delegate declaration for the <see cref="VerifySceneBeforeLoading"/> handler that provides
/// an additional level of scene loading security and/or validation to assure the scene being loaded
/// is valid scene to be loaded in the LoadSceneMode specified.
/// </summary>
/// <param name="sceneIndex">Build Settings Scenes in Build List index of the scene</param>
/// <param name="sceneName">Name of the scene</param>
/// <param name="loadSceneMode">LoadSceneMode the scene is going to be loaded</param>
/// <returns>true (valid) or false (not valid)</returns>
public delegate bool VerifySceneBeforeLoadingDelegateHandler(int sceneIndex, string sceneName, LoadSceneMode loadSceneMode);
/// <summary>
/// Delegate handler defined by <see cref="VerifySceneBeforeLoadingDelegateHandler"/> that is invoked before the
/// server or client loads a scene during an active netcode game session.
/// Client Side: In order for clients to be notified of this condition you must assign the <see cref="VerifySceneBeforeLoading"/> delegate handler.
/// Server Side: <see cref="LoadScene(string, LoadSceneMode)"/> will return <see cref="SceneEventProgressStatus.SceneFailedVerification"/>.
/// </summary>
public VerifySceneBeforeLoadingDelegateHandler VerifySceneBeforeLoading;
internal readonly Dictionary<Guid, SceneEventProgress> SceneEventProgressTracking = new Dictionary<Guid, SceneEventProgress>();
/// <summary>
/// Used to track in-scene placed NetworkObjects
/// We store them by:
/// [GlobalObjectIdHash][Scene.Handle][NetworkObject]
/// The Scene.Handle aspect allows us to distinguish duplicated in-scene placed NetworkObjects created by the loading
/// of the same additive scene multiple times.
/// </summary>
internal readonly Dictionary<uint, Dictionary<int, NetworkObject>> ScenePlacedObjects = new Dictionary<uint, Dictionary<int, NetworkObject>>();
/// <summary>
/// This is used for the deserialization of in-scene placed NetworkObjects in order to distinguish duplicated in-scene
/// placed NetworkObjects created by the loading of the same additive scene multiple times.
/// </summary>
internal Scene SceneBeingSynchronized;
/// <summary>
/// Used to track which scenes are currently loaded
/// We store the scenes as [SceneHandle][Scene] in order to handle the loading and unloading of the same scene additively
/// Scene handle is only unique locally. So, clients depend upon the <see cref="ServerSceneHandleToClientSceneHandle"/> in order
/// to be able to know which specific scene instance the server is instructing the client to unload.
/// The client links the server scene handle to the client local scene handle upon a scene being loaded
/// <see cref="GetAndAddNewlyLoadedSceneByName"/>
/// </summary>
internal Dictionary<int, Scene> ScenesLoaded = new Dictionary<int, Scene>();
/// <summary>
/// Since Scene.handle is unique per client, we create a look-up table between the client and server to associate server unique scene
/// instances with client unique scene instances
/// </summary>
internal Dictionary<int, int> ServerSceneHandleToClientSceneHandle = new Dictionary<int, int>();
/// <summary>
/// Hash to build index lookup table
/// </summary>
internal Dictionary<uint, int> HashToBuildIndex = new Dictionary<uint, int>();
/// <summary>
/// Build index to hash lookup table
/// </summary>
internal Dictionary<int, uint> BuildIndexToHash = new Dictionary<int, uint>();
/// <summary>
/// The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned
/// they need to be moved into the do not destroy temporary scene
/// When it is set: Just before starting the asynchronous loading call
/// When it is unset: After the scene has loaded, the PopulateScenePlacedObjects is called, and all NetworkObjects in the do
/// not destroy temporary scene are moved into the active scene
/// </summary>
internal static bool IsSpawnedObjectsPendingInDontDestroyOnLoad;
/// <summary>
/// Client and Server:
/// Used for all scene event processing
/// </summary>
internal Dictionary<uint, SceneEventData> SceneEventDataStore;
private NetworkManager m_NetworkManager { get; }
internal Scene DontDestroyOnLoadScene;
/// <summary>
/// LoadSceneMode.Single: All currently loaded scenes on the client will be unloaded and
/// the server's currently active scene will be loaded in single mode on the client
/// unless it was already loaded.
///
/// LoadSceneMode.Additive: All currently loaded scenes are left as they are and any newly loaded
/// scenes will be loaded additively. Users need to determine which scenes are valid to load via the
/// <see cref="VerifySceneBeforeLoading"/> method.
/// </summary>
public LoadSceneMode ClientSynchronizationMode { get; internal set; }
/// <summary>
/// When true, the <see cref="Debug.LogWarning(object)"/> messages will be turned off
/// </summary>
private bool m_DisableValidationWarningMessages;
/// <summary>
/// Handle NetworkSeneManager clean up
/// </summary>
public void Dispose()
{
foreach (var keypair in SceneEventDataStore)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogInfo($"{nameof(SceneEventDataStore)} is disposing {nameof(SceneEventData.SceneEventId)} '{keypair.Key}'.");
}
keypair.Value.Dispose();
}
SceneEventDataStore.Clear();
SceneEventDataStore = null;
}
/// <summary>
/// Creates a new SceneEventData object for a new scene event
/// </summary>
/// <returns>SceneEventData instance</returns>
internal SceneEventData BeginSceneEvent()
{
var sceneEventData = new SceneEventData(m_NetworkManager);
SceneEventDataStore.Add(sceneEventData.SceneEventId, sceneEventData);
return sceneEventData;
}
/// <summary>
/// Disposes and removes SceneEventData object for the scene event
/// </summary>
/// <param name="sceneEventId">SceneEventId to end</param>
internal void EndSceneEvent(uint sceneEventId)
{
if (SceneEventDataStore.ContainsKey(sceneEventId))
{
SceneEventDataStore[sceneEventId].Dispose();
SceneEventDataStore.Remove(sceneEventId);
}
else
{
Debug.LogWarning($"Trying to dispose and remove SceneEventData Id '{sceneEventId}' that no longer exists!");
}
}
/// <summary>
/// Gets the scene name from full path to the scene
/// </summary>
internal string GetSceneNameFromPath(string scenePath)
{
var begin = scenePath.LastIndexOf("/", StringComparison.Ordinal) + 1;
var end = scenePath.LastIndexOf(".", StringComparison.Ordinal);
return scenePath.Substring(begin, end - begin);
}
/// <summary>
/// Generates the hash values and associated tables
/// for the scenes in build list
/// </summary>
internal void GenerateScenesInBuild()
{
HashToBuildIndex.Clear();
BuildIndexToHash.Clear();
for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
{
var scenePath = SceneUtility.GetScenePathByBuildIndex(i);
var hash = XXHash.Hash32(scenePath);
var buildIndex = SceneUtility.GetBuildIndexByScenePath(scenePath);
HashToBuildIndex.Add(hash, buildIndex);
BuildIndexToHash.Add(buildIndex, hash);
}
}
/// <summary>
/// Gets the scene name from a hash value generated from the full scene path
/// </summary>
internal string SceneNameFromHash(uint sceneHash)
{
// In the event there is no scene associated with the scene event then just return "No Scene"
// This can happen during unit tests when clients first connect and the only scene loaded is the
// unit test scene (which is ignored by default) that results in a scene event that has no associated
// scene. Under this specific special case, we just return "No Scene".
if (sceneHash == 0)
{
return "No Scene";
}
return GetSceneNameFromPath(ScenePathFromHash(sceneHash));
}
/// <summary>
/// Gets the full scene path from a hash value
/// </summary>
internal string ScenePathFromHash(uint sceneHash)
{
if (HashToBuildIndex.ContainsKey(sceneHash))
{
return SceneUtility.GetScenePathByBuildIndex(HashToBuildIndex[sceneHash]);
}
else
{
throw new Exception($"Scene Hash {sceneHash} does not exist in the {nameof(HashToBuildIndex)} table!");
}
}
/// <summary>
/// Gets the associated hash value for the scene name or path
/// </summary>
internal uint SceneHashFromNameOrPath(string sceneNameOrPath)
{
var buildIndex = SceneUtility.GetBuildIndexByScenePath(sceneNameOrPath);
if (buildIndex >= 0)
{
if (BuildIndexToHash.ContainsKey(buildIndex))
{
return BuildIndexToHash[buildIndex];
}
else
{
throw new Exception($"Scene '{sceneNameOrPath}' has a build index of {buildIndex} that does not exist in the {nameof(BuildIndexToHash)} table!");
}
}
else
{
throw new Exception($"Scene '{sceneNameOrPath}' couldn't be loaded because it has not been added to the build settings scenes in build list.");
}
}
/// <summary>
/// When set to true, this will disable the console warnings about
/// a scene being invalidated.
/// </summary>
/// <param name="disabled">true/false</param>
public void DisableValidationWarnings(bool disabled)
{
m_DisableValidationWarningMessages = disabled;
}
/// <summary>
/// This will change how clients are initially synchronized.
/// LoadSceneMode.Single: All currently loaded scenes on the client will be unloaded and
/// the server's currently active scene will be loaded in single mode on the client
/// unless it was already loaded.
///
/// LoadSceneMode.Additive: All currently loaded scenes are left as they are and any newly loaded
/// scenes will be loaded additively. Users need to determine which scenes are valid to load via the
/// <see cref="VerifySceneBeforeLoading"/> method.
/// </summary>
/// <param name="mode"><see cref="LoadSceneMode"/> for initial client synchronization</param>
public void SetClientSynchronizationMode(LoadSceneMode mode)
{
ClientSynchronizationMode = mode;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="networkManager">one <see cref="NetworkManager"/> instance per <see cref="NetworkSceneManager"/> instance</param>
/// <param name="sceneEventDataPoolSize">maximum <see cref="SceneEventData"/> pool size</param>
internal NetworkSceneManager(NetworkManager networkManager)
{
m_NetworkManager = networkManager;
SceneEventDataStore = new Dictionary<uint, SceneEventData>();
GenerateScenesInBuild();
// If NetworkManager has this set to true, then we can get the DDOL (DontDestroyOnLoad) from its GaemObject
if (networkManager.DontDestroy)
{
DontDestroyOnLoadScene = networkManager.gameObject.scene;
}
else
{
// Otherwise, we have to create a GameObject and move it into the DDOL in order to
// register the DDOL scene handle with NetworkSceneManager
var myDDOLObject = new GameObject("DDOL-NWSM");
UnityEngine.Object.DontDestroyOnLoad(myDDOLObject);
DontDestroyOnLoadScene = myDDOLObject.scene;
UnityEngine.Object.Destroy(myDDOLObject);
}
ServerSceneHandleToClientSceneHandle.Add(DontDestroyOnLoadScene.handle, DontDestroyOnLoadScene.handle);
ScenesLoaded.Add(DontDestroyOnLoadScene.handle, DontDestroyOnLoadScene);
}
/// <summary>
/// If the VerifySceneBeforeLoading delegate handler has been set by the user, this will provide
/// an additional level of security and/or validation that the scene being loaded in the specified
/// loading mode is "a valid scene to be loaded in the LoadSceneMode specified".
/// </summary>
/// <param name="sceneIndex">index into ScenesInBuild</param>
/// <param name="loadSceneMode">LoadSceneMode the scene is going to be loaded</param>
/// <returns>true (Valid) or false (Invalid)</returns>
internal bool ValidateSceneBeforeLoading(uint sceneHash, LoadSceneMode loadSceneMode)
{
var validated = true;
var sceneName = SceneNameFromHash(sceneHash);
var sceneIndex = SceneUtility.GetBuildIndexByScenePath(sceneName);
if (VerifySceneBeforeLoading != null)
{
validated = VerifySceneBeforeLoading.Invoke((int)sceneIndex, sceneName, loadSceneMode);
}
if (!validated && !m_DisableValidationWarningMessages)
{
var serverHostorClient = "Client";
if (m_NetworkManager.IsServer)
{
serverHostorClient = m_NetworkManager.IsHost ? "Host" : "Server";
}
Debug.LogWarning($"Scene {sceneName} of Scenes in Build Index {sceneIndex} being loaded in {loadSceneMode} mode failed validation on the {serverHostorClient}!");
}
return validated;
}
/// <summary>
/// Since SceneManager.GetSceneByName only returns the first scene that matches the name
/// we must "find" a newly added scene by looking through all loaded scenes and determining
/// which scene with the same name has not yet been loaded.
/// In order to support loading the same additive scene within in-scene placed NetworkObjects,
/// we must do this to be able to soft synchronize the "right version" of the NetworkObject.
/// </summary>
/// <param name="sceneName"></param>
/// <returns></returns>
internal Scene GetAndAddNewlyLoadedSceneByName(string sceneName)
{
for (int i = 0; i < SceneManager.sceneCount; i++)
{
var sceneLoaded = SceneManager.GetSceneAt(i);
if (sceneLoaded.name == sceneName)
{
if (!ScenesLoaded.ContainsKey(sceneLoaded.handle))
{
ScenesLoaded.Add(sceneLoaded.handle, sceneLoaded);
return sceneLoaded;
}
}
}
throw new Exception($"Failed to find any loaded scene named {sceneName}!");
}
/// <summary>
/// Client Side Only:
/// This takes a server scene handle that is written by the server before the scene relative
/// NetworkObject is serialized and converts the server scene handle to a local client handle
/// so it can set the appropriate SceneBeingSynchronized.
/// Note: This is now part of the soft synchronization process and is needed for the scenario
/// where a user loads the same scene additively that has an in-scene placed NetworkObject
/// which means each scene relative in-scene placed NetworkObject will have the identical GlobalObjectIdHash
/// value. Scene handles are used to distinguish between in-scene placed NetworkObjects under this situation.
/// </summary>
/// <param name="serverSceneHandle"></param>
internal void SetTheSceneBeingSynchronized(int serverSceneHandle)
{
var clientSceneHandle = serverSceneHandle;
if (ServerSceneHandleToClientSceneHandle.ContainsKey(serverSceneHandle))
{
clientSceneHandle = ServerSceneHandleToClientSceneHandle[serverSceneHandle];
// If we were already set, then ignore
if (SceneBeingSynchronized.IsValid() && SceneBeingSynchronized.isLoaded && SceneBeingSynchronized.handle == clientSceneHandle)
{
return;
}
// Get the scene currently being synchronized
SceneBeingSynchronized = ScenesLoaded.ContainsKey(clientSceneHandle) ? ScenesLoaded[clientSceneHandle] : new Scene();
if (!SceneBeingSynchronized.IsValid() || !SceneBeingSynchronized.isLoaded)
{
// Let's go ahead and use the currently active scene under the scenario where a NetworkObject is determined to exist in a scene that the NetworkSceneManager is not aware of
SceneBeingSynchronized = SceneManager.GetActiveScene();
// Keeping the warning here in the event we cannot find the scene being synchronized
Debug.LogWarning($"[{nameof(NetworkSceneManager)}- {nameof(ScenesLoaded)}] Could not find the appropriate scene to set as being synchronized! Using the currently active scene.");
}
}
else
{
// Most common scenario for DontDestroyOnLoad is when NetworkManager is set to not be destroyed
if (serverSceneHandle == DontDestroyOnLoadScene.handle)
{
SceneBeingSynchronized = m_NetworkManager.gameObject.scene;
return;
}
else
{
// Let's go ahead and use the currently active scene under the scenario where a NetworkObject is determined to exist in a scene that the NetworkSceneManager is not aware of
// or the NetworkObject has yet to be moved to that specific scene (i.e. no DontDestroyOnLoad scene exists yet).
SceneBeingSynchronized = SceneManager.GetActiveScene();
// This could be the scenario where NetworkManager.DontDestroy is false and we are creating the first NetworkObject (client side) to be in the DontDestroyOnLoad scene
// Otherwise, this is some other specific scenario that we might not be handling currently.
Debug.LogWarning($"[{nameof(SceneEventData)}- Scene Handle Mismatch] {nameof(serverSceneHandle)} could not be found in {nameof(ServerSceneHandleToClientSceneHandle)}. Using the currently active scene.");
}
}
}
/// <summary>
/// During soft synchronization of in-scene placed NetworkObjects, this is now used by NetworkSpawnManager.CreateLocalNetworkObject
/// </summary>
/// <param name="globalObjectIdHash"></param>
/// <returns></returns>
internal NetworkObject GetSceneRelativeInSceneNetworkObject(uint globalObjectIdHash)
{
if (ScenePlacedObjects.ContainsKey(globalObjectIdHash))
{
if (ScenePlacedObjects[globalObjectIdHash].ContainsKey(SceneBeingSynchronized.handle))
{
var inScenePlacedNetworkObject = ScenePlacedObjects[globalObjectIdHash][SceneBeingSynchronized.handle];
// We can only have 1 duplicated globalObjectIdHash per scene instance, so remove it once it has been returned
ScenePlacedObjects[globalObjectIdHash].Remove(SceneBeingSynchronized.handle);
return inScenePlacedNetworkObject;
}
}
return null;
}
/// <summary>
/// Generic sending of scene event data
/// </summary>
/// <param name="targetClientIds">array of client identifiers to receive the scene event message</param>
private void SendSceneEventData(uint sceneEventId, ulong[] targetClientIds)
{
if (targetClientIds.Length == 0)
{
// This would be the Host/Server with no clients connected
// Silently return as there is nothing to be done
return;
}
var message = new SceneEventMessage
{
EventData = SceneEventDataStore[sceneEventId]
};
var size = m_NetworkManager.SendMessage(message, k_DeliveryType, targetClientIds);
m_NetworkManager.NetworkMetrics.TrackSceneEventSent(
targetClientIds, (uint)SceneEventDataStore[sceneEventId].SceneEventType, SceneNameFromHash(SceneEventDataStore[sceneEventId].SceneHash), size);
}
/// <summary>
/// Entry method for scene unloading validation
/// </summary>
/// <param name="scene">the scene to be unloaded</param>
/// <returns></returns>
private SceneEventProgress ValidateSceneEventUnLoading(Scene scene)
{
if (!m_NetworkManager.IsServer)
{
throw new NotServerException("Only server can start a scene event!");
}
if (!m_NetworkManager.NetworkConfig.EnableSceneManagement)
{
//Log message about enabling SceneManagement
throw new Exception($"{nameof(NetworkConfig.EnableSceneManagement)} flag is not enabled in the {nameof(NetworkManager)}'s {nameof(NetworkConfig)}. " +
$"Please set {nameof(NetworkConfig.EnableSceneManagement)} flag to true before calling " +
$"{nameof(NetworkSceneManager.LoadScene)} or {nameof(NetworkSceneManager.UnloadScene)}.");
}
if (!scene.isLoaded)
{
Debug.LogWarning($"{nameof(UnloadScene)} was called, but the scene {scene.name} is not currently loaded!");
return new SceneEventProgress(null, SceneEventProgressStatus.SceneNotLoaded);
}
return ValidateSceneEvent(scene.name, true);
}
/// <summary>
/// Entry method for scene loading validation
/// </summary>
/// <param name="sceneName">scene name to load</param>
/// <returns></returns>
private SceneEventProgress ValidateSceneEventLoading(string sceneName)
{
if (!m_NetworkManager.IsServer)
{
throw new NotServerException("Only server can start a scene event!");
}
if (!m_NetworkManager.NetworkConfig.EnableSceneManagement)
{
//Log message about enabling SceneManagement
throw new Exception($"{nameof(NetworkConfig.EnableSceneManagement)} flag is not enabled in the {nameof(NetworkManager)}'s {nameof(NetworkConfig)}. " +
$"Please set {nameof(NetworkConfig.EnableSceneManagement)} flag to true before calling " +
$"{nameof(NetworkSceneManager.LoadScene)} or {nameof(NetworkSceneManager.UnloadScene)}.");
}
return ValidateSceneEvent(sceneName);
}
/// <summary>
/// Validates the new scene event request by the server-side code.
/// This also initializes some commonly shared values as well as SceneEventProgress
/// </summary>
/// <param name="sceneName"></param>
/// <returns><see cref="SceneEventProgress"/> that should have a <see cref="SceneEventProgress.Status"/> of <see cref="SceneEventProgressStatus.Started"/> otherwise it failed.</returns>
private SceneEventProgress ValidateSceneEvent(string sceneName, bool isUnloading = false)
{
// Return scene event already in progress if one is already in progress
if (s_IsSceneEventActive)
{
return new SceneEventProgress(null, SceneEventProgressStatus.SceneEventInProgress);
}
// Return invalid scene name status if the scene name is invalid
if (SceneUtility.GetBuildIndexByScenePath(sceneName) == InvalidSceneNameOrPath)
{
Debug.LogError($"Scene '{sceneName}' couldn't be loaded because it has not been added to the build settings scenes in build list.");
return new SceneEventProgress(null, SceneEventProgressStatus.InvalidSceneName);
}
var sceneEventProgress = new SceneEventProgress(m_NetworkManager)
{
SceneHash = SceneHashFromNameOrPath(sceneName)
};
SceneEventProgressTracking.Add(sceneEventProgress.Guid, sceneEventProgress);
if (!isUnloading)
{
// The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned
// they need to be moved into the do not destroy temporary scene
// When it is set: Just before starting the asynchronous loading call
// When it is unset: After the scene has loaded, the PopulateScenePlacedObjects is called, and all NetworkObjects in the do
// not destroy temporary scene are moved into the active scene
IsSpawnedObjectsPendingInDontDestroyOnLoad = true;
}
s_IsSceneEventActive = true;
// Set our callback delegate handler for completion
sceneEventProgress.OnComplete = OnSceneEventProgressCompleted;
return sceneEventProgress;
}
/// <summary>
/// Callback for the <see cref="SceneEventProgress.OnComplete"/> <see cref="SceneEventProgress.OnCompletedDelegate"/> handler
/// </summary>
/// <param name="sceneEventProgress"></param>
/// <returns></returns>
private bool OnSceneEventProgressCompleted(SceneEventProgress sceneEventProgress)
{
var sceneEventData = BeginSceneEvent();
sceneEventData.SceneEventProgressId = sceneEventProgress.Guid;
sceneEventData.SceneHash = sceneEventProgress.SceneHash;
sceneEventData.SceneEventType = sceneEventProgress.SceneEventType;
sceneEventData.ClientsCompleted = sceneEventProgress.DoneClients;
sceneEventData.LoadSceneMode = sceneEventProgress.LoadSceneMode;
sceneEventData.ClientsTimedOut = m_NetworkManager.ConnectedClients.Keys.Except(sceneEventProgress.DoneClients).ToList();
var message = new SceneEventMessage
{
EventData = sceneEventData
};
var size = m_NetworkManager.SendMessage(message, k_DeliveryType, m_NetworkManager.ConnectedClientsIds);
m_NetworkManager.NetworkMetrics.TrackSceneEventSent(
m_NetworkManager.ConnectedClientsIds,
(uint)sceneEventProgress.SceneEventType,
SceneNameFromHash(sceneEventProgress.SceneHash),
size);
// Send a local notification to the server that all clients are done loading or unloading
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = sceneEventProgress.SceneEventType,
SceneName = SceneNameFromHash(sceneEventProgress.SceneHash),
ClientId = m_NetworkManager.ServerClientId,
LoadSceneMode = sceneEventProgress.LoadSceneMode,
ClientsThatCompleted = sceneEventProgress.DoneClients,
ClientsThatTimedOut = m_NetworkManager.ConnectedClients.Keys.Except(sceneEventProgress.DoneClients).ToList(),
});
if (sceneEventData.SceneEventType == SceneEventType.LoadEventCompleted)
{
OnLoadEventCompleted?.Invoke(SceneNameFromHash(sceneEventProgress.SceneHash), sceneEventProgress.LoadSceneMode, sceneEventData.ClientsCompleted, sceneEventData.ClientsTimedOut);
}
else
{
OnUnloadEventCompleted?.Invoke(SceneNameFromHash(sceneEventProgress.SceneHash), sceneEventProgress.LoadSceneMode, sceneEventData.ClientsCompleted, sceneEventData.ClientsTimedOut);
}
EndSceneEvent(sceneEventData.SceneEventId);
return true;
}
/// <summary>
/// Server Side:
/// Unloads an additively loaded scene. If you want to unload a <see cref="LoadSceneMode.Single"/> mode loaded scene load another <see cref="LoadSceneMode.Single"/> scene.
/// When applicable, the <see cref="AsyncOperation"/> is delivered within the <see cref="SceneEvent"/> via the <see cref="OnSceneEvent"/>
/// </summary>
/// <param name="sceneName">scene name to unload</param>
/// <returns><see cref="SceneEventProgressStatus"/> (<see cref="SceneEventProgressStatus.Started"/> means it was successful)</returns>
public SceneEventProgressStatus UnloadScene(Scene scene)
{
var sceneName = scene.name;
var sceneHandle = scene.handle;
if (!scene.isLoaded)
{
Debug.LogWarning($"{nameof(UnloadScene)} was called, but the scene {scene.name} is not currently loaded!");
return SceneEventProgressStatus.SceneNotLoaded;
}
var sceneEventProgress = ValidateSceneEventUnLoading(scene);
if (sceneEventProgress.Status != SceneEventProgressStatus.Started)
{
return sceneEventProgress.Status;
}
if (!ScenesLoaded.ContainsKey(sceneHandle))
{
Debug.LogError($"{nameof(UnloadScene)} internal error! {sceneName} with handle {scene.handle} is not within the internal scenes loaded dictionary!");
return SceneEventProgressStatus.InternalNetcodeError;
}
var sceneEventData = BeginSceneEvent();
sceneEventData.SceneEventProgressId = sceneEventProgress.Guid;
sceneEventData.SceneEventType = SceneEventType.Unload;
sceneEventData.SceneHash = SceneHashFromNameOrPath(sceneName);
sceneEventData.LoadSceneMode = LoadSceneMode.Additive; // The only scenes unloaded are scenes that were additively loaded
sceneEventData.SceneHandle = sceneHandle;
// This will be the message we send to everyone when this scene event sceneEventProgress is complete
sceneEventProgress.SceneEventType = SceneEventType.UnloadEventCompleted;
ScenesLoaded.Remove(scene.handle);
AsyncOperation sceneUnload = SceneManager.UnloadSceneAsync(scene);
sceneUnload.completed += (AsyncOperation asyncOp2) => { OnSceneUnloaded(sceneEventData.SceneEventId); };
sceneEventProgress.SetSceneLoadOperation(sceneUnload);
// Notify local server that a scene is going to be unloaded
OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = sceneUnload,
SceneEventType = sceneEventData.SceneEventType,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = sceneName,
ClientId = m_NetworkManager.ServerClientId // Server can only invoke this
});
OnUnload?.Invoke(m_NetworkManager.ServerClientId, sceneName, sceneUnload);
//Return the status
return sceneEventProgress.Status;
}
/// <summary>
/// Client Side:
/// Handles <see cref="SceneEventType.Unload"/> scene events.
/// </summary>
private void OnClientUnloadScene(uint sceneEventId)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
var sceneName = SceneNameFromHash(sceneEventData.SceneHash);
if (!ServerSceneHandleToClientSceneHandle.ContainsKey(sceneEventData.SceneHandle))
{
throw new Exception($"Client failed to unload scene {sceneName} " +
$"because we are missing the client scene handle due to the server scene handle {sceneEventData.SceneHandle} not being found!");
}
var sceneHandle = ServerSceneHandleToClientSceneHandle[sceneEventData.SceneHandle];
if (!ScenesLoaded.ContainsKey(sceneHandle))
{
// Error scene handle not found!
throw new Exception($"Client failed to unload scene {sceneName} " +
$"because the client scene handle {sceneHandle} was not found in ScenesLoaded!");
}
s_IsSceneEventActive = true;
var sceneUnload = (AsyncOperation)null;
#if UNITY_INCLUDE_TESTS
if (m_IsRunningUnitTest)
{
sceneUnload = new AsyncOperation();
}
else
{
sceneUnload = SceneManager.UnloadSceneAsync(ScenesLoaded[sceneHandle]);
sceneUnload.completed += asyncOp2 => OnSceneUnloaded(sceneEventId);
}
#else
sceneUnload = SceneManager.UnloadSceneAsync(ScenesLoaded[sceneHandle]);
sceneUnload.completed += asyncOp2 => OnSceneUnloaded(sceneEventId);
#endif
ScenesLoaded.Remove(sceneHandle);
// Remove our server to scene handle lookup
ServerSceneHandleToClientSceneHandle.Remove(sceneEventData.SceneHandle);
// Notify the local client that a scene is going to be unloaded
OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = sceneUnload,
SceneEventType = sceneEventData.SceneEventType,
LoadSceneMode = LoadSceneMode.Additive, // The only scenes unloaded are scenes that were additively loaded
SceneName = sceneName,
ClientId = m_NetworkManager.LocalClientId // Server sent this message to the client, but client is executing it
});
OnUnload?.Invoke(m_NetworkManager.LocalClientId, sceneName, sceneUnload);
#if UNITY_INCLUDE_TESTS
if (m_IsRunningUnitTest)
{
OnSceneUnloaded(sceneEventId);
}
#endif
}
/// <summary>
/// Server and Client:
/// Invoked when an additively loaded scene is unloaded
/// </summary>
private void OnSceneUnloaded(uint sceneEventId)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
// First thing we do, if we are a server, is to send the unload scene event.
if (m_NetworkManager.IsServer)
{
// Server sends the unload scene notification after unloading because it will despawn all scene relative in-scene NetworkObjects
// If we send this event to all clients before the server is finished unloading they will get warning about an object being
// despawned that no longer exists
SendSceneEventData(sceneEventId, m_NetworkManager.ConnectedClientsIds.Where(c => c != m_NetworkManager.ServerClientId).ToArray());
//Second, server sets itself as having finished unloading
if (SceneEventProgressTracking.ContainsKey(sceneEventData.SceneEventProgressId))
{
SceneEventProgressTracking[sceneEventData.SceneEventProgressId].AddClientAsDone(m_NetworkManager.ServerClientId);
}
}
// Next we prepare to send local notifications for unload complete
sceneEventData.SceneEventType = SceneEventType.UnloadComplete;
//Notify the client or server that a scene was unloaded
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = sceneEventData.SceneEventType,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = SceneNameFromHash(sceneEventData.SceneHash),
ClientId = m_NetworkManager.IsServer ? m_NetworkManager.ServerClientId : m_NetworkManager.LocalClientId
});
OnUnloadComplete?.Invoke(m_NetworkManager.LocalClientId, SceneNameFromHash(sceneEventData.SceneHash));
// Clients send a notification back to the server they have completed the unload scene event
if (!m_NetworkManager.IsServer)
{
SendSceneEventData(sceneEventId, new ulong[] { m_NetworkManager.ServerClientId });
}
EndSceneEvent(sceneEventId);
// This scene event is now considered "complete"
s_IsSceneEventActive = false;
}
/// <summary>
/// Clears all scenes when loading in single mode
/// Since we assume a single mode loaded scene will be considered the "currently active scene",
/// we only unload any additively loaded scenes.
/// </summary>
internal void UnloadAdditivelyLoadedScenes()
{
// Unload all additive scenes while making sure we don't try to unload the base scene ( loaded in single mode ).
var currentActiveScene = SceneManager.GetActiveScene();
foreach (var keyHandleEntry in ScenesLoaded)
{
if (currentActiveScene.name != keyHandleEntry.Value.name)
{
OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = SceneManager.UnloadSceneAsync(keyHandleEntry.Value),
SceneEventType = SceneEventType.Unload,
SceneName = keyHandleEntry.Value.name,
LoadSceneMode = LoadSceneMode.Additive, // The only scenes unloaded are scenes that were additively loaded
ClientId = m_NetworkManager.ServerClientId
});
}
}
// clear out our scenes loaded list
ScenesLoaded.Clear();
}
/// <summary>
/// Server side:
/// Loads the scene name in either additive or single loading mode.
/// When applicable, the <see cref="AsyncOperation"/> is delivered within the <see cref="SceneEvent"/> via <see cref="OnSceneEvent"/>
/// </summary>
/// <param name="sceneName">the name of the scene to be loaded</param>
/// <returns><see cref="SceneEventProgressStatus"/> (<see cref="SceneEventProgressStatus.Started"/> means it was successful)</returns>
public SceneEventProgressStatus LoadScene(string sceneName, LoadSceneMode loadSceneMode)
{
var sceneEventProgress = ValidateSceneEventLoading(sceneName);
if (sceneEventProgress.Status != SceneEventProgressStatus.Started)
{
return sceneEventProgress.Status;
}
// This will be the message we send to everyone when this scene event sceneEventProgress is complete
sceneEventProgress.SceneEventType = SceneEventType.LoadEventCompleted;
sceneEventProgress.LoadSceneMode = loadSceneMode;
var sceneEventData = BeginSceneEvent();
// Now set up the current scene event
sceneEventData.SceneEventProgressId = sceneEventProgress.Guid;
sceneEventData.SceneEventType = SceneEventType.Load;
sceneEventData.SceneHash = SceneHashFromNameOrPath(sceneName);
sceneEventData.LoadSceneMode = loadSceneMode;
// This both checks to make sure the scene is valid and if not resets the active scene event
s_IsSceneEventActive = ValidateSceneBeforeLoading(sceneEventData.SceneHash, loadSceneMode);
if (!s_IsSceneEventActive)
{
EndSceneEvent(sceneEventData.SceneEventId);
return SceneEventProgressStatus.SceneFailedVerification;
}
if (sceneEventData.LoadSceneMode == LoadSceneMode.Single)
{
// Destroy current scene objects before switching.
m_NetworkManager.SpawnManager.ServerDestroySpawnedSceneObjects();
// Preserve the objects that should not be destroyed during the scene event
MoveObjectsToDontDestroyOnLoad();
// Now Unload all currently additively loaded scenes
UnloadAdditivelyLoadedScenes();
}
// Now start loading the scene
AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
sceneLoad.completed += (AsyncOperation asyncOp2) => { OnSceneLoaded(sceneEventData.SceneEventId, sceneName); };
sceneEventProgress.SetSceneLoadOperation(sceneLoad);
// Notify the local server that a scene loading event has begun
OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = sceneLoad,
SceneEventType = sceneEventData.SceneEventType,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = sceneName,
ClientId = m_NetworkManager.ServerClientId
});
OnLoad?.Invoke(m_NetworkManager.ServerClientId, sceneName, sceneEventData.LoadSceneMode, sceneLoad);
//Return our scene progress instance
return sceneEventProgress.Status;
}
/// <summary>
/// Client Side:
/// Handles both forms of scene loading
/// </summary>
/// <param name="objectStream">Stream data associated with the event</param>
private void OnClientSceneLoadingEvent(uint sceneEventId)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
var sceneName = SceneNameFromHash(sceneEventData.SceneHash);
// Run scene validation before loading a scene
if (!ValidateSceneBeforeLoading(sceneEventData.SceneHash, sceneEventData.LoadSceneMode))
{
EndSceneEvent(sceneEventId);
return;
}
#if UNITY_INCLUDE_TESTS
if (m_IsRunningUnitTest)
{
// Send the loading message
OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = new AsyncOperation(),
SceneEventType = sceneEventData.SceneEventType,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = sceneName,
ClientId = m_NetworkManager.LocalClientId
});
// Only for testing
OnLoad?.Invoke(m_NetworkManager.ServerClientId, sceneName, sceneEventData.LoadSceneMode, new AsyncOperation());
// Unit tests must mirror the server's scenes loaded dictionary, otherwise this portion will fail
if (ScenesLoaded.ContainsKey(sceneEventData.SceneHandle))
{
OnClientLoadedScene(sceneEventId, ScenesLoaded[sceneEventData.SceneHandle]);
}
else
{
EndSceneEvent(sceneEventId);
throw new Exception($"Could not find the scene handle {sceneEventData.SceneHandle} for scene {sceneName} " +
$"during unit test. Did you forget to register this in the unit test?");
}
return;
}
#endif
if (sceneEventData.LoadSceneMode == LoadSceneMode.Single)
{
// Move ALL NetworkObjects to the temp scene
MoveObjectsToDontDestroyOnLoad();
// Now Unload all currently additively loaded scenes
UnloadAdditivelyLoadedScenes();
}
// The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned
// they need to be moved into the do not destroy temporary scene
// When it is set: Just before starting the asynchronous loading call
// When it is unset: After the scene has loaded, the PopulateScenePlacedObjects is called, and all NetworkObjects in the do
// not destroy temporary scene are moved into the active scene
if (sceneEventData.LoadSceneMode == LoadSceneMode.Single)
{
IsSpawnedObjectsPendingInDontDestroyOnLoad = true;
}
var sceneLoad = SceneManager.LoadSceneAsync(sceneName, sceneEventData.LoadSceneMode);
sceneLoad.completed += asyncOp2 => OnSceneLoaded(sceneEventId, sceneName);
OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = sceneLoad,
SceneEventType = sceneEventData.SceneEventType,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = sceneName,
ClientId = m_NetworkManager.LocalClientId
});
OnLoad?.Invoke(m_NetworkManager.LocalClientId, sceneName, sceneEventData.LoadSceneMode, sceneLoad);
}
/// <summary>
/// Client and Server:
/// Generic on scene loaded callback method to be called upon a scene loading
/// </summary>
private void OnSceneLoaded(uint sceneEventId, string sceneName)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
var nextScene = GetAndAddNewlyLoadedSceneByName(sceneName);
if (!nextScene.isLoaded || !nextScene.IsValid())
{
throw new Exception($"Failed to find valid scene internal Unity.Netcode for {nameof(GameObject)}s error!");
}
if (sceneEventData.LoadSceneMode == LoadSceneMode.Single)
{
SceneManager.SetActiveScene(nextScene);
}
//Get all NetworkObjects loaded by the scene
PopulateScenePlacedObjects(nextScene);
if (sceneEventData.LoadSceneMode == LoadSceneMode.Single)
{
// Move all objects to the new scene
MoveObjectsFromDontDestroyOnLoadToScene(nextScene);
}
// The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned
// they need to be moved into the do not destroy temporary scene
// When it is set: Just before starting the asynchronous loading call
// When it is unset: After the scene has loaded, the PopulateScenePlacedObjects is called, and all NetworkObjects in the do
// not destroy temporary scene are moved into the active scene
IsSpawnedObjectsPendingInDontDestroyOnLoad = false;
if (m_NetworkManager.IsServer)
{
OnServerLoadedScene(sceneEventId, nextScene);
}
else
{
// For the client, we make a server scene handle to client scene handle look up table
if (!ServerSceneHandleToClientSceneHandle.ContainsKey(sceneEventData.SceneHandle))
{
ServerSceneHandleToClientSceneHandle.Add(sceneEventData.SceneHandle, nextScene.handle);
}
else
{
// If the exact same handle exists then there are problems with using handles
throw new Exception($"Server Scene Handle ({sceneEventData.SceneHandle}) already exist! Happened during scene load of {nextScene.name} with Client Handle ({nextScene.handle})");
}
OnClientLoadedScene(sceneEventId, nextScene);
}
}
/// <summary>
/// Server side:
/// On scene loaded callback method invoked by OnSceneLoading only
/// </summary>
private void OnServerLoadedScene(uint sceneEventId, Scene scene)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
// Register in-scene placed NetworkObjects with spawn manager
foreach (var keyValuePairByGlobalObjectIdHash in ScenePlacedObjects)
{
foreach (var keyValuePairBySceneHandle in keyValuePairByGlobalObjectIdHash.Value)
{
if (!keyValuePairBySceneHandle.Value.IsPlayerObject)
{
m_NetworkManager.SpawnManager.SpawnNetworkObjectLocally(keyValuePairBySceneHandle.Value, m_NetworkManager.SpawnManager.GetNetworkObjectId(), true, false, null, true);
}
}
}
// Set the server's scene's handle so the client can build a look up table
sceneEventData.SceneHandle = scene.handle;
// Send all clients the scene load event
for (int j = 0; j < m_NetworkManager.ConnectedClientsList.Count; j++)
{
var clientId = m_NetworkManager.ConnectedClientsList[j].ClientId;
if (clientId != m_NetworkManager.ServerClientId)
{
sceneEventData.TargetClientId = clientId;
var message = new SceneEventMessage
{
EventData = sceneEventData
};
var size = m_NetworkManager.SendMessage(message, k_DeliveryType, clientId);
m_NetworkManager.NetworkMetrics.TrackSceneEventSent(clientId, (uint)sceneEventData.SceneEventType, scene.name, size);
}
}
s_IsSceneEventActive = false;
//First, notify local server that the scene was loaded
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = SceneEventType.LoadComplete,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = SceneNameFromHash(sceneEventData.SceneHash),
ClientId = m_NetworkManager.ServerClientId,
Scene = scene,
});
OnLoadComplete?.Invoke(m_NetworkManager.ServerClientId, SceneNameFromHash(sceneEventData.SceneHash), sceneEventData.LoadSceneMode);
//Second, set the server as having loaded for the associated SceneEventProgress
if (SceneEventProgressTracking.ContainsKey(sceneEventData.SceneEventProgressId))
{
SceneEventProgressTracking[sceneEventData.SceneEventProgressId].AddClientAsDone(m_NetworkManager.ServerClientId);
}
EndSceneEvent(sceneEventId);
}
/// <summary>
/// Client side:
/// On scene loaded callback method invoked by OnSceneLoading only
/// </summary>
private void OnClientLoadedScene(uint sceneEventId, Scene scene)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
sceneEventData.DeserializeScenePlacedObjects();
sceneEventData.SceneEventType = SceneEventType.LoadComplete;
SendSceneEventData(sceneEventId, new ulong[] { m_NetworkManager.ServerClientId });
s_IsSceneEventActive = false;
// Notify local client that the scene was loaded
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = SceneEventType.LoadComplete,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = SceneNameFromHash(sceneEventData.SceneHash),
ClientId = m_NetworkManager.LocalClientId,
Scene = scene,
});
OnLoadComplete?.Invoke(m_NetworkManager.LocalClientId, SceneNameFromHash(sceneEventData.SceneHash), sceneEventData.LoadSceneMode);
EndSceneEvent(sceneEventId);
}
/// <summary>
/// Server Side:
/// This is used for players that have just had their connection approved and will assure they are synchronized
/// properly if they are late joining
/// Note: We write out all of the scenes to be loaded first and then all of the NetworkObjects that need to be
/// synchronized.
/// </summary>
/// <param name="clientId">newly joined client identifier</param>
internal void SynchronizeNetworkObjects(ulong clientId)
{
// Update the clients
m_NetworkManager.SpawnManager.UpdateObservedNetworkObjects(clientId);
var sceneEventData = BeginSceneEvent();
sceneEventData.InitializeForSynch();
sceneEventData.TargetClientId = clientId;
sceneEventData.LoadSceneMode = ClientSynchronizationMode;
var activeScene = SceneManager.GetActiveScene();
sceneEventData.SceneEventType = SceneEventType.Synchronize;
// Organize how (and when) we serialize our NetworkObjects
for (int i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);
var sceneHash = SceneHashFromNameOrPath(scene.path);
// This would depend upon whether we are additive or not
// If we are the base scene, then we set the root scene index;
if (activeScene == scene)
{
if (!ValidateSceneBeforeLoading(sceneHash, sceneEventData.LoadSceneMode))
{
continue;
}
sceneEventData.SceneHash = sceneHash;
sceneEventData.SceneHandle = scene.handle;
}
else if (!ValidateSceneBeforeLoading(sceneHash, LoadSceneMode.Additive))
{
continue;
}
sceneEventData.AddSceneToSynchronize(sceneHash, scene.handle);
}
sceneEventData.AddSpawnedNetworkObjects();
var message = new SceneEventMessage
{
EventData = sceneEventData
};
var size = m_NetworkManager.SendMessage(message, k_DeliveryType, clientId);
m_NetworkManager.NetworkMetrics.TrackSceneEventSent(
clientId, (uint)sceneEventData.SceneEventType, "", size);
// Notify the local server that the client has been sent the synchronize event
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = sceneEventData.SceneEventType,
ClientId = clientId
});
OnSynchronize?.Invoke(clientId);
EndSceneEvent(sceneEventData.SceneEventId);
}
/// <summary>
/// This is called when the client receives the <see cref="SceneEventType.Synchronize"/> event
/// Note: This can recurse one additional time by the client if the current scene loaded by the client
/// is already loaded.
/// </summary>
private void OnClientBeginSync(uint sceneEventId)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
var sceneHash = sceneEventData.GetNextSceneSynchronizationHash();
var sceneHandle = sceneEventData.GetNextSceneSynchronizationHandle();
var sceneName = SceneNameFromHash(sceneHash);
var activeScene = SceneManager.GetActiveScene();
var loadSceneMode = sceneHash == sceneEventData.SceneHash ? sceneEventData.LoadSceneMode : LoadSceneMode.Additive;
// Always check to see if the scene needs to be validated
if (!ValidateSceneBeforeLoading(sceneHash, loadSceneMode))
{
EndSceneEvent(sceneEventId);
return;
}
// If this is the beginning of the synchronization event, then send client a notification that synchronization has begun
if (sceneHash == sceneEventData.SceneHash)
{
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = SceneEventType.Synchronize,
ClientId = m_NetworkManager.LocalClientId,
});
OnSynchronize?.Invoke(m_NetworkManager.LocalClientId);
// Clear the in-scene placed NetworkObjects when we load the first scene in our synchronization process
ScenePlacedObjects.Clear();
}
var shouldPassThrough = false;
var sceneLoad = (AsyncOperation)null;
// Check to see if the client already has loaded the scene to be loaded
if (sceneName == activeScene.name)
{
// If the client is already in the same scene, then pass through and
// don't try to reload it.
shouldPassThrough = true;
}
#if UNITY_INCLUDE_TESTS
if (m_IsRunningUnitTest)
{
// In unit tests, we don't allow clients to load additional scenes since
// MultiInstance unit tests share the same scene space.
shouldPassThrough = true;
sceneLoad = new AsyncOperation();
}
#endif
if (!shouldPassThrough)
{
// If not, then load the scene
sceneLoad = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
sceneLoad.completed += asyncOp2 => ClientLoadedSynchronization(sceneEventId, sceneHash, sceneHandle);
}
// Notify local client that a scene load has begun
OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = sceneLoad,
SceneEventType = SceneEventType.Load,
LoadSceneMode = loadSceneMode,
SceneName = sceneName,
ClientId = m_NetworkManager.LocalClientId,
});
OnLoad?.Invoke(m_NetworkManager.LocalClientId, sceneName, loadSceneMode, sceneLoad);
if (shouldPassThrough)
{
// If so, then pass through
ClientLoadedSynchronization(sceneEventId, sceneHash, sceneHandle);
}
}
/// <summary>
/// Once a scene is loaded ( or if it was already loaded) this gets called.
/// This handles all of the in-scene and dynamically spawned NetworkObject synchronization
/// </summary>
/// <param name="sceneIndex">Netcode scene index that was loaded</param>
private void ClientLoadedSynchronization(uint sceneEventId, uint sceneHash, int sceneHandle)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
var sceneName = SceneNameFromHash(sceneHash);
var nextScene = GetAndAddNewlyLoadedSceneByName(sceneName);
if (!nextScene.isLoaded || !nextScene.IsValid())
{
throw new Exception($"Failed to find valid scene internal Unity.Netcode for {nameof(GameObject)}s error!");
}
var loadSceneMode = (sceneHash == sceneEventData.SceneHash ? sceneEventData.LoadSceneMode : LoadSceneMode.Additive);
// For now, during a synchronization event, we will make the first scene the "base/master" scene that denotes a "complete scene switch"
if (loadSceneMode == LoadSceneMode.Single)
{
SceneManager.SetActiveScene(nextScene);
}
if (!ServerSceneHandleToClientSceneHandle.ContainsKey(sceneHandle))
{
ServerSceneHandleToClientSceneHandle.Add(sceneHandle, nextScene.handle);
}
else
{
// If the exact same handle exists then there are problems with using handles
throw new Exception($"Server Scene Handle ({sceneEventData.SceneHandle}) already exist! Happened during scene load of {nextScene.name} with Client Handle ({nextScene.handle})");
}
// Apply all in-scene placed NetworkObjects loaded by the scene
PopulateScenePlacedObjects(nextScene, false);
// Send notification back to server that we finished loading this scene
var responseSceneEventData = BeginSceneEvent();
responseSceneEventData.LoadSceneMode = loadSceneMode;
responseSceneEventData.SceneEventType = SceneEventType.LoadComplete;
responseSceneEventData.SceneHash = sceneHash;
var message = new SceneEventMessage
{
EventData = responseSceneEventData
};
var size = m_NetworkManager.SendMessage(message, k_DeliveryType, m_NetworkManager.ServerClientId);
m_NetworkManager.NetworkMetrics.TrackSceneEventSent(m_NetworkManager.ServerClientId, (uint)responseSceneEventData.SceneEventType, sceneName, size);
EndSceneEvent(responseSceneEventData.SceneEventId);
// Send notification to local client that the scene has finished loading
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = SceneEventType.LoadComplete,
LoadSceneMode = loadSceneMode,
SceneName = sceneName,
Scene = nextScene,
ClientId = m_NetworkManager.LocalClientId,
});
OnLoadComplete?.Invoke(m_NetworkManager.LocalClientId, sceneName, loadSceneMode);
// Check to see if we still have scenes to load and synchronize with
HandleClientSceneEvent(sceneEventId);
}
/// <summary>
/// Client Side:
/// Handles incoming Scene_Event messages for clients
/// </summary>
/// <param name="stream">data associated with the event</param>
private void HandleClientSceneEvent(uint sceneEventId)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
switch (sceneEventData.SceneEventType)
{
case SceneEventType.Load:
{
OnClientSceneLoadingEvent(sceneEventId);
break;
}
case SceneEventType.Unload:
{
OnClientUnloadScene(sceneEventId);
break;
}
case SceneEventType.Synchronize:
{
if (!sceneEventData.IsDoneWithSynchronization())
{
OnClientBeginSync(sceneEventId);
}
else
{
// Include anything in the DDOL scene
PopulateScenePlacedObjects(DontDestroyOnLoadScene, false);
// Synchronize the NetworkObjects for this scene
sceneEventData.SynchronizeSceneNetworkObjects(m_NetworkManager);
sceneEventData.SceneEventType = SceneEventType.SynchronizeComplete;
SendSceneEventData(sceneEventId, new ulong[] { m_NetworkManager.ServerClientId });
// All scenes are synchronized, let the server know we are done synchronizing
m_NetworkManager.IsConnectedClient = true;
// Client is now synchronized and fully "connected". This also means the client can send "RPCs" at this time
m_NetworkManager.InvokeOnClientConnectedCallback(m_NetworkManager.LocalClientId);
// Notify the client that they have finished synchronizing
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = sceneEventData.SceneEventType,
ClientId = m_NetworkManager.LocalClientId, // Client sent this to the server
});
OnSynchronizeComplete?.Invoke(m_NetworkManager.LocalClientId);
EndSceneEvent(sceneEventId);
}
break;
}
case SceneEventType.ReSynchronize:
{
// Notify the local client that they have been re-synchronized after being synchronized with an in progress game session
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = sceneEventData.SceneEventType,
ClientId = m_NetworkManager.ServerClientId, // Server sent this to client
});
EndSceneEvent(sceneEventId);
break;
}
case SceneEventType.LoadEventCompleted:
case SceneEventType.UnloadEventCompleted:
{
// Notify the local client that all clients have finished loading or unloading
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = sceneEventData.SceneEventType,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = SceneNameFromHash(sceneEventData.SceneHash),
ClientId = m_NetworkManager.ServerClientId,
ClientsThatCompleted = sceneEventData.ClientsCompleted,
ClientsThatTimedOut = sceneEventData.ClientsTimedOut,
});
if (sceneEventData.SceneEventType == SceneEventType.LoadEventCompleted)
{
OnLoadEventCompleted?.Invoke(SceneNameFromHash(sceneEventData.SceneHash), sceneEventData.LoadSceneMode, sceneEventData.ClientsCompleted, sceneEventData.ClientsTimedOut);
}
else
{
OnUnloadEventCompleted?.Invoke(SceneNameFromHash(sceneEventData.SceneHash), sceneEventData.LoadSceneMode, sceneEventData.ClientsCompleted, sceneEventData.ClientsTimedOut);
}
EndSceneEvent(sceneEventId);
break;
}
default:
{
Debug.LogWarning($"{sceneEventData.SceneEventType} is not currently supported!");
break;
}
}
}
/// <summary>
/// Server Side:
/// Handles incoming Scene_Event messages for host or server
/// </summary>
private void HandleServerSceneEvent(uint sceneEventId, ulong clientId)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
switch (sceneEventData.SceneEventType)
{
case SceneEventType.LoadComplete:
{
// Notify the local server that the client has finished loading a scene
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = sceneEventData.SceneEventType,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = SceneNameFromHash(sceneEventData.SceneHash),
ClientId = clientId
});
OnLoadComplete?.Invoke(clientId, SceneNameFromHash(sceneEventData.SceneHash), sceneEventData.LoadSceneMode);
if (SceneEventProgressTracking.ContainsKey(sceneEventData.SceneEventProgressId))
{
SceneEventProgressTracking[sceneEventData.SceneEventProgressId].AddClientAsDone(clientId);
}
EndSceneEvent(sceneEventId);
break;
}
case SceneEventType.UnloadComplete:
{
if (SceneEventProgressTracking.ContainsKey(sceneEventData.SceneEventProgressId))
{
SceneEventProgressTracking[sceneEventData.SceneEventProgressId].AddClientAsDone(clientId);
}
// Notify the local server that the client has finished unloading a scene
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = sceneEventData.SceneEventType,
LoadSceneMode = sceneEventData.LoadSceneMode,
SceneName = SceneNameFromHash(sceneEventData.SceneHash),
ClientId = clientId
});
OnUnloadComplete?.Invoke(clientId, SceneNameFromHash(sceneEventData.SceneHash));
EndSceneEvent(sceneEventId);
break;
}
case SceneEventType.SynchronizeComplete:
{
// Notify the local server that a client has finished synchronizing
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = sceneEventData.SceneEventType,
SceneName = string.Empty,
ClientId = clientId
});
OnSynchronizeComplete?.Invoke(clientId);
// We now can call the client connected callback on the server at this time
// This assures the client is fully synchronized with all loaded scenes and
// NetworkObjects
m_NetworkManager.InvokeOnClientConnectedCallback(clientId);
// TODO: This check and associated code can be removed once we determine all
// snapshot destroy messages are being updated until the server receives ACKs
if (sceneEventData.ClientNeedsReSynchronization() && !DisableReSynchronization)
{
sceneEventData.SceneEventType = SceneEventType.ReSynchronize;
SendSceneEventData(sceneEventId, new ulong[] { clientId });
OnSceneEvent?.Invoke(new SceneEvent()
{
SceneEventType = sceneEventData.SceneEventType,
SceneName = string.Empty,
ClientId = clientId
});
}
EndSceneEvent(sceneEventId);
break;
}
default:
{
Debug.LogWarning($"{sceneEventData.SceneEventType} is not currently supported!");
break;
}
}
}
/// <summary>
/// Both Client and Server: Incoming scene event entry point
/// </summary>
/// <param name="clientId">client who sent the scene event</param>
/// <param name="reader">data associated with the scene event</param>
internal void HandleSceneEvent(ulong clientId, FastBufferReader reader)
{
if (m_NetworkManager != null)
{
var sceneEventData = BeginSceneEvent();
sceneEventData.Deserialize(reader);
m_NetworkManager.NetworkMetrics.TrackSceneEventReceived(
clientId, (uint)sceneEventData.SceneEventType, SceneNameFromHash(sceneEventData.SceneHash), reader.Length);
if (sceneEventData.IsSceneEventClientSide())
{
HandleClientSceneEvent(sceneEventData.SceneEventId);
}
else
{
HandleServerSceneEvent(sceneEventData.SceneEventId, clientId);
}
}
else
{
Debug.LogError($"{nameof(NetworkSceneManager.HandleSceneEvent)} was invoked but {nameof(NetworkManager)} reference was null!");
}
}
/// <summary>
/// Moves all NetworkObjects that don't have the <see cref="NetworkObject.DestroyWithScene"/> set to
/// the "Do not destroy on load" scene.
/// </summary>
private void MoveObjectsToDontDestroyOnLoad()
{
// Move ALL NetworkObjects to the temp scene
var objectsToKeep = new HashSet<NetworkObject>(m_NetworkManager.SpawnManager.SpawnedObjectsList);
foreach (var sobj in objectsToKeep)
{
if (!sobj.DestroyWithScene || (sobj.IsSceneObject != null && sobj.IsSceneObject.Value && sobj.gameObject.scene == DontDestroyOnLoadScene))
{
// Only move objects with no parent as child objects will follow
if (sobj.gameObject.transform.parent == null)
{
UnityEngine.Object.DontDestroyOnLoad(sobj.gameObject);
// Since we are doing a scene transition, disable the GameObject until the next scene is loaded
sobj.gameObject.SetActive(false);
}
}
else if (m_NetworkManager.IsServer)
{
sobj.Despawn();
}
}
}
/// <summary>
/// Should be invoked on both the client and server side after:
/// -- A new scene has been loaded
/// -- Before any "DontDestroyOnLoad" NetworkObjects have been added back into the scene.
/// Added the ability to choose not to clear the scene placed objects for additive scene loading.
/// We organize our ScenePlacedObjects by:
/// [GlobalObjectIdHash][SceneHandle][NetworkObject]
/// Using the local scene relative Scene.handle as a sub-key to the root dictionary allows us to
/// distinguish between duplicate in-scene placed NetworkObjects
/// </summary>
private void PopulateScenePlacedObjects(Scene sceneToFilterBy, bool clearScenePlacedObjects = true)
{
if (clearScenePlacedObjects)
{
ScenePlacedObjects.Clear();
}
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
// Just add every NetworkObject found that isn't already in the list
// With additive scenes, we can have multiple in-scene placed NetworkObjects with the same GlobalObjectIdHash value
// During Client Side Synchronization: We add them on a FIFO basis, for each scene loaded without clearing, and then
// at the end of scene loading we use this list to soft synchronize all in-scene placed NetworkObjects
foreach (var networkObjectInstance in networkObjects)
{
// We check to make sure the NetworkManager instance is the same one to be "MultiInstanceHelpers" compatible and filter the list on a per scene basis (additive scenes)
if (networkObjectInstance.IsSceneObject == null && networkObjectInstance.NetworkManager == m_NetworkManager && networkObjectInstance.gameObject.scene == sceneToFilterBy &&
networkObjectInstance.gameObject.scene.handle == sceneToFilterBy.handle)
{
if (!ScenePlacedObjects.ContainsKey(networkObjectInstance.GlobalObjectIdHash))
{
ScenePlacedObjects.Add(networkObjectInstance.GlobalObjectIdHash, new Dictionary<int, NetworkObject>());
}
if (!ScenePlacedObjects[networkObjectInstance.GlobalObjectIdHash].ContainsKey(networkObjectInstance.gameObject.scene.handle))
{
ScenePlacedObjects[networkObjectInstance.GlobalObjectIdHash].Add(networkObjectInstance.gameObject.scene.handle, networkObjectInstance);
}
else
{
var exitingEntryName = ScenePlacedObjects[networkObjectInstance.GlobalObjectIdHash][networkObjectInstance.gameObject.scene.handle] != null ?
ScenePlacedObjects[networkObjectInstance.GlobalObjectIdHash][networkObjectInstance.gameObject.scene.handle].name : "Null Entry";
throw new Exception($"{networkObjectInstance.name} tried to registered with {nameof(ScenePlacedObjects)} which already contains " +
$"the same {nameof(NetworkObject.GlobalObjectIdHash)} value {networkObjectInstance.GlobalObjectIdHash} for {exitingEntryName}!");
}
}
}
}
/// <summary>
/// Moves all spawned NetworkObjects (from do not destroy on load) to the scene specified
/// </summary>
/// <param name="scene">scene to move the NetworkObjects to</param>
private void MoveObjectsFromDontDestroyOnLoadToScene(Scene scene)
{
// Move ALL NetworkObjects to the temp scene
var objectsToKeep = m_NetworkManager.SpawnManager.SpawnedObjectsList;
foreach (var sobj in objectsToKeep)
{
if (sobj.gameObject.scene == DontDestroyOnLoadScene && (sobj.IsSceneObject == null || sobj.IsSceneObject.Value))
{
continue;
}
// Only move objects with no parent as child objects will follow
if (sobj.gameObject.transform.parent == null)
{
// set it back to active at this point
sobj.gameObject.SetActive(true);
SceneManager.MoveGameObjectToScene(sobj.gameObject, scene);
}
}
}
}
}