Skip to content

Commit dc96c01

Browse files
test - refactor
Adding a bit more coverage to the `ValidatedDisableddNetworkBehaviourWarning` test so it validates that when either a child GameObject is set inactive or the NetworkBehaviiour itself is disabled the warning message is generated and the network prefab still spawns and synchronizes.
1 parent f43da8a commit dc96c01

File tree

1 file changed

+62
-19
lines changed

1 file changed

+62
-19
lines changed

com.unity.netcode.gameobjects/Tests/Runtime/NetworkBehaviourGenericTests.cs

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ internal class NetworkBehaviourGenericTests : NetcodeIntegrationTest
1616

1717
private bool m_AllowServerToStart;
1818

19+
private GameObject m_PrefabToSpawn;
20+
1921
protected override bool CanStartServerAndClients()
2022
{
2123
return m_AllowServerToStart;
@@ -32,47 +34,88 @@ public override void OnNetworkDespawn()
3234
}
3335
}
3436

37+
protected override void OnServerAndClientsCreated()
38+
{
39+
m_PrefabToSpawn = CreateNetworkObjectPrefab("TestPrefab");
40+
41+
var childObject = new GameObject
42+
{
43+
name = "ChildObject"
44+
};
45+
childObject.transform.parent = m_PrefabToSpawn.transform;
46+
childObject.AddComponent<NetworkTransform>();
47+
base.OnServerAndClientsCreated();
48+
}
49+
3550
protected override IEnumerator OnSetup()
3651
{
3752
m_AllowServerToStart = false;
3853
return base.OnSetup();
3954
}
4055

56+
57+
protected override void OnNewClientCreated(NetworkManager networkManager)
58+
{
59+
networkManager.NetworkConfig.Prefabs.Add(new NetworkPrefab()
60+
{
61+
Prefab = m_PrefabToSpawn,
62+
});
63+
base.OnNewClientCreated(networkManager);
64+
}
65+
66+
4167
/// <summary>
42-
/// This validates the fix for when a child GameObject with a NetworkBehaviour
68+
/// This validates:
69+
/// - The fix for when a child GameObject with a NetworkBehaviour
4370
/// is deleted while the parent GameObject with a NetworkObject is spawned and
4471
/// is not deleted until a later time would cause an exception due to the
4572
/// NetworkBehaviour not being removed from the NetworkObject.ChildNetworkBehaviours
4673
/// list.
74+
/// - That when a child GameObject is deleted/disabled or a NetworkBehaviour is disabled
75+
/// a message is logged and the NetworkObject still can be spawned and synchronized.
4776
/// </summary>
4877
[UnityTest]
49-
public IEnumerator ValidatedDisableddNetworkBehaviourWarning()
78+
public IEnumerator ValidatedDisableddNetworkBehaviourWarning([Values] bool disableGameObject)
5079
{
5180
m_AllowServerToStart = true;
5281

53-
yield return s_DefaultWaitForTick;
54-
5582
// Now just start the Host
5683
yield return StartServerAndClients();
5784

58-
var parentObject = new GameObject();
59-
var childObject = new GameObject
60-
{
61-
name = "ChildObject"
62-
};
63-
childObject.transform.parent = parentObject.transform;
64-
var parentNetworkObject = parentObject.AddComponent<NetworkObject>();
65-
var childBehaviour = childObject.AddComponent<NetworkTransform>();
66-
67-
// Set the child object to be inactive in the hierarchy
68-
childObject.SetActive(false);
69-
70-
var expectedWarning = parentNetworkObject.GenerateDisabledNetworkBehaviourWarning(childBehaviour);
85+
// Now join a new client to make sure a connected client spawns the instance.
86+
yield return CreateAndStartNewClient();
7187

88+
// Adjust the prefab to either have the child GameObject completely disabled or the NetworkBehaviour
89+
// disabled.
90+
var childBehaviour = m_PrefabToSpawn.GetComponentInChildren<NetworkTransform>(true);
91+
if (disableGameObject)
92+
{
93+
childBehaviour.enabled = true;
94+
childBehaviour.gameObject.SetActive(false);
95+
}
96+
else
97+
{
98+
childBehaviour.enabled = false;
99+
childBehaviour.gameObject.SetActive(true);
100+
}
101+
// Now create an instance of the prefab
102+
var instance = Object.Instantiate(m_PrefabToSpawn);
103+
var instanceNetworkObject = instance.GetComponent<NetworkObject>();
104+
// Generate the expected warning message
105+
var expectedWarning = instanceNetworkObject.GenerateDisabledNetworkBehaviourWarning(instanceNetworkObject.GetComponentInChildren<NetworkTransform>(true));
106+
// Spawn the instance
107+
SpawnObjectInstance(instanceNetworkObject, m_ServerNetworkManager);
72108
LogAssert.Expect(LogType.Warning, $"{expectedWarning}");
109+
// Asure the connected client spawned the object first
110+
yield return WaitForSpawnedOnAllOrTimeOut(instanceNetworkObject);
111+
AssertOnTimeout($"Not all clients spawned {instanceNetworkObject.name}!");
73112

74-
parentNetworkObject.Spawn();
75-
yield return s_DefaultWaitForTick;
113+
// Now join a new client to make sure the client synchronizes with the disabled GameObject or NetworkBehaviour component.
114+
yield return CreateAndStartNewClient();
115+
116+
// Asure the newly connected client synchronizes the spawned object correctly
117+
yield return WaitForSpawnedOnAllOrTimeOut(instanceNetworkObject);
118+
AssertOnTimeout($"Not all clients spawned {instanceNetworkObject.name}!");
76119
}
77120

78121
/// <summary>

0 commit comments

Comments
 (0)