Skip to content

Commit 13ca162

Browse files
test
test that validates this fix. updates to bring the logging of Vector3 values into v1.x.
1 parent 7701438 commit 13ca162

File tree

4 files changed

+181
-4
lines changed

4 files changed

+181
-4
lines changed

com.unity.netcode.gameobjects/TestHelpers/Runtime/IntegrationTestWithApproximation.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ public abstract class IntegrationTestWithApproximation : NetcodeIntegrationTest
88
{
99
private const float k_AproximateDeltaVariance = 0.01f;
1010

11+
protected string GetVector3Values(ref Vector3 vector3)
12+
{
13+
return $"({vector3.x:F6},{vector3.y:F6},{vector3.z:F6})";
14+
}
15+
16+
protected string GetVector3Values(Vector3 vector3)
17+
{
18+
return GetVector3Values(ref vector3);
19+
}
20+
1121
protected virtual float GetDeltaVarianceThreshold()
1222
{
1323
return k_AproximateDeltaVariance;
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using NUnit.Framework;
5+
using Unity.Netcode.TestHelpers.Runtime;
6+
using UnityEngine;
7+
using UnityEngine.TestTools;
8+
9+
namespace Unity.Netcode.RuntimeTests
10+
{
11+
[TestFixture(NetworkSpawnTypes.OnNetworkSpawn)]
12+
[TestFixture(NetworkSpawnTypes.OnNetworkPostSpawn)]
13+
internal class ParentingDuringSpawnTests : IntegrationTestWithApproximation
14+
{
15+
protected override int NumberOfClients => 2;
16+
17+
public enum NetworkSpawnTypes
18+
{
19+
OnNetworkSpawn,
20+
OnNetworkPostSpawn,
21+
}
22+
23+
private NetworkSpawnTypes m_NetworkSpawnType;
24+
25+
private GameObject m_ParentPrefab;
26+
private GameObject m_ChildPrefab;
27+
private NetworkObject m_AuthorityInstance;
28+
private List<NetworkManager> m_NetworkManagers = new List<NetworkManager>();
29+
private StringBuilder m_Errors = new StringBuilder();
30+
31+
public class ParentDuringSpawnBehaviour : NetworkBehaviour
32+
{
33+
public GameObject ChildToSpawn;
34+
35+
public NetworkSpawnTypes NetworkSpawnType;
36+
37+
public Transform ChildSpawnPoint;
38+
39+
private void SpawnThenParent()
40+
{
41+
var child = NetworkObject.InstantiateAndSpawn(ChildToSpawn, NetworkManager, position: ChildSpawnPoint.position, rotation: ChildSpawnPoint.rotation);
42+
if (!child.TrySetParent(NetworkObject))
43+
{
44+
var errorMessage = $"[{ChildToSpawn}] Failed to parent child {child.name} under parent {gameObject.name}!";
45+
Debug.LogError(errorMessage);
46+
}
47+
}
48+
49+
public override void OnNetworkSpawn()
50+
{
51+
if (IsServer && NetworkSpawnType == NetworkSpawnTypes.OnNetworkSpawn)
52+
{
53+
SpawnThenParent();
54+
}
55+
56+
base.OnNetworkSpawn();
57+
}
58+
59+
protected override void OnNetworkPostSpawn()
60+
{
61+
if (IsServer && NetworkSpawnType == NetworkSpawnTypes.OnNetworkPostSpawn)
62+
{
63+
SpawnThenParent();
64+
}
65+
base.OnNetworkPostSpawn();
66+
}
67+
}
68+
69+
public ParentingDuringSpawnTests(NetworkSpawnTypes networkSpawnType) : base()
70+
{
71+
m_NetworkSpawnType = networkSpawnType;
72+
}
73+
74+
protected override void OnServerAndClientsCreated()
75+
{
76+
m_ParentPrefab = CreateNetworkObjectPrefab("Parent");
77+
m_ChildPrefab = CreateNetworkObjectPrefab("Child");
78+
var parentComponet = m_ParentPrefab.AddComponent<ParentDuringSpawnBehaviour>();
79+
parentComponet.ChildToSpawn = m_ChildPrefab;
80+
var spawnPoint = new GameObject();
81+
parentComponet.ChildSpawnPoint = spawnPoint.transform;
82+
parentComponet.ChildSpawnPoint.position = GetRandomVector3(-5.0f, 5.0f);
83+
var rotation = parentComponet.ChildSpawnPoint.rotation;
84+
rotation.eulerAngles = GetRandomVector3(-180.0f, 180.0f);
85+
parentComponet.ChildSpawnPoint.rotation = rotation;
86+
base.OnServerAndClientsCreated();
87+
}
88+
89+
private bool NonAuthorityInstancesSpawnedParent()
90+
{
91+
foreach (var networkManager in m_NetworkManagers)
92+
{
93+
if (!networkManager.SpawnManager.SpawnedObjects.ContainsKey(m_AuthorityInstance.NetworkObjectId))
94+
{
95+
return false;
96+
}
97+
}
98+
return true;
99+
}
100+
101+
private bool NonAuthorityInstancesParentedChild()
102+
{
103+
m_Errors.Clear();
104+
if (m_AuthorityInstance.transform.childCount == 0)
105+
{
106+
return false;
107+
}
108+
var authorityChildObject = m_AuthorityInstance.transform.GetChild(0).GetComponent<NetworkObject>();
109+
110+
foreach (var networkManager in m_NetworkManagers)
111+
{
112+
if (!networkManager.SpawnManager.SpawnedObjects.ContainsKey(authorityChildObject.NetworkObjectId))
113+
{
114+
m_Errors.AppendLine($"{networkManager.name} has not spawned the child {authorityChildObject.name}!");
115+
return false;
116+
}
117+
var childObject = networkManager.SpawnManager.SpawnedObjects[authorityChildObject.NetworkObjectId];
118+
119+
if (childObject.transform.parent == null)
120+
{
121+
m_Errors.AppendLine($"{childObject.name} does not have a parent!");
122+
return false;
123+
}
124+
125+
if (!Approximately(authorityChildObject.transform.position, childObject.transform.position))
126+
{
127+
m_Errors.AppendLine($"{childObject.name} position {GetVector3Values(childObject.transform.position)} does " +
128+
$"not match the authority's position {GetVector3Values(authorityChildObject.transform.position)}!");
129+
return false;
130+
}
131+
132+
if (!Approximately(authorityChildObject.transform.rotation, childObject.transform.rotation))
133+
{
134+
m_Errors.AppendLine($"{childObject.name} rotation {GetVector3Values(childObject.transform.rotation.eulerAngles)} does " +
135+
$"not match the authority's position {GetVector3Values(authorityChildObject.transform.rotation.eulerAngles)}!");
136+
return false;
137+
}
138+
}
139+
return true;
140+
}
141+
142+
[UnityTest]
143+
public IEnumerator ParentDuringSpawn()
144+
{
145+
m_NetworkManagers.Clear();
146+
var authorityNetworkManager = m_ServerNetworkManager;
147+
148+
m_NetworkManagers.AddRange(m_ClientNetworkManagers);
149+
m_NetworkManagers.Add(m_ServerNetworkManager);
150+
151+
m_AuthorityInstance = SpawnObject(m_ParentPrefab, authorityNetworkManager).GetComponent<NetworkObject>();
152+
153+
yield return WaitForConditionOrTimeOut(NonAuthorityInstancesSpawnedParent);
154+
AssertOnTimeout($"Not all clients spawned the parent {nameof(NetworkObject)}!");
155+
156+
yield return WaitForConditionOrTimeOut(NonAuthorityInstancesParentedChild);
157+
AssertOnTimeout($"Non-Authority instance had a mismatched value: \n {m_Errors}");
158+
}
159+
}
160+
}

com.unity.netcode.gameobjects/Tests/Runtime/ParentingDuringSpawnTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testproject/Assets/Tests/Runtime/NetworkTransform/NestedNetworkTransformTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,6 @@ protected override float GetDeltaVarianceThreshold()
181181

182182
private StringBuilder m_ValidationErrors;
183183

184-
private string GetVector3Values(ref Vector3 vector3)
185-
{
186-
return $"({vector3.x:F6},{vector3.y:F6},{vector3.z:F6})";
187-
}
188184

189185
/// <summary>
190186
/// Validates all transform instance values match the authority's

0 commit comments

Comments
 (0)