Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 463
fix: spawn disabled in scene placed and network prefab registration#4093
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
NoelStephensUnity
merged 25 commits into
develop-2.0.0
from
fix/spawn-disabled-in-scene-placed-and-network-prefab-registrationJul 28, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6289aba
fix
NoelStephensUnity e70d82a
update
NoelStephensUnity 8768263
test
NoelStephensUnity 9ec5725
update
NoelStephensUnity a27cffb
test
NoelStephensUnity 461ca09
test - fix
NoelStephensUnity db238f5
Merge branch 'develop-2.0.0' into fix/spawn-disabled-in-scene-placed-…
NoelStephensUnity 0764197
style
NoelStephensUnity a365363
style
NoelStephensUnity 026f450
style
NoelStephensUnity b7f0769
style
NoelStephensUnity 091b502
style
NoelStephensUnity c484ce7
update
NoelStephensUnity 34a6b8d
update
NoelStephensUnity eb40ac0
Merge branch 'fix/spawn-disabled-in-scene-placed-and-network-prefab-r…
NoelStephensUnity c3eeb41
update
NoelStephensUnity 3c52055
style
NoelStephensUnity a1a15ba
Merge branch 'develop-2.0.0' into fix/spawn-disabled-in-scene-placed-…
NoelStephensUnity c695fd5
update
NoelStephensUnity d8b6cde
update
NoelStephensUnity d5e2484
doc
NoelStephensUnity ad3a3a1
Update documentation for the two types of NetworkObjects
NoelStephensUnity f7cbdf3
style
NoelStephensUnity 4f61564
Apply suggestions from code review
NoelStephensUnity 98a9fe2
Merge branch 'develop-2.0.0' into fix/spawn-disabled-in-scene-placed-…
NoelStephensUnity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
6 changes: 5 additions & 1 deletion
6 ...eobjects/Documentation~/basics/scenemanagement/inscene-placed-networkobjects.md
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
26 changes: 26 additions & 0 deletions
26 com.unity.netcode.gameobjects/Documentation~/components/core/networkobject.md
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
14 changes: 14 additions & 0 deletions
14 com.unity.netcode.gameobjects/Editor/InScenePlacedProcessor.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
2 changes: 1 addition & 1 deletion
2 com.unity.netcode.gameobjects/Runtime/Components/NetworkAnimator.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
124 changes: 100 additions & 24 deletions
124 com.unity.netcode.gameobjects/Runtime/Configuration/NetworkPrefabs.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 |
|---|---|---|
| @@ -47,22 +47,110 @@ | ||
| [NonSerialized] | ||
| private List<NetworkPrefab> m_Prefabs = new List<NetworkPrefab>(); | ||
| /// <summary> | ||
| /// Returns the last registered prefab. | ||
| /// </summary> | ||
| internal NetworkPrefab GetLastRegisteredPrefab() | ||
| { | ||
| if (m_Prefabs.Count == 0) | ||
| { | ||
| return null; | ||
| } | ||
| return m_Prefabs[m_Prefabs.Count - 1]; | ||
| } | ||
| /// <summary> | ||
| /// Applies a network prefab at a specific index | ||
| /// </summary> | ||
| /// <param name="index">index to apply</param> | ||
| /// <param name="networkPrefab">network prefab to be applied</param> | ||
| /// <returns></returns> | ||
| internal bool AssignPrefabAtIndex(int index, NetworkPrefab networkPrefab) | ||
| { | ||
| if (index >= m_Prefabs.Count) | ||
| { | ||
| NetworkManager.Singleton.Log.Error(new Logging.Context(LogLevel.Normal, $"[{nameof(NetworkPrefabs)}][{nameof(AssignPrefabAtIndex)}] Cannot apply prefab to index {index} when the {nameof(m_Prefabs)} count is only {m_Prefabs.Count}!")); | ||
| return false; | ||
| } | ||
| m_Prefabs[index] = networkPrefab; | ||
| return true; | ||
| } | ||
| [NonSerialized] | ||
| private Dictionary<uint, NetworkPrefab> m_PrefabHashIds = new Dictionary<uint, NetworkPrefab>(); | ||
| [NonSerialized] | ||
| private List<NetworkPrefab> m_RuntimeAddedPrefabs = new List<NetworkPrefab>(); | ||
| private void AddTriggeredByNetworkPrefabList(NetworkPrefab networkPrefab) | ||
| private bool InternalAddPrefab(NetworkPrefab networkPrefab) | ||
| { | ||
| if (AddPrefabRegistration(networkPrefab)) | ||
| { | ||
| // Don't add this to m_RuntimeAddedPrefabs | ||
| // This prefab is now in the PrefabList, so if we shutdown and initialize again, we'll pick it up from there. | ||
| m_Prefabs.Add(networkPrefab); | ||
| // We are not getting all potential overrides but just determining if the prefab has been registered. | ||
| if (!m_PrefabHashIds.ContainsKey(networkPrefab.SourcePrefabGlobalObjectIdHash)) | ||
| { | ||
| m_PrefabHashIds.Add(networkPrefab.SourcePrefabGlobalObjectIdHash, networkPrefab); | ||
| } | ||
| if (!m_PrefabHashIds.ContainsKey(networkPrefab.TargetPrefabGlobalObjectIdHash)) | ||
| { | ||
| m_PrefabHashIds.Add(networkPrefab.TargetPrefabGlobalObjectIdHash, networkPrefab); | ||
| } | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| private void RemoveTriggeredByNetworkPrefabList(NetworkPrefab networkPrefab) | ||
| private void InternalRemovePrefab(NetworkPrefab networkPrefab) | ||
| { | ||
| m_Prefabs.Remove(networkPrefab); | ||
| m_PrefabHashIds.Remove(networkPrefab.SourcePrefabGlobalObjectIdHash); | ||
| } | ||
| internal bool IsBasedOnRegisteredPrefab(NetworkObject networkObject) | ||
| { | ||
| return m_PrefabHashIds.ContainsKey(networkObject.GlobalObjectIdHash); | ||
| } | ||
| internal bool IsActualPrefabAsset(NetworkObject networkObject) | ||
| { | ||
| var isActualPrefabAsset = false; | ||
| if (m_PrefabHashIds.TryGetValue(networkObject.GlobalObjectIdHash, out NetworkPrefab networkPrefab)) | ||
| { | ||
| switch (networkPrefab.Override) | ||
| { | ||
| case NetworkPrefabOverride.Prefab: | ||
| case NetworkPrefabOverride.None: | ||
| { | ||
| isActualPrefabAsset = networkPrefab.Prefab != null && networkObject.gameObject == networkPrefab.Prefab; | ||
| break; | ||
| } | ||
| case NetworkPrefabOverride.Hash: | ||
| { | ||
| isActualPrefabAsset = networkPrefab.SourceHashToOverride == networkObject.GlobalObjectIdHash; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return isActualPrefabAsset; | ||
| } | ||
| private void AddTriggeredByNetworkPrefabList(NetworkPrefab networkPrefab) | ||
| { | ||
| // Don't add this to m_RuntimeAddedPrefabs | ||
| // This prefab is now in the PrefabList, so if we shutdown and initialize again, we'll pick it up from there. | ||
| InternalAddPrefab(networkPrefab); | ||
| // Log warning if this returns false? | ||
| } | ||
| private void RemoveTriggeredByNetworkPrefabList(NetworkPrefab networkPrefab) | ||
| { | ||
| InternalRemovePrefab(networkPrefab); | ||
| } | ||
| /// <summary> | ||
| @@ -93,6 +181,7 @@ | ||
| /// <param name="warnInvalid">When true, logs warnings about invalid prefabs that are removed during initialization</param> | ||
| public void Initialize(bool warnInvalid = true) | ||
| { | ||
| m_PrefabHashIds.Clear(); | ||
| m_Prefabs.Clear(); | ||
| NetworkPrefabsLists.RemoveAll(x => x == null); | ||
| foreach (var list in NetworkPrefabsLists) | ||
| @@ -113,7 +202,7 @@ | ||
| prefabs.AddRange(list.PrefabList); | ||
| } | ||
| } | ||
| m_PrefabHashIds = new Dictionary<uint, NetworkPrefab>(); | ||
| m_Prefabs = new List<NetworkPrefab>(); | ||
| List<NetworkPrefab> removeList = null; | ||
| @@ -124,23 +213,15 @@ | ||
| foreach (var networkPrefab in prefabs) | ||
| { | ||
| if (AddPrefabRegistration(networkPrefab)) | ||
| { | ||
| m_Prefabs.Add(networkPrefab); | ||
| } | ||
| else | ||
| if (!InternalAddPrefab(networkPrefab)) | ||
| { | ||
| removeList?.Add(networkPrefab); | ||
| } | ||
| } | ||
| foreach (var networkPrefab in m_RuntimeAddedPrefabs) | ||
| { | ||
| if (AddPrefabRegistration(networkPrefab)) | ||
| { | ||
| m_Prefabs.Add(networkPrefab); | ||
| } | ||
| else | ||
| if (!InternalAddPrefab(networkPrefab)) | ||
| { | ||
| removeList?.Add(networkPrefab); | ||
| } | ||
| @@ -171,14 +252,12 @@ | ||
| /// </remarks> | ||
| public bool Add(NetworkPrefab networkPrefab) | ||
| { | ||
| if (AddPrefabRegistration(networkPrefab)) | ||
| var added = InternalAddPrefab(networkPrefab); | ||
| if (added) | ||
| { | ||
| m_Prefabs.Add(networkPrefab); | ||
| m_RuntimeAddedPrefabs.Add(networkPrefab); | ||
| return true; | ||
| } | ||
| return false; | ||
| return added; | ||
| } | ||
| /// <summary> | ||
| @@ -197,8 +276,7 @@ | ||
| { | ||
| throw new ArgumentNullException(nameof(prefab)); | ||
| } | ||
| m_Prefabs.Remove(prefab); | ||
| InternalRemovePrefab(prefab); | ||
| m_RuntimeAddedPrefabs.Remove(prefab); | ||
| OverrideToNetworkPrefab.Remove(prefab.TargetPrefabGlobalObjectIdHash); | ||
| NetworkPrefabOverrideLinks.Remove(prefab.SourcePrefabGlobalObjectIdHash); | ||
| @@ -294,14 +372,12 @@ | ||
| uint source = networkPrefab.SourcePrefabGlobalObjectIdHash; | ||
| uint target = networkPrefab.TargetPrefabGlobalObjectIdHash; | ||
| // Make sure the prefab isn't already registered. | ||
| if (NetworkPrefabOverrideLinks.ContainsKey(source)) | ||
| { | ||
| var networkObject = networkPrefab.Prefab.GetComponent<NetworkObject>(); | ||
| var nameOrHashOverride = networkPrefab.Override == NetworkPrefabOverride.Hash ? $"Hash: {networkPrefab.SourcePrefabGlobalObjectIdHash}" : networkPrefab.Prefab?.name; | ||
NoelStephensUnity marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // This should never happen, but in the case it somehow does log an error and remove the duplicate entry | ||
| Debug.LogError($"{nameof(NetworkPrefab)} ({networkObject.name}) has a duplicate {nameof(NetworkObject.GlobalObjectIdHash)} source entry value of: {source}!"); | ||
| Debug.LogError($"{nameof(NetworkPrefab)} ({nameOrHashOverride}) has a duplicate {nameof(NetworkObject.GlobalObjectIdHash)} source entry value of: {source}!"); | ||
| return false; | ||
| } | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.