-
Notifications
You must be signed in to change notification settings - Fork 450
fix: NetworkManager always sent to the DDOL and cannot be nested #1484
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
NoelStephensUnity
merged 25 commits into
develop
from
fix/nested-networkmanager-ddol-editor-check
Dec 9, 2021
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6083f04
refactor
NoelStephensUnity 6057d41
update
NoelStephensUnity 7468b7a
changelog update
NoelStephensUnity d2d2bdf
refactor
NoelStephensUnity a11dbb9
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 7fcea47
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity da1827e
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 04eac17
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity c40ff2b
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 58120ad
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 6122eca
refactor
NoelStephensUnity a41b7e3
test
NoelStephensUnity e5826e7
style
NoelStephensUnity ce126af
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 3224049
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity c5249d7
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 79bff1b
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 152c181
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 3c828a4
fix
NoelStephensUnity d4b2625
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity a3e2f53
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 882da30
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 7e08ed3
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity bdff640
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity 1dabae7
Merge branch 'develop' into fix/nested-networkmanager-ddol-editor-check
NoelStephensUnity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
108 changes: 108 additions & 0 deletions
108
com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs
This file contains hidden or 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,108 @@ | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEditor; | ||
|
||
namespace Unity.Netcode.Editor | ||
{ | ||
#if UNITY_EDITOR | ||
/// <summary> | ||
/// Specialized editor specific NetworkManager code | ||
/// </summary> | ||
public class NetworkManagerHelper : NetworkManager.INetworkManagerHelper | ||
{ | ||
internal static NetworkManagerHelper Singleton; | ||
|
||
// This is primarily to handle multiInstance scenarios where more than 1 NetworkManager could exist | ||
private static Dictionary<NetworkManager, Transform> s_LastKnownNetworkManagerParents = new Dictionary<NetworkManager, Transform>(); | ||
|
||
/// <summary> | ||
/// Initializes the singleton instance and registers for: | ||
/// Hierarchy changed notification: to notify the user when they nest a NetworkManager | ||
/// Play mode state change notification: to capture when entering or exiting play mode (currently only exiting) | ||
/// </summary> | ||
[InitializeOnLoadMethod] | ||
private static void InitializeOnload() | ||
{ | ||
Singleton = new NetworkManagerHelper(); | ||
NetworkManager.NetworkManagerHelper = Singleton; | ||
|
||
EditorApplication.playModeStateChanged -= EditorApplication_playModeStateChanged; | ||
EditorApplication.hierarchyChanged -= EditorApplication_hierarchyChanged; | ||
|
||
EditorApplication.playModeStateChanged += EditorApplication_playModeStateChanged; | ||
EditorApplication.hierarchyChanged += EditorApplication_hierarchyChanged; | ||
} | ||
|
||
private static void EditorApplication_playModeStateChanged(PlayModeStateChange playModeStateChange) | ||
{ | ||
switch (playModeStateChange) | ||
{ | ||
case PlayModeStateChange.ExitingEditMode: | ||
{ | ||
s_LastKnownNetworkManagerParents.Clear(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private static void EditorApplication_hierarchyChanged() | ||
{ | ||
var allNetworkManagers = Resources.FindObjectsOfTypeAll<NetworkManager>(); | ||
foreach (var networkManager in allNetworkManagers) | ||
{ | ||
networkManager.NetworkManagerCheckForParent(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Handles notifying the user, via display dialog window, that they have nested a NetworkManager. | ||
/// When in edit mode it provides the option to automatically fix the issue | ||
/// When in play mode it just notifies the user when entering play mode as well as when the user | ||
/// tries to start a network session while a NetworkManager is still nested. | ||
/// </summary> | ||
public bool NotifyUserOfNestedNetworkManager(NetworkManager networkManager, bool ignoreNetworkManagerCache = false, bool editorTest = false) | ||
{ | ||
var gameObject = networkManager.gameObject; | ||
var transform = networkManager.transform; | ||
var isParented = transform.root != transform; | ||
|
||
var message = NetworkManager.GenerateNestedNetworkManagerMessage(transform); | ||
if (s_LastKnownNetworkManagerParents.ContainsKey(networkManager) && !ignoreNetworkManagerCache) | ||
{ | ||
// If we have already notified the user, then don't notify them again | ||
if (s_LastKnownNetworkManagerParents[networkManager] == transform.root) | ||
{ | ||
return isParented; | ||
} | ||
else // If we are no longer a child, then we can remove ourself from this list | ||
if (transform.root == gameObject.transform) | ||
{ | ||
s_LastKnownNetworkManagerParents.Remove(networkManager); | ||
} | ||
} | ||
if (!EditorApplication.isUpdating && isParented) | ||
{ | ||
if (!EditorApplication.isPlaying && !editorTest) | ||
{ | ||
message += $"Click 'Auto-Fix' to automatically remove it from {transform.root.gameObject.name} or 'Manual-Fix' to fix it yourself in the hierarchy view."; | ||
if (EditorUtility.DisplayDialog("Invalid Nested NetworkManager", message, "Auto-Fix", "Manual-Fix")) | ||
{ | ||
transform.parent = null; | ||
isParented = false; | ||
} | ||
} | ||
else | ||
{ | ||
Debug.LogError(message); | ||
} | ||
|
||
if (!s_LastKnownNetworkManagerParents.ContainsKey(networkManager) && isParented) | ||
{ | ||
s_LastKnownNetworkManagerParents.Add(networkManager, networkManager.transform.root); | ||
} | ||
} | ||
return isParented; | ||
} | ||
} | ||
#endif | ||
} |
11 changes: 11 additions & 0 deletions
11
com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
38 changes: 38 additions & 0 deletions
38
com.unity.netcode.gameobjects/Tests/Editor/NetworkManagerConfigurationTests.cs
This file contains hidden or 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,38 @@ | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
using Unity.Netcode.Editor; | ||
using UnityEngine.TestTools; | ||
|
||
namespace Unity.Netcode.EditorTests | ||
{ | ||
public class NetworkManagerConfigurationTests | ||
{ | ||
/// <summary> | ||
/// Does a simple check to make sure the nested network manager will | ||
/// notify the user when in the editor. This is just a unit test to | ||
/// validate this is functioning | ||
/// </summary> | ||
[Test] | ||
public void NestedNetworkManagerCheck() | ||
{ | ||
var parent = new GameObject("ParentObject"); | ||
var networkManagerObject = new GameObject(nameof(NestedNetworkManagerCheck)); | ||
var networkManager = networkManagerObject.AddComponent<NetworkManager>(); | ||
|
||
// Make our NetworkManager's GameObject nested | ||
networkManagerObject.transform.parent = parent.transform; | ||
|
||
// Pre-generate the error message we are expecting to see | ||
var messageToCheck = NetworkManager.GenerateNestedNetworkManagerMessage(networkManagerObject.transform); | ||
|
||
// Trap for the nested NetworkManager exception | ||
LogAssert.Expect(LogType.Error, messageToCheck); | ||
|
||
// Since this is an in-editor test, we must force this invocation | ||
NetworkManagerHelper.Singleton.NotifyUserOfNestedNetworkManager(networkManager, false, true); | ||
|
||
// Clean up | ||
Object.DestroyImmediate(parent); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
com.unity.netcode.gameobjects/Tests/Editor/NetworkManagerConfigurationTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Poor NetworkManager is so large already....could this live in its own file? Preferably in the Editor folder?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will see what I can do about removing the editor related code from NetworkManager into some other class within the NGO editor assembly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I see the update, but originally imagined like all this code from line 1705 to 1785 would be hoisted to
NetworkManagerHelper.cs