Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public class NetworkSceneManager
/// </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.
Comment on lines +99 to +100
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the previous PR #1030, @andrews-unity and @LukeStampfli approved this PR and I also do approve this PR too.
please feel free to approve your own PR @NoelStephensUnity and merge it in :)

#if UNITY_EDITOR || DEVELOPMENT_BUILD
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"/>
Expand Down Expand Up @@ -538,11 +544,21 @@ private void OnClientUnloadScene()
$"because the client scene handle {sceneHandle} was not found in ScenesLoaded!");
}
s_IsSceneEventActive = true;

var sceneUnload = SceneManager.UnloadSceneAsync(ScenesLoaded[sceneHandle]);

var sceneUnload = (AsyncOperation)null;
#if UNITY_EDITOR || DEVELOPMENT_BUILD
if (m_IsRunningUnitTest)
{
sceneUnload = new AsyncOperation();
}
else
{
sceneUnload = SceneManager.UnloadSceneAsync(ScenesLoaded[sceneHandle]);
sceneUnload.completed += asyncOp2 => OnSceneUnloaded();
}
#else
sceneUnload = SceneManager.UnloadSceneAsync(ScenesLoaded[sceneHandle]);
sceneUnload.completed += asyncOp2 => OnSceneUnloaded();

#endif
ScenesLoaded.Remove(sceneHandle);

// Remove our server to scene handle lookup
Expand All @@ -559,6 +575,12 @@ private void OnClientUnloadScene()
});


#if UNITY_EDITOR || DEVELOPMENT_BUILD
if (m_IsRunningUnitTest)
{
OnSceneUnloaded();
}
#endif
}

/// <summary>
Expand Down Expand Up @@ -704,6 +726,33 @@ private void OnClientSceneLoadingEvent(Stream objectStream)
return;
}

#if UNITY_EDITOR || DEVELOPMENT_BUILD
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
});

// Unit tests must mirror the server's scenes loaded dictionary, otherwise this portion will fail
if (ScenesLoaded.ContainsKey(SceneEventData.SceneHandle))
{
OnClientLoadedScene(ScenesLoaded[SceneEventData.SceneHandle]);
}
else
{
throw new Exception($"Could not find the scene handle {SceneEventData.SceneHandle} for scene {GetSceneNameFromNetcodeSceneIndex(SceneEventData.SceneIndex)} " +
$"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
Expand Down Expand Up @@ -973,25 +1022,44 @@ private void OnClientBeginSync()
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 (sceneName == activeScene.name)
{
// If not, then load the scene
var sceneLoad = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);

// Notify local client that a scene load has begun
OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = sceneLoad,
SceneEventType = SceneEventData.SceneEventTypes.S2C_Load,
LoadSceneMode = loadSceneMode,
SceneName = sceneName,
ClientId = m_NetworkManager.LocalClientId,
});
// If the client is already in the same scene, then pass through and
// don't try to reload it.
shouldPassThrough = true;
}

#if UNITY_EDITOR || DEVELOPMENT_BUILD
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(sceneIndex, sceneHandle);
}
else

// Notify local client that a scene load has begun
OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = sceneLoad,
SceneEventType = SceneEventData.SceneEventTypes.S2C_Load,
LoadSceneMode = loadSceneMode,
SceneName = sceneName,
ClientId = m_NetworkManager.LocalClientId,
});

if (shouldPassThrough)
{
// If so, then pass through
ClientLoadedSynchronization(sceneIndex, sceneHandle);
Expand Down

This file was deleted.

15 changes: 9 additions & 6 deletions testproject/Assets/Tests/Manual/Scripts/NetworkPrefabPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,18 @@ private void OnSceneEvent(SceneEvent sceneEvent)
/// </summary>
private void OnUnloadScene()
{
if (IsServer)
if (NetworkObject != null && NetworkManager != null)
{
StopCoroutine(SpawnObjects());
}
if (IsServer)
{
StopCoroutine(SpawnObjects());
}

// De-register the custom prefab handler
DeregisterCustomPrefabHandler();
// De-register the custom prefab handler
DeregisterCustomPrefabHandler();

CleanNetworkObjects();
CleanNetworkObjects();
}
}


Expand Down
Loading