Skip to content

Commit a4edf18

Browse files
TwoTenPvP0xFA11
andauthored
test: Nested NetworkObjects in prefabs throws a warning [MTT-2976] (#1969)
Co-authored-by: Fatih Mar <[email protected]>
1 parent e58355d commit a4edf18

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Added
1212

13+
- Added test to ensure a warning occurs when nesting NetworkObjects in a NetworkPrefab (#1969)
1314
- Added `NetworkManager.RemoveNetworkPrefab(...)` to remove a prefab from the prefabs list (#1950)
1415

1516
### Changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ public Func<ConnectionApprovalRequest, ConnectionApprovalResponse> ConnectionApp
424424
internal static event Action OnSingletonReady;
425425

426426
#if UNITY_EDITOR
427-
private void OnValidate()
427+
internal void OnValidate()
428428
{
429429
if (NetworkConfig == null)
430430
{

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using NUnit.Framework;
22
using UnityEngine;
33
using Unity.Netcode.Editor;
4+
using UnityEditor.SceneManagement;
5+
using UnityEngine.SceneManagement;
46
using UnityEngine.TestTools;
57

68
namespace Unity.Netcode.EditorTests
@@ -78,5 +80,37 @@ public void NetworkObjectNotAllowed([Values] NetworkObjectPlacement networkObjec
7880
// Clean up
7981
Object.DestroyImmediate(gameObject);
8082
}
83+
84+
[Test]
85+
public void NestedNetworkObjectPrefabCheck()
86+
{
87+
// Setup
88+
var networkManagerObject = new GameObject(nameof(NestedNetworkObjectPrefabCheck));
89+
var networkManager = networkManagerObject.AddComponent<NetworkManager>();
90+
networkManager.NetworkConfig = new NetworkConfig();
91+
92+
var parent = new GameObject("Parent").AddComponent<NetworkObject>();
93+
var child = new GameObject("Child").AddComponent<NetworkObject>();
94+
95+
// Set parent
96+
child.transform.SetParent(parent.transform);
97+
98+
// Make it a prefab, warning only applies to prefabs
99+
networkManager.AddNetworkPrefab(parent.gameObject);
100+
101+
// Mark scene as dirty to ensure OnValidate actually runs
102+
EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
103+
104+
// Force OnValidate
105+
networkManager.OnValidate();
106+
107+
// Expect a warning
108+
LogAssert.Expect(LogType.Warning, $"[Netcode] {NetworkManager.PrefabDebugHelper(networkManager.NetworkConfig.NetworkPrefabs[0])} has child {nameof(NetworkObject)}(s) but they will not be spawned across the network (unsupported {nameof(NetworkPrefab)} setup)");
109+
110+
// Clean up
111+
Object.DestroyImmediate(networkManagerObject);
112+
Object.DestroyImmediate(parent);
113+
114+
}
81115
}
82116
}

0 commit comments

Comments
 (0)