Skip to content
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

fix: in-sceneobject NetworkObject update in editor tool (up-port) #3092

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
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

- Fixed issue with the in-scene network prefab instance update menu tool where it was not properly updating scenes when invoked on the root prefab instance. (#3092)
- Fixed issue where applying the position and/or rotation to the `NetworkManager.ConnectionApprovalResponse` when connection approval and auto-spawn player prefab were enabled would not apply the position and/or rotation when the player prefab was instantiated. (#3078)
- Fixed issue where `NetworkObject.SpawnWithObservers` was not being honored when spawning the player prefab. (#3077)
- Fixed issue with the client count not being correct on the host or server side when a client disconnects itself from a session. (#3075)
Expand Down
10 changes: 3 additions & 7 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ internal void RefreshAllPrefabInstances()
}

// Handle updating the currently active scene
var networkObjects = FindObjectsByType<NetworkObject>(FindObjectsInactive.Include, FindObjectsSortMode.None);
foreach (var networkObject in networkObjects)
{
networkObject.OnValidate();
}
NetworkObjectRefreshTool.ProcessActiveScene();

// Refresh all build settings scenes
Expand All @@ -130,14 +125,14 @@ internal void RefreshAllPrefabInstances()
continue;
}
// Add the scene to be processed
NetworkObjectRefreshTool.ProcessScene(editorScene.path, false);
NetworkObjectRefreshTool.ProcessScene(editorScene.path, true);
}

// Process all added scenes
NetworkObjectRefreshTool.ProcessScenes();
}

private void OnValidate()
internal void OnValidate()
{
// do NOT regenerate GlobalObjectIdHash for NetworkPrefabs while Editor is in PlayMode
if (EditorApplication.isPlaying && !string.IsNullOrEmpty(gameObject.scene.name))
Expand Down Expand Up @@ -229,6 +224,7 @@ private void CheckForInScenePlaced()
if (sourceAsset != null && sourceAsset.GlobalObjectIdHash != 0 && InScenePlacedSourceGlobalObjectIdHash != sourceAsset.GlobalObjectIdHash)
{
InScenePlacedSourceGlobalObjectIdHash = sourceAsset.GlobalObjectIdHash;
EditorUtility.SetDirty(this);
}
IsSceneObject = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
Expand All @@ -21,6 +23,28 @@ internal class NetworkObjectRefreshTool

internal static Action AllScenesProcessed;

internal static NetworkObject PrefabNetworkObject;

internal static void LogInfo(string msg, bool append = false)
{
if (!append)
{
s_Log.AppendLine(msg);
}
else
{
s_Log.Append(msg);
}
}

internal static void FlushLog()
{
Debug.Log(s_Log.ToString());
s_Log.Clear();
}

private static StringBuilder s_Log = new StringBuilder();

internal static void ProcessScene(string scenePath, bool processScenes = true)
{
if (!s_ScenesToUpdate.Contains(scenePath))
Expand All @@ -29,14 +53,18 @@ internal static void ProcessScene(string scenePath, bool processScenes = true)
{
EditorSceneManager.sceneOpened += EditorSceneManager_sceneOpened;
EditorSceneManager.sceneSaved += EditorSceneManager_sceneSaved;
s_Log.Clear();
LogInfo("NetworkObject Refresh Scenes to Process:");
}
LogInfo($"[{scenePath}]", true);
s_ScenesToUpdate.Add(scenePath);
}
s_ProcessScenes = processScenes;
}

internal static void ProcessActiveScene()
{
FlushLog();
var activeScene = SceneManager.GetActiveScene();
if (s_ScenesToUpdate.Contains(activeScene.path) && s_ProcessScenes)
{
Expand All @@ -54,10 +82,12 @@ internal static void ProcessScenes()
}
else
{
s_ProcessScenes = false;
s_CloseScenes = false;
EditorSceneManager.sceneSaved -= EditorSceneManager_sceneSaved;
EditorSceneManager.sceneOpened -= EditorSceneManager_sceneOpened;
AllScenesProcessed?.Invoke();
FlushLog();
}
}

Expand All @@ -68,9 +98,8 @@ private static void FinishedProcessingScene(Scene scene, bool refreshed = false)
// Provide a log of all scenes that were modified to the user
if (refreshed)
{
Debug.Log($"Refreshed and saved updates to scene: {scene.name}");
LogInfo($"Refreshed and saved updates to scene: {scene.name}");
}
s_ProcessScenes = false;
s_ScenesToUpdate.Remove(scene.path);

if (scene != SceneManager.GetActiveScene())
Expand All @@ -88,24 +117,41 @@ private static void EditorSceneManager_sceneSaved(Scene scene)

private static void SceneOpened(Scene scene)
{
LogInfo($"Processing scene {scene.name}:");
if (s_ScenesToUpdate.Contains(scene.path))
{
if (s_ProcessScenes)
{
if (!EditorSceneManager.MarkSceneDirty(scene))
{
Debug.Log($"Scene {scene.name} did not get marked as dirty!");
FinishedProcessingScene(scene);
}
else
var prefabInstances = PrefabUtility.FindAllInstancesOfPrefab(PrefabNetworkObject.gameObject);

if (prefabInstances.Length > 0)
{
EditorSceneManager.SaveScene(scene);
var instancesSceneLoadedSpecific = prefabInstances.Where((c) => c.scene == scene).ToList();

if (instancesSceneLoadedSpecific.Count > 0)
{
foreach (var prefabInstance in instancesSceneLoadedSpecific)
{
prefabInstance.GetComponent<NetworkObject>().OnValidate();
}

if (!EditorSceneManager.MarkSceneDirty(scene))
{
LogInfo($"Scene {scene.name} did not get marked as dirty!");
FinishedProcessingScene(scene);
}
else
{
LogInfo($"Changes detected and applied!");
EditorSceneManager.SaveScene(scene);
}
return;
}
}
}
else
{
FinishedProcessingScene(scene);
}

LogInfo($"No changes required.");
FinishedProcessingScene(scene);
}
}

Expand Down
Loading