Skip to content

fix: NetworkObject parenting support in scene transitioning #1148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 7, 2021
Merged
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 @@ -994,7 +994,7 @@ private void OnSceneLoaded(string sceneName)
if (SceneEventData.LoadSceneMode == LoadSceneMode.Single)
{
// Move all objects to the new scene
MoveObjectsToScene(nextScene);
MoveObjectsFromDontDestroyOnLoadToScene(nextScene);
}

// The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned
Expand Down Expand Up @@ -1580,15 +1580,15 @@ private void MoveObjectsToDontDestroyOnLoad()

foreach (var sobj in objectsToKeep)
{
//In case an object has been set as a child of another object it has to be removed from the parent in order to be moved from one scene to another.
if (sobj.gameObject.transform.parent != null)
{
sobj.gameObject.transform.parent = null;
}

if (!sobj.DestroyWithScene || (sobj.IsSceneObject != null && sobj.IsSceneObject.Value && sobj.gameObject.scene == DontDestroyOnLoadScene))
{
UnityEngine.Object.DontDestroyOnLoad(sobj.gameObject);
// 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)
{
Expand Down Expand Up @@ -1650,25 +1650,25 @@ private void PopulateScenePlacedObjects(Scene sceneToFilterBy, bool clearScenePl
/// 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 MoveObjectsToScene(Scene scene)
private void MoveObjectsFromDontDestroyOnLoadToScene(Scene scene)
{
// Move ALL NetworkObjects to the temp scene
var objectsToKeep = m_NetworkManager.SpawnManager.SpawnedObjectsList;

foreach (var sobj in objectsToKeep)
{
//In case an object has been set as a child of another object it has to be removed from the parent in order to be moved from one scene to another.
if (sobj.gameObject.transform.parent != null)
{
sobj.gameObject.transform.parent = null;
}

if (sobj.gameObject.scene == DontDestroyOnLoadScene && (sobj.IsSceneObject == null || sobj.IsSceneObject.Value))
{
continue;
}

SceneManager.MoveGameObjectToScene(sobj.gameObject, scene);
// 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);
}
}
}
}
Expand Down