This commit is contained in:
2025-06-16 15:14:23 +02:00
committed by devbeni
parent 60fe4620ff
commit 4ff561284f
3174 changed files with 428263 additions and 0 deletions

View File

@ -0,0 +1,187 @@
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Mirror.Examples.AdditiveLevels
{
[AddComponentMenu("")]
public class AdditiveLevelsNetworkManager : NetworkManager
{
public static new AdditiveLevelsNetworkManager singleton => (AdditiveLevelsNetworkManager)NetworkManager.singleton;
[Header("Additive Scenes - First is start scene")]
[Scene, Tooltip("Add additive scenes here.\nFirst entry will be players' start scene")]
public string[] additiveScenes;
[Header("Fade Control - See child FadeCanvas")]
[Tooltip("Reference to FadeInOut script on child FadeCanvas")]
public FadeInOut fadeInOut;
// This is set true after server loads all subscene instances
bool subscenesLoaded;
// This is managed in LoadAdditive, UnloadAdditive, and checked in OnClientSceneChanged
bool isInTransition;
#region Scene Management
/// <summary>
/// Called on the server when a scene is completed loaded, when the scene load was initiated by the server with ServerChangeScene().
/// </summary>
/// <param name="sceneName">The name of the new scene.</param>
public override void OnServerSceneChanged(string sceneName)
{
// This fires after server fully changes scenes, e.g. offline to online
// If server has just loaded the Container (online) scene, load the subscenes on server
if (sceneName == onlineScene)
StartCoroutine(ServerLoadSubScenes());
}
IEnumerator ServerLoadSubScenes()
{
foreach (string additiveScene in additiveScenes)
yield return SceneManager.LoadSceneAsync(additiveScene, new LoadSceneParameters
{
loadSceneMode = LoadSceneMode.Additive,
localPhysicsMode = LocalPhysicsMode.Physics3D // change this to .Physics2D for a 2D game
});
subscenesLoaded = true;
}
/// <summary>
/// Called from ClientChangeScene immediately before SceneManager.LoadSceneAsync is executed
/// <para>This allows client to do work / cleanup / prep before the scene changes.</para>
/// </summary>
/// <param name="sceneName">Name of the scene that's about to be loaded</param>
/// <param name="sceneOperation">Scene operation that's about to happen</param>
/// <param name="customHandling">true to indicate that scene loading will be handled through overrides</param>
public override void OnClientChangeScene(string sceneName, SceneOperation sceneOperation, bool customHandling)
{
//Debug.Log($"{System.DateTime.Now:HH:mm:ss:fff} OnClientChangeScene {sceneName} {sceneOperation}");
if (sceneOperation == SceneOperation.UnloadAdditive)
StartCoroutine(UnloadAdditive(sceneName));
if (sceneOperation == SceneOperation.LoadAdditive)
StartCoroutine(LoadAdditive(sceneName));
}
IEnumerator LoadAdditive(string sceneName)
{
isInTransition = true;
// This will return immediately if already faded in
// e.g. by UnloadAdditive or by default startup state
yield return fadeInOut.FadeIn();
// host client is on server...don't load the additive scene again
if (mode == NetworkManagerMode.ClientOnly)
{
// Start loading the additive subscene
loadingSceneAsync = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
while (loadingSceneAsync != null && !loadingSceneAsync.isDone)
yield return null;
}
// Reset these to false when ready to proceed
NetworkClient.isLoadingScene = false;
isInTransition = false;
OnClientSceneChanged();
// Reveal the new scene content.
yield return fadeInOut.FadeOut();
}
IEnumerator UnloadAdditive(string sceneName)
{
isInTransition = true;
// This will return immediately if already faded in
// e.g. by LoadAdditive above or by default startup state.
yield return fadeInOut.FadeIn();
// host client is on server...don't unload the additive scene here.
if (mode == NetworkManagerMode.ClientOnly)
{
yield return SceneManager.UnloadSceneAsync(sceneName);
yield return Resources.UnloadUnusedAssets();
}
// Reset these to false when ready to proceed
NetworkClient.isLoadingScene = false;
isInTransition = false;
OnClientSceneChanged();
// There is no call to FadeOut here on purpose.
// Expectation is that a LoadAdditive or full scene change
// will follow that will call FadeOut after that scene loads.
}
/// <summary>
/// Called on clients when a scene has completed loaded, when the scene load was initiated by the server.
/// <para>Scene changes can cause player objects to be destroyed. The default implementation of OnClientSceneChanged in the NetworkManager is to add a player object for the connection if no player object exists.</para>
/// </summary>
/// <param name="conn">The network connection that the scene change message arrived on.</param>
public override void OnClientSceneChanged()
{
// Only call the base method if not in a transition.
// This will be called from LoadAdditive / UnloadAdditive after setting isInTransition to false
// but will also be called first by Mirror when the scene loading finishes.
if (!isInTransition)
base.OnClientSceneChanged();
}
#endregion
#region Server System Callbacks
/// <summary>
/// Called on the server when a client is ready.
/// <para>The default implementation of this function calls NetworkServer.SetClientReady() to continue the network setup process.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public override void OnServerReady(NetworkConnectionToClient conn)
{
// This fires from a Ready message client sends to server after loading the online scene
base.OnServerReady(conn);
if (conn.identity == null)
StartCoroutine(AddPlayerDelayed(conn));
}
// This delay is mostly for the host player that loads too fast for the
// server to have subscenes async loaded from OnServerSceneChanged ahead of it.
IEnumerator AddPlayerDelayed(NetworkConnectionToClient conn)
{
// Wait for server to async load all subscenes for game instances
while (!subscenesLoaded)
yield return null;
// Send Scene msg to client telling it to load the first additive scene
conn.Send(new SceneMessage { sceneName = additiveScenes[0], sceneOperation = SceneOperation.LoadAdditive, customHandling = true });
// We have Network Start Positions in first additive scene...pick one
Transform start = GetStartPosition();
// Instantiate player as child of start position - this will place it in the additive scene
// This also lets player object "inherit" pos and rot from start position transform
GameObject player = Instantiate(playerPrefab, start);
// now set parent null to get it out from under the Start Position object
player.transform.SetParent(null);
// Wait for end of frame before adding the player to ensure Scene Message goes first
yield return new WaitForEndOfFrame();
// Finally spawn the player object for this connection
NetworkServer.AddPlayerForConnection(conn, player);
}
#endregion
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 01eafa8309a0894479f0f87ae1a9c30f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/AdditiveLevels/Scripts/AdditiveLevelsNetworkManager.cs
uploadId: 736421

View File

@ -0,0 +1,77 @@
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace Mirror.Examples.AdditiveLevels
{
public class FadeInOut : MonoBehaviour
{
[Header("Components")]
[SerializeField] Image panelImage;
[Header("Settings")]
[SerializeField, Range(1, 10)]
[Tooltip("Time in seconds to fade in")]
byte fadeInTime = 2;
[SerializeField, Range(1, 10)]
[Tooltip("Time in seconds to fade out")]
byte fadeOutTime = 2;
bool isFading;
void OnValidate()
{
if (panelImage == null)
panelImage = GetComponentInChildren<Image>();
fadeInTime = (byte)Mathf.Max(fadeInTime, 1);
fadeOutTime = (byte)Mathf.Max(fadeOutTime, 1);
}
public float GetFadeInTime() => fadeInTime + Time.fixedDeltaTime;
public IEnumerator FadeIn()
{
//Debug.Log($"FadeIn {isFading}");
yield return FadeImage(0f, 1f, fadeInTime);
}
public float GetFadeOutTime() => fadeOutTime + Time.fixedDeltaTime;
public IEnumerator FadeOut()
{
//Debug.Log($"FadeOut {isFading}");
yield return FadeImage(1f, 0f, fadeOutTime);
}
private IEnumerator FadeImage(float startAlpha, float endAlpha, float duration)
{
if (panelImage == null) yield break;
if (isFading) yield break;
// Short circuit if the alpha is already at endAlpha
Color color = panelImage.color;
if (Mathf.Approximately(color.a, endAlpha)) yield break;
isFading = true;
float elapsedTime = 0f;
float fixedDeltaTime = Time.fixedDeltaTime;
while (elapsedTime < duration)
{
elapsedTime += fixedDeltaTime;
float alpha = Mathf.Lerp(startAlpha, endAlpha, elapsedTime / duration);
panelImage.color = new Color(color.r, color.g, color.b, alpha);
yield return new WaitForFixedUpdate();
}
// Ensure the final alpha value is set
panelImage.color = new Color(color.r, color.g, color.b, endAlpha);
isFading = false;
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 363a8867bb9c7b845a73233566df8c1e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/AdditiveLevels/Scripts/FadeInOut.cs
uploadId: 736421

View File

@ -0,0 +1,21 @@
using UnityEngine;
namespace Mirror.Examples.AdditiveLevels
{
// This script is attached to portal labels to keep them facing the camera
public class LookAtMainCamera : MonoBehaviour
{
// This will be enabled by Portal script in OnStartClient
void OnValidate()
{
this.enabled = false;
}
// LateUpdate so that all camera updates are finished.
[ClientCallback]
void LateUpdate()
{
transform.forward = Camera.main.transform.forward;
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: cc58300ee45438a418d9e32957fdc0c0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/AdditiveLevels/Scripts/LookAtMainCamera.cs
uploadId: 736421

View File

@ -0,0 +1,104 @@
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Mirror.Examples.AdditiveLevels
{
public class Portal : NetworkBehaviour
{
[Scene, Tooltip("Which scene to send player from here")]
public string destinationScene;
[Tooltip("Where to spawn player in Destination Scene")]
public Vector3 startPosition;
[Tooltip("Reference to child TextMesh label")]
public TextMesh label; // don't depend on TMPro. 2019 errors.
[SyncVar(hook = nameof(OnLabelTextChanged))]
public string labelText;
public void OnLabelTextChanged(string _, string newValue)
{
label.text = labelText;
}
public override void OnStartServer()
{
labelText = Path.GetFileNameWithoutExtension(destinationScene).Replace("MirrorAdditiveLevels", "");
// Simple Regex to insert spaces before capitals, numbers
labelText = Regex.Replace(labelText, @"\B[A-Z0-9]+", " $0");
}
public override void OnStartClient()
{
if (label.TryGetComponent(out LookAtMainCamera lookAtMainCamera))
lookAtMainCamera.enabled = true;
}
// Note that I have created layers called Player(6) and Portal(7) and set them
// up in the Physics collision matrix so only Player collides with Portal.
void OnTriggerEnter(Collider other)
{
if (!(other is CapsuleCollider)) return; // ignore CharacterController colliders
//Debug.Log($"Portal.OnTriggerEnter {other}");
// tag check in case you didn't set up the layers and matrix as noted above
if (!other.CompareTag("Player")) return;
// applies to host client on server and remote clients
if (other.TryGetComponent(out Common.Controllers.Player.PlayerControllerBase playerController))
playerController.enabled = false;
if (isServer)
StartCoroutine(SendPlayerToNewScene(other.gameObject));
}
[ServerCallback]
IEnumerator SendPlayerToNewScene(GameObject player)
{
if (!player.TryGetComponent(out NetworkIdentity identity)) yield break;
NetworkConnectionToClient conn = identity.connectionToClient;
if (conn == null) yield break;
// Tell client to unload previous subscene with custom handling (see NetworkManager::OnClientChangeScene).
conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true });
// wait for fader to complete.
yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetFadeInTime());
// Remove player after fader has completed
NetworkServer.RemovePlayerForConnection(conn, RemovePlayerOptions.Unspawn);
// yield a frame allowing interest management to update
// and all spawned objects to be destroyed on client
yield return null;
// reposition player on server and client
player.transform.position = startPosition;
// Rotate player to face center of scene
// Player is 2m tall with pivot at 0,1,0 so we need to look at
// 1m height to not tilt the player down to look at origin
player.transform.LookAt(Vector3.up);
// Move player to new subscene.
SceneManager.MoveGameObjectToScene(player, SceneManager.GetSceneByPath(destinationScene));
// Tell client to load the new subscene with custom handling (see NetworkManager::OnClientChangeScene).
conn.Send(new SceneMessage { sceneName = destinationScene, sceneOperation = SceneOperation.LoadAdditive, customHandling = true });
// Player will be spawned after destination scene is loaded
NetworkServer.AddPlayerForConnection(conn, player);
// host client playerController would have been disabled by OnTriggerEnter above
// Remote client players are respawned with playerController already enabled
if (NetworkClient.localPlayer != null && NetworkClient.localPlayer.TryGetComponent(out Common.Controllers.Player.PlayerControllerBase playerController))
playerController.enabled = true;
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 0e680878006965146a8f9d85834c4d1c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 129321
packageName: Mirror
packageVersion: 96.0.1
assetPath: Assets/Mirror/Examples/AdditiveLevels/Scripts/Portal.cs
uploadId: 736421