-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split apart the NetworkObjectRefreshTool from NetworkObject. Made some updates that don't require any form of editor application update. Added script in NetworkObject.RefreshAllPrefabInstances context menu method that handles refreshing the currently active scene and all enabled scenes in the build list.
- Loading branch information
1 parent
570f3ba
commit 906fad3
Showing
5 changed files
with
213 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
com.unity.netcode.gameobjects/Runtime/Core/NetworkObjectRefreshTool.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#if UNITY_EDITOR | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using UnityEditor.SceneManagement; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace Unity.Netcode | ||
{ | ||
/// <summary> | ||
/// This is a helper tool to update all in-scene placed instances of a prefab that | ||
/// originally did not have a NetworkObject component but one was added to the prefab | ||
/// later. | ||
/// </summary> | ||
internal class NetworkObjectRefreshTool | ||
{ | ||
private static List<string> s_ScenesToUpdate = new List<string>(); | ||
private static bool s_ProcessScenes; | ||
private static bool s_CloseScenes; | ||
|
||
internal static void ProcessScene(string scenePath, bool processScenes = true) | ||
{ | ||
if (!s_ScenesToUpdate.Contains(scenePath)) | ||
{ | ||
if (s_ScenesToUpdate.Count == 0) | ||
{ | ||
EditorSceneManager.sceneOpened += EditorSceneManager_sceneOpened; | ||
EditorSceneManager.sceneSaved += EditorSceneManager_sceneSaved; | ||
} | ||
s_ScenesToUpdate.Add(scenePath); | ||
} | ||
s_ProcessScenes = processScenes; | ||
} | ||
|
||
internal static void ProcessActiveScene() | ||
{ | ||
var activeScene = SceneManager.GetActiveScene(); | ||
if (s_ScenesToUpdate.Contains(activeScene.path) && s_ProcessScenes) | ||
{ | ||
SceneOpened(activeScene); | ||
} | ||
} | ||
|
||
internal static void ProcessScenes() | ||
{ | ||
if (s_ScenesToUpdate.Count != 0) | ||
{ | ||
s_CloseScenes = true; | ||
var scenePath = s_ScenesToUpdate.First(); | ||
EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive); | ||
} | ||
else | ||
{ | ||
s_CloseScenes = false; | ||
EditorSceneManager.sceneSaved -= EditorSceneManager_sceneSaved; | ||
EditorSceneManager.sceneOpened -= EditorSceneManager_sceneOpened; | ||
} | ||
} | ||
|
||
private static void FinishedProcessingScene(Scene scene, bool refreshed = false) | ||
{ | ||
if (s_ScenesToUpdate.Contains(scene.path)) | ||
{ | ||
// Provide a log of all scenes that were modified to the user | ||
if (refreshed) | ||
{ | ||
Debug.Log($"Refreshed and saved updates to scene: {scene.name}"); | ||
} | ||
s_ProcessScenes = false; | ||
s_ScenesToUpdate.Remove(scene.path); | ||
|
||
if (scene != SceneManager.GetActiveScene()) | ||
{ | ||
EditorSceneManager.CloseScene(scene, s_CloseScenes); | ||
} | ||
ProcessScenes(); | ||
} | ||
} | ||
|
||
private static void EditorSceneManager_sceneSaved(Scene scene) | ||
{ | ||
FinishedProcessingScene(scene, true); | ||
} | ||
|
||
private static void SceneOpened(Scene scene) | ||
{ | ||
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 | ||
{ | ||
EditorSceneManager.SaveScene(scene); | ||
} | ||
} | ||
else | ||
{ | ||
FinishedProcessingScene(scene); | ||
} | ||
} | ||
} | ||
|
||
private static void EditorSceneManager_sceneOpened(Scene scene, OpenSceneMode mode) | ||
{ | ||
SceneOpened(scene); | ||
} | ||
} | ||
} | ||
#endif // UNITY_EDITOR |
11 changes: 11 additions & 0 deletions
11
com.unity.netcode.gameobjects/Runtime/Core/NetworkObjectRefreshTool.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
testproject/Assets/Tests/Manual/SceneTransitioningAdditive/MyNetworkObject.prefab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
%YAML 1.1 | ||
%TAG !u! tag:unity3d.com,2011: | ||
--- !u!1 &1445163809825623502 | ||
GameObject: | ||
m_ObjectHideFlags: 0 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInstance: {fileID: 0} | ||
m_PrefabAsset: {fileID: 0} | ||
serializedVersion: 6 | ||
m_Component: | ||
- component: {fileID: 4895178628090191772} | ||
- component: {fileID: 7314688500250972154} | ||
m_Layer: 0 | ||
m_Name: MyNetworkObject | ||
m_TagString: Untagged | ||
m_Icon: {fileID: 0} | ||
m_NavMeshLayer: 0 | ||
m_StaticEditorFlags: 0 | ||
m_IsActive: 1 | ||
--- !u!4 &4895178628090191772 | ||
Transform: | ||
m_ObjectHideFlags: 0 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInstance: {fileID: 0} | ||
m_PrefabAsset: {fileID: 0} | ||
m_GameObject: {fileID: 1445163809825623502} | ||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} | ||
m_LocalPosition: {x: 0, y: 0, z: 0} | ||
m_LocalScale: {x: 1, y: 1, z: 1} | ||
m_ConstrainProportionsScale: 0 | ||
m_Children: [] | ||
m_Father: {fileID: 0} | ||
m_RootOrder: 0 | ||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} | ||
--- !u!114 &7314688500250972154 | ||
MonoBehaviour: | ||
m_ObjectHideFlags: 0 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInstance: {fileID: 0} | ||
m_PrefabAsset: {fileID: 0} | ||
m_GameObject: {fileID: 1445163809825623502} | ||
m_Enabled: 1 | ||
m_EditorHideFlags: 0 | ||
m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} | ||
m_Name: | ||
m_EditorClassIdentifier: | ||
GlobalObjectIdHash: 0 | ||
AlwaysReplicateAsRoot: 0 | ||
SynchronizeTransform: 1 | ||
ActiveSceneSynchronization: 0 | ||
SceneMigrationSynchronization: 1 | ||
SpawnWithObservers: 1 | ||
DontDestroyWithOwner: 0 | ||
AutoObjectParentSync: 1 |
7 changes: 7 additions & 0 deletions
7
testproject/Assets/Tests/Manual/SceneTransitioningAdditive/MyNetworkObject.prefab.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.