-
Notifications
You must be signed in to change notification settings - Fork 464
fix: Exception with disabled GameObjects and NetworkTransforms #3243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
EmandM
merged 8 commits into
develop-2.0.0
from
fix/mttb-959-exception-with-disabled-gameobjects
Feb 7, 2025
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f0dab01
fix: Exception with disabled GameObjects and NetworkTransforms
EmandM bd79f9b
Add CHANGELOG
EmandM 2456a82
Merge branch 'develop-2.0.0' of https://github.com/Unity-Technologies…
EmandM c3493fe
Update naming and code comments to be clearer
EmandM fbe8081
fix
NoelStephensUnity bb6b109
fix
NoelStephensUnity 7d49662
style
NoelStephensUnity 1865157
update + style
NoelStephensUnity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,8 +71,21 @@ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int | |
| if (isSpawnedLocally) | ||
| { | ||
| networkObject = networkManager.SpawnManager.SpawnedObjects[networkObjectId]; | ||
| if (networkObject.ChildNetworkBehaviours.Count <= networkBehaviourId || networkObject.ChildNetworkBehaviours[networkBehaviourId] == null) | ||
| { | ||
| Debug.LogError($"[{nameof(NetworkTransformMessage)}][Invalid] Targeted {nameof(NetworkTransform)}, {nameof(NetworkBehaviour.NetworkBehaviourId)} ({networkBehaviourId}), does not exist! Make sure you are not spawning {nameof(NetworkObject)}s with disabled {nameof(GameObject)}s that have {nameof(NetworkBehaviour)} components on them."); | ||
| return false; | ||
| } | ||
|
|
||
| // Get the target NetworkTransform | ||
| NetworkTransform = networkObject.ChildNetworkBehaviours[networkBehaviourId] as NetworkTransform; | ||
| var transform = networkObject.ChildNetworkBehaviours[networkBehaviourId] as NetworkTransform; | ||
| if (transform == null) | ||
| { | ||
| Debug.LogError($"[{nameof(NetworkTransformMessage)}][Invalid] Targeted {nameof(NetworkTransform)}, {nameof(NetworkBehaviour.NetworkBehaviourId)} ({networkBehaviourId}), does not exist! Make sure you are not spawning {nameof(NetworkObject)}s with disabled {nameof(GameObject)}s that have {nameof(NetworkBehaviour)} components on them."); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ➕ |
||
| return false; | ||
| } | ||
|
|
||
| NetworkTransform = transform; | ||
| isServerAuthoritative = NetworkTransform.IsServerAuthoritative(); | ||
| ownerAuthoritativeServerSide = !isServerAuthoritative && networkManager.IsServer; | ||
|
|
||
|
|
@@ -81,8 +94,8 @@ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int | |
| } | ||
| else | ||
| { | ||
| // Deserialize the state | ||
| reader.ReadNetworkSerializableInPlace(ref State); | ||
| Debug.LogError($"[{nameof(NetworkTransformMessage)}][Invalid] Target NetworkObject does not exist!"); | ||
| return false; | ||
| } | ||
|
|
||
| unsafe | ||
|
|
||
130 changes: 130 additions & 0 deletions
130
com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformErrorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| using System.Collections; | ||
| using Unity.Netcode.Components; | ||
| using Unity.Netcode.TestHelpers.Runtime; | ||
| using UnityEngine; | ||
| using UnityEngine.TestTools; | ||
|
|
||
|
|
||
| namespace Unity.Netcode.RuntimeTests | ||
| { | ||
| internal class NetworkTransformErrorTests : NetcodeIntegrationTest | ||
| { | ||
| protected override int NumberOfClients => 1; | ||
|
|
||
| private GameObject m_AuthorityPrefab; | ||
| private GameObject m_NonAuthorityPrefab; | ||
|
|
||
| private HostAndClientPrefabHandler m_HostAndClientPrefabHandler; | ||
|
|
||
| public class EmptyNetworkBehaviour : NetworkBehaviour { } | ||
|
|
||
| /// <summary> | ||
| /// PrefabHandler that tracks and separates the client GameObject from the host GameObject. | ||
| /// Allows independent management of client and host game world while still instantiating NetworkObjects as expected. | ||
| /// </summary> | ||
| private class HostAndClientPrefabHandler : INetworkPrefabInstanceHandler | ||
| { | ||
| /// <summary> | ||
| /// The registered prefab is the prefab the networking stack is instantiated with. | ||
| /// Registering the prefab simulates the prefab that exists on the authority. | ||
| /// </summary> | ||
| private readonly GameObject m_RegisteredPrefab; | ||
|
|
||
| /// <summary> | ||
| /// Mocks the registered prefab changing on the non-authority after registration. | ||
| /// Allows testing situations mismatched GameObject state between the authority and non-authority. | ||
| /// </summary> | ||
| private readonly GameObject m_InstantiatedPrefab; | ||
|
|
||
| public HostAndClientPrefabHandler(GameObject authorityPrefab, GameObject nonAuthorityPrefab) | ||
| { | ||
| m_RegisteredPrefab = authorityPrefab; | ||
| m_InstantiatedPrefab = nonAuthorityPrefab; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the prefab that will mock the instantiated prefab not matching the registered prefab | ||
| /// </summary> | ||
| public NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation) | ||
| { | ||
| return Object.Instantiate(m_InstantiatedPrefab).GetComponent<NetworkObject>(); | ||
| } | ||
|
|
||
| public void Destroy(NetworkObject networkObject) | ||
| { | ||
| Object.Destroy(networkObject.gameObject); | ||
| } | ||
|
|
||
| public void Register(NetworkManager networkManager) | ||
| { | ||
| // Register the version that will be spawned by the authority (i.e. Host) | ||
| networkManager.PrefabHandler.AddHandler(m_RegisteredPrefab, this); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a GameObject and sets the transform parent to the given transform | ||
| /// Adds a component of the given type to the GameObject | ||
| /// </summary> | ||
| private static void AddChildToNetworkObject<T>(Transform transform) where T : Component | ||
| { | ||
| var gameObj = new GameObject(); | ||
| gameObj.transform.parent = transform; | ||
| gameObj.AddComponent<T>(); | ||
| } | ||
|
|
||
| protected override void OnServerAndClientsCreated() | ||
| { | ||
| // Create a prefab that has many child NetworkBehaviours | ||
| m_AuthorityPrefab = CreateNetworkObjectPrefab("AuthorityPrefab"); | ||
| AddChildToNetworkObject<EmptyNetworkBehaviour>(m_AuthorityPrefab.transform); | ||
| AddChildToNetworkObject<EmptyNetworkBehaviour>(m_AuthorityPrefab.transform); | ||
| AddChildToNetworkObject<NetworkTransform>(m_AuthorityPrefab.transform); | ||
|
|
||
| // Create a second prefab with only one NetworkBehaviour | ||
| // This simulates the GameObjects on the other NetworkBehaviours being disabled | ||
| m_NonAuthorityPrefab = CreateNetworkObjectPrefab("NonAuthorityPrefab"); | ||
| AddChildToNetworkObject<NetworkTransform>(m_NonAuthorityPrefab.transform); | ||
|
|
||
| // Create and register a prefab handler | ||
| // The prefab handler will behave as if the GameObjects have been disabled on the non-authority client | ||
| m_HostAndClientPrefabHandler = new HostAndClientPrefabHandler(m_AuthorityPrefab, m_NonAuthorityPrefab); | ||
| m_HostAndClientPrefabHandler.Register(m_ServerNetworkManager); | ||
| foreach (var client in m_ClientNetworkManagers) | ||
| { | ||
| m_HostAndClientPrefabHandler.Register(client); | ||
| } | ||
|
|
||
| base.OnServerAndClientsCreated(); | ||
| } | ||
|
|
||
|
|
||
| [UnityTest] | ||
| public IEnumerator DisabledGameObjectErrorTest() | ||
| { | ||
| var instance = SpawnObject(m_AuthorityPrefab, m_ServerNetworkManager); | ||
| var networkObjectInstance = instance.GetComponent<NetworkObject>(); | ||
|
|
||
| yield return WaitForConditionOrTimeOut(() => ObjectSpawnedOnAllClients(networkObjectInstance.NetworkObjectId)); | ||
| AssertOnTimeout("Timed out waiting for object to spawn!"); | ||
|
|
||
| LogAssert.Expect(LogType.Error, "[Netcode] NetworkBehaviour index 3 was out of bounds for NonAuthorityPrefab(Clone). NetworkBehaviours must be the same, and in the same order, between server and client."); | ||
| LogAssert.Expect(LogType.Error, "[NetworkTransformMessage][Invalid] Targeted NetworkTransform, NetworkBehaviourId (3), does not exist! Make sure you are not spawning NetworkObjects with disabled GameObjects that have NetworkBehaviour components on them."); | ||
|
|
||
| yield return new WaitForSeconds(0.3f); | ||
| } | ||
|
|
||
| private bool ObjectSpawnedOnAllClients(ulong networkObjectId) | ||
| { | ||
| foreach (var client in m_ClientNetworkManagers) | ||
| { | ||
| if (!client.SpawnManager.SpawnedObjects.ContainsKey(networkObjectId)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| } |
3 changes: 3 additions & 0 deletions
3
...ity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformErrorTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice wordsmithing...