Skip to content

fix: in-sceneobject NetworkObject update in editor tool #3084

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
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 @@ -17,6 +17,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. (#3084)
- Fixed issue where `NetworkAnimator` would send updates to non-observer clients. (#3058)
- Fixed issue where an exception could occur when receiving a universal RPC for a `NetworkObject` that has been despawned. (#3055)
- Fixed issue where setting a prefab hash value during connection approval but not having a player prefab assigned could cause an exception when spawning a player. (#3046)
Expand Down
11 changes: 4 additions & 7 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,14 @@ public uint PrefabIdHash
internal void RefreshAllPrefabInstances()
{
var instanceGlobalId = GlobalObjectId.GetGlobalObjectIdSlow(this);
NetworkObjectRefreshTool.PrefabNetworkObject = this;
if (!PrefabUtility.IsPartOfAnyPrefab(this) || instanceGlobalId.identifierType != k_ImportedAssetObjectType)
{
EditorUtility.DisplayDialog("Network Prefab Assets Only", "This action can only be performed on a network prefab asset.", "Ok");
return;
}

// 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 @@ -98,14 +94,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 @@ -197,6 +193,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