Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,14 @@
IsDestroying = true;
}

/// <summary>
/// Invoked by <see cref="NetworkObject.ResetIsDestroying"/>.
/// </summary>
internal void ResetIsDestroying()
{
IsDestroying = false;
}

Check warning on line 711 in com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs#L709-L711

Added lines #L709 - L711 were not covered by tests

/// <summary>
/// Updates properties with network session related
/// dependencies such as a NetworkObject's spawned
Expand Down
19 changes: 17 additions & 2 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1953,14 +1953,29 @@
{
// Tell the childBehaviour that this NetworkObject is being destroyed.
// Keeping the property a private set to assure this is
// the only way it can be set as it should never be reset
// back to false once invoked.
// the only way it can be set. Only a hybrid instance that
// its ghost keeps alive resets it (ResetIsDestroying).
childBehaviour?.SetIsDestroying();
}
}
IsDestroying = true;
}

/// <summary>
/// Clears the despawning flag of a hybrid instance that its ghost keeps alive, and of its child NetworkBehaviours.
/// </summary>
internal void ResetIsDestroying()
{
if (ChildNetworkBehaviours != null)
{
foreach (var childBehaviour in ChildNetworkBehaviours.Values)
{
childBehaviour?.ResetIsDestroying();
}
}
IsDestroying = false;
}

Check warning on line 1977 in com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs#L1968-L1977

Added lines #L1968 - L1977 were not covered by tests

private void OnDestroy()
{
SceneManager.activeSceneChanged -= CurrentlyActiveSceneChanged;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@

// TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
// NOTE: We might be able to use the NetworkSceneHandle to get the associated local scene handle to which we can use to get the targeted scene.
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(networkObject.gameObject, UnityEngine.SceneManagement.SceneManager.GetActiveScene());
// A child kept alive by its ghost (NetworkHide) is still parented and stays in its parent's scene.
if (networkObject.transform.parent == null)
{
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(networkObject.gameObject, UnityEngine.SceneManagement.SceneManager.GetActiveScene());
}

Check warning on line 122 in com.unity.netcode.gameobjects/Runtime/Spawning/GhostSpawnManager.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Spawning/GhostSpawnManager.cs#L119-L122

Added lines #L119 - L122 were not covered by tests
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,12 @@
// Unified netcode handles destroying the instance of the object on the non-authority side when the object has a ghost representation.
if (destroyGameObject && networkObject.HasGhost && !NetworkManager.IsServer)
{
// exit early
// A NetworkHide leaves the ghost alive, so the instance waits for a later NetworkShow (removed again if N4E destroys it).
if (networkObject && NetworkManager.IsConnectedClient && !NetworkManager.ShutdownInProgress)
Comment thread
NoelStephensUnity marked this conversation as resolved.
Resolved
{
networkObject.ResetIsDestroying();
GhostSpawnManager.RegisterGhostPendingSpawn(networkObject, networkObject.NetworkObjectId);
Comment thread
NoelStephensUnity marked this conversation as resolved.
Resolved
}

Check warning on line 1966 in com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs#L1962-L1966

Added lines #L1962 - L1966 were not covered by tests
return;
}
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#if UNIFIED_NETCODE
using System;
using System.Collections;
using NUnit.Framework;
using Unity.Netcode.Components;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.TestTools;

namespace Unity.Netcode.RuntimeTests
{
/// <summary>
/// Validates that hiding and then showing a spawned hybrid instance respawns it on the client.
/// </summary>
[TestFixture(HostOrServer.UnifiedHost)]
[TestFixture(HostOrServer.UnifiedServer)]
internal class HybridNetworkShowHideTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 1;

private GameObject m_HybridPrefab;
private GameObject m_AttachablePrefab;
private GameObject m_AttachableNodePrefab;
private ulong m_NetworkObjectId;

public HybridNetworkShowHideTests(HostOrServer hostOrServer) : base(hostOrServer) { }

protected override bool UseUnifiedTests()
{
return true;
}

protected override void OnServerAndClientsCreated()
{
m_HybridPrefab = CreateNetworkObjectPrefab("HybridShowHide");
m_AttachablePrefab = CreateNetworkObjectPrefab("HybridAttachable");
var attachableChild = new GameObject("AttachableChild");
attachableChild.transform.parent = m_AttachablePrefab.transform;
attachableChild.AddComponent<AttachableBehaviour>();
m_AttachableNodePrefab = CreateNetworkObjectPrefab("HybridAttachableNode");
var nodeChild = new GameObject("NodeChild");
nodeChild.transform.parent = m_AttachableNodePrefab.transform;
nodeChild.AddComponent<AttachableNode>();
base.OnServerAndClientsCreated();
}

private bool IsSpawnedOnClient()
{
return GetNonAuthorityNetworkManager().SpawnManager.SpawnedObjects.ContainsKey(m_NetworkObjectId);
}

private IEnumerator SpawnOnAll(GameObject prefab, Action<NetworkObject> onSpawned)
{
var authorityInstance = SpawnObject(prefab, GetAuthorityNetworkManager()).GetComponent<NetworkObject>();
yield return WaitForSpawnedOnAllOrTimeOut(authorityInstance);
AssertOnTimeout($"[Setup] {authorityInstance.name} was not spawned on all instances!");
onSpawned(authorityInstance);
}

private IEnumerator HideThenShow(NetworkObject authorityInstance, string context)
{
var nonAuthority = GetNonAuthorityNetworkManager();
m_NetworkObjectId = authorityInstance.NetworkObjectId;
authorityInstance.NetworkHide(nonAuthority.LocalClientId);
yield return WaitForConditionOrTimeOut(() => !IsSpawnedOnClient());
AssertOnTimeout($"[{context}] {authorityInstance.name} was not despawned on Client-{nonAuthority.LocalClientId}!");

authorityInstance.NetworkShow(nonAuthority.LocalClientId);
yield return WaitForConditionOrTimeOut(IsSpawnedOnClient);
AssertOnTimeout($"[{context}] {authorityInstance.name} was not respawned on Client-{nonAuthority.LocalClientId}!");
}

[UnityTest]
public IEnumerator HideThenShowRespawnsOnClient()
{
var nonAuthority = GetNonAuthorityNetworkManager();
NetworkObject authorityInstance = null;
yield return SpawnOnAll(m_HybridPrefab, spawned => authorityInstance = spawned);
var clientInstance = nonAuthority.SpawnManager.SpawnedObjects[authorityInstance.NetworkObjectId];

// A second cycle shows the respawned instance can be hidden and shown again.
for (int i = 0; i < 2; i++)
{
yield return HideThenShow(authorityInstance, $"Cycle {i}");
Assert.AreSame(clientInstance, nonAuthority.SpawnManager.SpawnedObjects[m_NetworkObjectId], $"[Cycle {i}] Client-{nonAuthority.LocalClientId} did not respawn the instance its ghost kept alive!");
}
}

[UnityTest]
public IEnumerator HideThenShowParentedChild()
{
var nonAuthority = GetNonAuthorityNetworkManager();
NetworkObject parent = null;
NetworkObject child = null;
yield return SpawnOnAll(m_HybridPrefab, spawned => parent = spawned);
yield return SpawnOnAll(m_HybridPrefab, spawned => child = spawned);
var clientParent = nonAuthority.SpawnManager.SpawnedObjects[parent.NetworkObjectId].transform;
var clientChild = nonAuthority.SpawnManager.SpawnedObjects[child.NetworkObjectId].transform;
Assert.IsTrue(child.TrySetParent(parent, false), $"{nameof(NetworkObject.TrySetParent)} failed!");
yield return WaitForConditionOrTimeOut(() => clientChild.parent == clientParent);
AssertOnTimeout($"[Setup] {child.name} was not parented on Client-{nonAuthority.LocalClientId}!");

yield return HideThenShow(child, "Parented");
Assert.AreSame(clientParent, clientChild.parent, $"Client-{nonAuthority.LocalClientId} did not keep {child.name} parented!");
}

[UnityTest]
public IEnumerator HideThenShowThenAttachAndDetach()
{
var nonAuthority = GetNonAuthorityNetworkManager();
NetworkObject source = null;
NetworkObject node = null;
yield return SpawnOnAll(m_AttachablePrefab, spawned => source = spawned);
yield return SpawnOnAll(m_AttachableNodePrefab, spawned => node = spawned);
yield return HideThenShow(source, "Attachable");

var clientSource = nonAuthority.SpawnManager.SpawnedObjects[source.NetworkObjectId].transform;
var clientAttachable = clientSource.GetComponentInChildren<AttachableBehaviour>().transform;
var clientNode = nonAuthority.SpawnManager.SpawnedObjects[node.NetworkObjectId].GetComponentInChildren<AttachableNode>().transform;
var attachable = source.GetComponentInChildren<AttachableBehaviour>();

attachable.Attach(node.GetComponentInChildren<AttachableNode>());
yield return WaitForConditionOrTimeOut(() => clientAttachable.parent == clientNode);
AssertOnTimeout($"Client-{nonAuthority.LocalClientId} did not attach {clientAttachable.name}!");

attachable.Detach();
yield return WaitForConditionOrTimeOut(() => clientAttachable.parent == clientSource);
AssertOnTimeout($"Client-{nonAuthority.LocalClientId} did not detach {clientAttachable.name}!");
}

[UnityTest]
public IEnumerator DespawnWhileHiddenReleasesClientInstance()
{
var nonAuthority = GetNonAuthorityNetworkManager();
NetworkObject authorityInstance = null;
yield return SpawnOnAll(m_HybridPrefab, spawned => authorityInstance = spawned);
m_NetworkObjectId = authorityInstance.NetworkObjectId;
var clientInstance = nonAuthority.SpawnManager.SpawnedObjects[m_NetworkObjectId];

authorityInstance.NetworkHide(nonAuthority.LocalClientId);
yield return WaitForConditionOrTimeOut(() => !IsSpawnedOnClient());
AssertOnTimeout($"[Setup] {authorityInstance.name} was not despawned on Client-{nonAuthority.LocalClientId}!");
Assert.IsTrue(nonAuthority.SpawnManager.GhostSpawnManager.IsGhostPendingSpawn(m_NetworkObjectId), $"Client-{nonAuthority.LocalClientId} is not holding the hidden instance for a later show!");

authorityInstance.Despawn();
yield return WaitForConditionOrTimeOut(() => clientInstance == null);
AssertOnTimeout($"N4E did not destroy the hidden instance on Client-{nonAuthority.LocalClientId}!");
Assert.IsFalse(nonAuthority.SpawnManager.GhostSpawnManager.IsGhostPendingSpawn(m_NetworkObjectId), $"Client-{nonAuthority.LocalClientId} still holds the destroyed instance as pending!");
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading