Skip to content

Commit 5ea87e0

Browse files
fix: notify networkmanager and networkobject not allowed (#1777)
* fix notify the user when they try to add a NetworkObject to a GameObject that already has a NetworkManager assigned to it. * fix extending the INetworkManagerHelper interface. * update and style Renamed the method being invoked to NotifyUserNetworkObjectRemoved in order to be more explicit in what was happening. Updated comments * Update CHANGELOG.md * test and update adding a test to validate that checking for a NetworkObject, removing the NetworkObject, and notifying the user that a NetworkManager cannot exist if a NetworkObject resides on the GameObject. This also now checks children containing a NetworkObject. It does not need to check for a parent as this is checked after to invoking NetworkManagerCheckForParent. * update making a method to return the error message. adjusting the error message to include children in the description. adding comments for clarity. * update Adding a validation check to assure the NetworkObject has been removed. * style whitespace fix
1 parent 419df98 commit 5ea87e0

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Additional documentation and release notes are available at [Multiplayer Documen
1111
### Fixed
1212
- Fixed issue when spawning new player if an already existing player exists it does not remove IsPlayer from the previous player (#1779)
1313

14+
## [Unreleased]
15+
### Added
16+
### Changed
17+
### Fixed
18+
- Fixed lack of notification that NetworkManager and NetworkObject cannot be added to the same GameObject with in-editor notifications (#1777)
19+
1420
## [1.0.0-pre.6] - 2022-03-02
1521

1622
### Added

com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,63 @@ private static void EditorApplication_playModeStateChanged(PlayModeStateChange p
4545
}
4646
}
4747

48+
/// <summary>
49+
/// Invoked only when the hierarchy changes
50+
/// </summary>
4851
private static void EditorApplication_hierarchyChanged()
4952
{
5053
var allNetworkManagers = Resources.FindObjectsOfTypeAll<NetworkManager>();
5154
foreach (var networkManager in allNetworkManagers)
5255
{
53-
networkManager.NetworkManagerCheckForParent();
56+
if (!networkManager.NetworkManagerCheckForParent())
57+
{
58+
Singleton.CheckAndNotifyUserNetworkObjectRemoved(networkManager);
59+
}
60+
}
61+
}
62+
63+
/// <summary>
64+
/// Handles notifying users that they cannot add a NetworkObject component
65+
/// to a GameObject that also has a NetworkManager component. The NetworkObject
66+
/// will always be removed.
67+
/// GameObject + NetworkObject then NetworkManager = NetworkObject removed
68+
/// GameObject + NetworkManager then NetworkObject = NetworkObject removed
69+
/// Note: Since this is always invoked after <see cref="NetworkManagerCheckForParent"/>
70+
/// we do not need to check for parent when searching for a NetworkObject component
71+
/// </summary>
72+
public void CheckAndNotifyUserNetworkObjectRemoved(NetworkManager networkManager, bool editorTest = false)
73+
{
74+
// Check for any NetworkObject at the same gameObject relative layer
75+
var networkObject = networkManager.gameObject.GetComponent<NetworkObject>();
76+
77+
if (networkObject == null)
78+
{
79+
// if none is found, check to see if any children have a NetworkObject
80+
networkObject = networkManager.gameObject.GetComponentInChildren<NetworkObject>();
81+
if (networkObject == null)
82+
{
83+
return;
84+
}
5485
}
86+
87+
if (!EditorApplication.isUpdating)
88+
{
89+
Object.DestroyImmediate(networkObject);
90+
91+
if (!EditorApplication.isPlaying && !editorTest)
92+
{
93+
EditorUtility.DisplayDialog($"Removing {nameof(NetworkObject)}", NetworkManagerAndNetworkObjectNotAllowedMessage(), "OK");
94+
}
95+
else
96+
{
97+
Debug.LogError(NetworkManagerAndNetworkObjectNotAllowedMessage());
98+
}
99+
}
100+
}
101+
102+
public string NetworkManagerAndNetworkObjectNotAllowedMessage()
103+
{
104+
return $"A {nameof(GameObject)} cannot have both a {nameof(NetworkManager)} and {nameof(NetworkObject)} assigned to it or any children under it.";
55105
}
56106

57107
/// <summary>

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,7 @@ static internal string GenerateNestedNetworkManagerMessage(Transform transform)
10141014
internal interface INetworkManagerHelper
10151015
{
10161016
bool NotifyUserOfNestedNetworkManager(NetworkManager networkManager, bool ignoreNetworkManagerCache = false, bool editorTest = false);
1017+
void CheckAndNotifyUserNetworkObjectRemoved(NetworkManager networkManager, bool editorTest = false);
10171018
}
10181019
#endif
10191020

com.unity.netcode.gameobjects/Tests/Editor/NetworkManagerConfigurationTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,49 @@ public void NestedNetworkManagerCheck()
3434
// Clean up
3535
Object.DestroyImmediate(parent);
3636
}
37+
38+
public enum NetworkObjectPlacement
39+
{
40+
Root, // Added to the same root GameObject
41+
Child // Added to a child GameObject
42+
}
43+
44+
[Test]
45+
public void NetworkObjectNotAllowed([Values] NetworkObjectPlacement networkObjectPlacement)
46+
{
47+
var gameObject = new GameObject(nameof(NetworkManager));
48+
var targetforNetworkObject = gameObject;
49+
50+
if (networkObjectPlacement == NetworkObjectPlacement.Child)
51+
{
52+
var childGameObject = new GameObject($"{nameof(NetworkManager)}-Child");
53+
childGameObject.transform.parent = targetforNetworkObject.transform;
54+
targetforNetworkObject = childGameObject;
55+
}
56+
57+
var networkManager = gameObject.AddComponent<NetworkManager>();
58+
59+
// Trap for the error message generated when a NetworkObject is discovered on the same GameObject or any children under it
60+
LogAssert.Expect(LogType.Error, NetworkManagerHelper.Singleton.NetworkManagerAndNetworkObjectNotAllowedMessage());
61+
62+
// Add the NetworkObject
63+
var networkObject = targetforNetworkObject.AddComponent<NetworkObject>();
64+
65+
// Since this is an in-editor test, we must force this invocation
66+
NetworkManagerHelper.Singleton.CheckAndNotifyUserNetworkObjectRemoved(networkManager, true);
67+
68+
// Validate that the NetworkObject has been removed
69+
if (networkObjectPlacement == NetworkObjectPlacement.Root)
70+
{
71+
Assert.IsNull(networkManager.gameObject.GetComponent<NetworkObject>(), $"There is still a {nameof(NetworkObject)} on {nameof(NetworkManager)}'s GameObject!");
72+
}
73+
else
74+
{
75+
Assert.IsNull(networkManager.gameObject.GetComponentInChildren<NetworkObject>(), $"There is still a {nameof(NetworkObject)} on {nameof(NetworkManager)}'s child GameObject!");
76+
}
77+
78+
// Clean up
79+
Object.DestroyImmediate(gameObject);
80+
}
3781
}
3882
}

0 commit comments

Comments
 (0)