/// <summary> /// Reset replication, instruct all clients to load the new scene, actually /// load the new scene on the server and finally create a new GameMode instance. /// </summary> /// <param name="sceneName"></param> public void LoadScene(string sceneName) { // Cleanup if (gameMode != null) { gameMode.StartToLeaveMap(); } IsLoadingScene = true; ++loadSceneGeneration; numLoadScenePlayerAcks = 0; loadSceneName = sceneName; server.ReplicaManager.Reset(); if (sceneHandle.IsValid()) { Addressables.UnloadSceneAsync(sceneHandle); } // Instruct clients var bs = new BitStream(); bs.Write((byte)MessageId.LoadScene); bs.Write(sceneName); bs.Write(loadSceneGeneration); server.NetworkInterface.BroadcastBitStream(bs, PacketReliability.ReliableSequenced); // Disable ReplicaViews during level load foreach (var connection in server.connections) { var replicaView = server.ReplicaManager.GetReplicaView(connection); if (replicaView == null) { continue; } replicaView.IsLoadingLevel = true; } // Load new map Debug.Log($"[Server] Loading level {sceneName}"); #if UNITY_EDITOR if (SceneManager.GetSceneByName(sceneName).isLoaded) { SceneManager.UnloadScene(sceneName); } #endif sceneHandle = Addressables.LoadSceneAsync(sceneName, LoadSceneMode.Additive); sceneHandle.Completed += ctx => { IsLoadingScene = false; gameMode = CreateGameModeForScene(sceneName); Assert.IsNotNull(gameMode); Debug.Log($"[Server] New GameMode <i>{gameMode}</i>"); }; }
protected virtual ApprovalResult OnApproveConnection(BitStream bs) { return(new ApprovalResult() { Approved = true }); }
void OnNewIncomingConnection(Connection connection) { Debug.Log($"[Server] <b>New connection</b> <i>{connection}</i>"); // Send load scene packet if we loaded one previously if (loadSceneName != null) { var bs2 = new BitStream(); bs2.Write((byte)MessageId.LoadScene); bs2.Write(loadSceneName); bs2.Write(loadSceneGeneration); server.NetworkInterface.SendBitStream(bs2, PacketReliability.ReliableSequenced, connection); } var newPC = CreatePlayerController(connection); world.playerControllers.Add(newPC); var replicaView = CreateReplicaView(connection); server.ReplicaManager.AddReplicaView(replicaView); if (gameMode != null) // null if there's no ongoing match { gameMode.HandleNewPlayer(newPC); } }
void OnPossessPawn(BitStream bs) { var replicaId = bs.ReadReplicaId(); var pawnIdx = bs.ReadByte(); currentReplicaPossess = replicaId; pawnIdxToPossess = pawnIdx; }
IEnumerator Foo() { yield return(new WaitForSeconds(1)); var bs = new BitStream(); bs.Write((byte)MessageId.LoadSceneDone); bs.Write(currentLoadedSceneGeneration); Client.networkInterface.Send(bs, PacketReliability.ReliableUnordered); }
void OnLoadScene(BitStream bs) { var sceneName = bs.ReadString(); var generation = bs.ReadByte(); if (currentLoadedSceneGeneration != generation) { currentLoadedSceneGeneration = generation; World.StartCoroutine(LoadScene(sceneName)); } }
void OnLoadSceneDone(Connection connection, BitStream bs) { var generation = bs.ReadByte(); if (generation != loadSceneGeneration) { return; } Debug.Log("[Server] On load scene done: <i>" + connection + "</i> (generation=" + generation + ")"); ++numLoadScenePlayerAcks; // var replicaView = server.ReplicaManager.GetReplicaView(connection); if (replicaView != null) { replicaView.IsLoadingLevel = false; server.ReplicaManager.ForceReplicaViewRefresh(replicaView); } }
public void Deserialize(BitStream bs) { Amount = bs.ReadByte(); }
public void Serialize(BitStream bs) { bs.Write(Amount); }