internal static void HandleSwitchScene(ulong clientId, Stream stream) { #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleSwitchScene.Begin(); #endif using (var reader = PooledNetworkReader.Get(stream)) { uint sceneIndex = reader.ReadUInt32Packed(); Guid switchSceneGuid = new Guid(reader.ReadByteArray()); var objectBuffer = new NetworkBuffer(); objectBuffer.CopyUnreadFrom(stream); objectBuffer.Position = 0; NetworkSceneManager.OnSceneSwitch(sceneIndex, switchSceneGuid, objectBuffer); } #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleSwitchScene.End(); #endif }
internal static void HandleConnectionApproved(ulong clientId, Stream stream, float receiveTime) { #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleConnectionApproved.Begin(); #endif using (var reader = PooledNetworkReader.Get(stream)) { NetworkManager.Singleton.LocalClientId = reader.ReadUInt64Packed(); uint sceneIndex = 0; Guid sceneSwitchProgressGuid = new Guid(); if (NetworkManager.Singleton.NetworkConfig.EnableSceneManagement) { sceneIndex = reader.ReadUInt32Packed(); sceneSwitchProgressGuid = new Guid(reader.ReadByteArray()); } bool sceneSwitch = NetworkManager.Singleton.NetworkConfig.EnableSceneManagement && NetworkSceneManager.HasSceneMismatch(sceneIndex); float netTime = reader.ReadSinglePacked(); NetworkManager.Singleton.UpdateNetworkTime(clientId, netTime, receiveTime, true); NetworkManager.Singleton.ConnectedClients.Add(NetworkManager.Singleton.LocalClientId, new NetworkClient { ClientId = NetworkManager.Singleton.LocalClientId }); void DelayedSpawnAction(Stream continuationStream) { using (var continuationReader = PooledNetworkReader.Get(continuationStream)) { if (!NetworkManager.Singleton.NetworkConfig.EnableSceneManagement || NetworkManager.Singleton.NetworkConfig.UsePrefabSync) { NetworkSpawnManager.DestroySceneObjects(); } else { NetworkSpawnManager.ClientCollectSoftSyncSceneObjectSweep(null); } uint objectCount = continuationReader.ReadUInt32Packed(); for (int i = 0; i < objectCount; i++) { bool isPlayerObject = continuationReader.ReadBool(); ulong networkId = continuationReader.ReadUInt64Packed(); ulong ownerId = continuationReader.ReadUInt64Packed(); bool hasParent = continuationReader.ReadBool(); ulong?parentNetworkId = null; if (hasParent) { parentNetworkId = continuationReader.ReadUInt64Packed(); } ulong prefabHash; ulong instanceId; bool softSync; if (!NetworkManager.Singleton.NetworkConfig.EnableSceneManagement || NetworkManager.Singleton.NetworkConfig.UsePrefabSync) { softSync = false; instanceId = 0; prefabHash = continuationReader.ReadUInt64Packed(); } else { softSync = continuationReader.ReadBool(); if (softSync) { instanceId = continuationReader.ReadUInt64Packed(); prefabHash = 0; } else { prefabHash = continuationReader.ReadUInt64Packed(); instanceId = 0; } } Vector3? pos = null; Quaternion?rot = null; if (continuationReader.ReadBool()) { pos = new Vector3(continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked()); rot = Quaternion.Euler(continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked(), continuationReader.ReadSinglePacked()); } var networkObject = NetworkSpawnManager.CreateLocalNetworkObject(softSync, instanceId, prefabHash, parentNetworkId, pos, rot); NetworkSpawnManager.SpawnNetworkObjectLocally(networkObject, networkId, softSync, isPlayerObject, ownerId, continuationStream, false, 0, true, false); Queue <BufferManager.BufferedMessage> bufferQueue = BufferManager.ConsumeBuffersForNetworkId(networkId); // Apply buffered messages if (bufferQueue != null) { while (bufferQueue.Count > 0) { BufferManager.BufferedMessage message = bufferQueue.Dequeue(); NetworkManager.Singleton.HandleIncomingData(message.SenderClientId, message.NetworkChannel, new ArraySegment <byte>(message.NetworkBuffer.GetBuffer(), (int)message.NetworkBuffer.Position, (int)message.NetworkBuffer.Length), message.ReceiveTime, false); BufferManager.RecycleConsumedBufferedMessage(message); } } } NetworkSpawnManager.CleanDiffedSceneObjects(); NetworkManager.Singleton.IsConnectedClient = true; NetworkManager.Singleton.InvokeOnClientConnectedCallback(NetworkManager.Singleton.LocalClientId); } } if (sceneSwitch) { UnityAction <Scene, Scene> onSceneLoaded = null; var continuationBuffer = new NetworkBuffer(); continuationBuffer.CopyUnreadFrom(stream); continuationBuffer.Position = 0; void OnSceneLoadComplete() { SceneManager.activeSceneChanged -= onSceneLoaded; NetworkSceneManager.IsSpawnedObjectsPendingInDontDestroyOnLoad = false; DelayedSpawnAction(continuationBuffer); } onSceneLoaded = (oldScene, newScene) => { OnSceneLoadComplete(); }; SceneManager.activeSceneChanged += onSceneLoaded; NetworkSceneManager.OnFirstSceneSwitchSync(sceneIndex, sceneSwitchProgressGuid); } else { DelayedSpawnAction(stream); } } #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleConnectionApproved.End(); #endif }
public void HandleConnectionApproved(ulong clientId, Stream stream, float receiveTime) { #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleConnectionApproved.Begin(); #endif using (var reader = PooledNetworkReader.Get(stream)) { NetworkManager.LocalClientId = reader.ReadUInt64Packed(); uint sceneIndex = 0; var sceneSwitchProgressGuid = new Guid(); if (NetworkManager.NetworkConfig.EnableSceneManagement) { sceneIndex = reader.ReadUInt32Packed(); sceneSwitchProgressGuid = new Guid(reader.ReadByteArray()); } bool sceneSwitch = NetworkManager.NetworkConfig.EnableSceneManagement && NetworkManager.SceneManager.HasSceneMismatch(sceneIndex); float netTime = reader.ReadSinglePacked(); NetworkManager.UpdateNetworkTime(clientId, netTime, receiveTime, true); NetworkManager.ConnectedClients.Add(NetworkManager.LocalClientId, new NetworkClient { ClientId = NetworkManager.LocalClientId }); void DelayedSpawnAction(Stream continuationStream) { using (var continuationReader = PooledNetworkReader.Get(continuationStream)) { if (!NetworkManager.NetworkConfig.EnableSceneManagement) { NetworkManager.SpawnManager.DestroySceneObjects(); } else { NetworkManager.SpawnManager.ClientCollectSoftSyncSceneObjectSweep(null); } var objectCount = continuationReader.ReadUInt32Packed(); for (int i = 0; i < objectCount; i++) { NetworkObject.DeserializeSceneObject(continuationStream as NetworkBuffer, continuationReader, m_NetworkManager); } NetworkManager.SpawnManager.CleanDiffedSceneObjects(); NetworkManager.IsConnectedClient = true; NetworkManager.InvokeOnClientConnectedCallback(NetworkManager.LocalClientId); } } if (sceneSwitch) { UnityAction <Scene, Scene> onSceneLoaded = null; var continuationBuffer = new NetworkBuffer(); continuationBuffer.CopyUnreadFrom(stream); continuationBuffer.Position = 0; void OnSceneLoadComplete() { SceneManager.activeSceneChanged -= onSceneLoaded; NetworkSceneManager.IsSpawnedObjectsPendingInDontDestroyOnLoad = false; DelayedSpawnAction(continuationBuffer); } onSceneLoaded = (oldScene, newScene) => { OnSceneLoadComplete(); }; SceneManager.activeSceneChanged += onSceneLoaded; m_NetworkManager.SceneManager.OnFirstSceneSwitchSync(sceneIndex, sceneSwitchProgressGuid); } else { DelayedSpawnAction(stream); } } #if DEVELOPMENT_BUILD || UNITY_EDITOR s_HandleConnectionApproved.End(); #endif }