|
1 | 1 | using System.Collections;
|
| 2 | +using System.Linq; |
2 | 3 | using NUnit.Framework;
|
3 | 4 | using Unity.Netcode.TestHelpers.Runtime;
|
4 | 5 | using UnityEngine;
|
@@ -48,4 +49,112 @@ public IEnumerator SpawnAndReplaceExistingPlayerObject()
|
48 | 49 | Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for client-side player object to change!");
|
49 | 50 | }
|
50 | 51 | }
|
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Validate that when auto-player spawning but SpawnWithObservers is disabled, |
| 55 | + /// the player instantiated is only spawned on the authority side. |
| 56 | + /// </summary> |
| 57 | + [TestFixture(HostOrServer.Host)] |
| 58 | + [TestFixture(HostOrServer.Server)] |
| 59 | + internal class PlayerSpawnNoObserversTest : NetcodeIntegrationTest |
| 60 | + { |
| 61 | + protected override int NumberOfClients => 2; |
| 62 | + |
| 63 | + public PlayerSpawnNoObserversTest(HostOrServer hostOrServer) : base(hostOrServer) { } |
| 64 | + |
| 65 | + protected override bool ShouldCheckForSpawnedPlayers() |
| 66 | + { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + protected override void OnCreatePlayerPrefab() |
| 71 | + { |
| 72 | + var playerNetworkObject = m_PlayerPrefab.GetComponent<NetworkObject>(); |
| 73 | + playerNetworkObject.SpawnWithObservers = false; |
| 74 | + base.OnCreatePlayerPrefab(); |
| 75 | + } |
| 76 | + |
| 77 | + [UnityTest] |
| 78 | + public IEnumerator SpawnWithNoObservers() |
| 79 | + { |
| 80 | + yield return s_DefaultWaitForTick; |
| 81 | + |
| 82 | + var playerObjects = m_ServerNetworkManager.SpawnManager.SpawnedObjectsList.Where((c) => c.IsPlayerObject).ToList(); |
| 83 | + |
| 84 | + // Make sure clients did not spawn their player object on any of the clients including the owner. |
| 85 | + foreach (var client in m_ClientNetworkManagers) |
| 86 | + { |
| 87 | + foreach (var playerObject in playerObjects) |
| 88 | + { |
| 89 | + Assert.IsFalse(client.SpawnManager.SpawnedObjects.ContainsKey(playerObject.NetworkObjectId), $"Client-{client.LocalClientId} spawned player object for Client-{playerObject.NetworkObjectId}!"); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// This test validates the player position and rotation is correct |
| 97 | + /// relative to the prefab's initial settings if no changes are applied. |
| 98 | + /// </summary> |
| 99 | + [TestFixture(HostOrServer.Host)] |
| 100 | + [TestFixture(HostOrServer.Server)] |
| 101 | + internal class PlayerSpawnPositionTests : IntegrationTestWithApproximation |
| 102 | + { |
| 103 | + protected override int NumberOfClients => 2; |
| 104 | + |
| 105 | + public PlayerSpawnPositionTests(HostOrServer hostOrServer) |
| 106 | + { |
| 107 | + m_UseHost = hostOrServer == HostOrServer.Host; |
| 108 | + } |
| 109 | + |
| 110 | + private Vector3 m_PlayerPosition; |
| 111 | + private Quaternion m_PlayerRotation; |
| 112 | + |
| 113 | + protected override void OnCreatePlayerPrefab() |
| 114 | + { |
| 115 | + var playerNetworkObject = m_PlayerPrefab.GetComponent<NetworkObject>(); |
| 116 | + m_PlayerPosition = GetRandomVector3(-10.0f, 10.0f); |
| 117 | + m_PlayerRotation = Quaternion.Euler(GetRandomVector3(-180.0f, 180.0f)); |
| 118 | + playerNetworkObject.transform.position = m_PlayerPosition; |
| 119 | + playerNetworkObject.transform.rotation = m_PlayerRotation; |
| 120 | + base.OnCreatePlayerPrefab(); |
| 121 | + } |
| 122 | + |
| 123 | + private void PlayerTransformMatches(NetworkObject player) |
| 124 | + { |
| 125 | + var position = player.transform.position; |
| 126 | + var rotation = player.transform.rotation; |
| 127 | + Assert.True(Approximately(m_PlayerPosition, position), $"Client-{player.OwnerClientId} position {position} does not match the prefab position {m_PlayerPosition}!"); |
| 128 | + Assert.True(Approximately(m_PlayerRotation, rotation), $"Client-{player.OwnerClientId} rotation {rotation.eulerAngles} does not match the prefab rotation {m_PlayerRotation.eulerAngles}!"); |
| 129 | + } |
| 130 | + |
| 131 | + [UnityTest] |
| 132 | + public IEnumerator PlayerSpawnPosition() |
| 133 | + { |
| 134 | + if (m_ServerNetworkManager.IsHost) |
| 135 | + { |
| 136 | + PlayerTransformMatches(m_ServerNetworkManager.LocalClient.PlayerObject); |
| 137 | + |
| 138 | + foreach (var client in m_ClientNetworkManagers) |
| 139 | + { |
| 140 | + yield return WaitForConditionOrTimeOut(() => client.SpawnManager.SpawnedObjects.ContainsKey(m_ServerNetworkManager.LocalClient.PlayerObject.NetworkObjectId)); |
| 141 | + AssertOnTimeout($"Client-{client.LocalClientId} does not contain a player prefab instance for client-{m_ServerNetworkManager.LocalClientId}!"); |
| 142 | + PlayerTransformMatches(client.SpawnManager.SpawnedObjects[m_ServerNetworkManager.LocalClient.PlayerObject.NetworkObjectId]); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + foreach (var client in m_ClientNetworkManagers) |
| 147 | + { |
| 148 | + yield return WaitForConditionOrTimeOut(() => m_ServerNetworkManager.SpawnManager.SpawnedObjects.ContainsKey(client.LocalClient.PlayerObject.NetworkObjectId)); |
| 149 | + AssertOnTimeout($"Client-{m_ServerNetworkManager.LocalClientId} does not contain a player prefab instance for client-{client.LocalClientId}!"); |
| 150 | + PlayerTransformMatches(m_ServerNetworkManager.SpawnManager.SpawnedObjects[client.LocalClient.PlayerObject.NetworkObjectId]); |
| 151 | + foreach (var subClient in m_ClientNetworkManagers) |
| 152 | + { |
| 153 | + yield return WaitForConditionOrTimeOut(() => subClient.SpawnManager.SpawnedObjects.ContainsKey(client.LocalClient.PlayerObject.NetworkObjectId)); |
| 154 | + AssertOnTimeout($"Client-{subClient.LocalClientId} does not contain a player prefab instance for client-{client.LocalClientId}!"); |
| 155 | + PlayerTransformMatches(subClient.SpawnManager.SpawnedObjects[client.LocalClient.PlayerObject.NetworkObjectId]); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
51 | 160 | }
|
0 commit comments