From acfb50f827fc293227275a3e1386e1a64639f6ed Mon Sep 17 00:00:00 2001 From: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Mon, 14 Jun 2021 14:36:21 -0500 Subject: [PATCH 1/3] fix Resolves the following github issue: NetworkPrefabs container's elements invalidated in the NetworkManager after relaunching Unity Project #904 https://github.com/Unity-Technologies/com.unity.multiplayer.mlapi/issues/904 Checking to make sure the scene is dirty and that the asset database is not currently updating resolves this issue. --- .../Runtime/Core/NetworkManager.cs | 82 ++++++++++--------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs index de2d275029..2bbf73b9da 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs @@ -261,54 +261,58 @@ private void OnValidate() }; } - // During OnValidate we will always clear out NetworkPrefabOverrideLinks and rebuild it - NetworkConfig.NetworkPrefabOverrideLinks.Clear(); - - // Check network prefabs and assign to dictionary for quick look up - for (int i = 0; i < NetworkConfig.NetworkPrefabs.Count; i++) + // If the scene is dirty and the asset database is not updating then we update NetworkPrefab information + if (activeScene.isDirty && !UnityEditor.EditorApplication.isUpdating) { - if (NetworkConfig.NetworkPrefabs[i] != null && NetworkConfig.NetworkPrefabs[i].Prefab != null) + // During OnValidate we will always clear out NetworkPrefabOverrideLinks and rebuild it + NetworkConfig.NetworkPrefabOverrideLinks.Clear(); + + // Check network prefabs and assign to dictionary for quick look up + for (int i = 0; i < NetworkConfig.NetworkPrefabs.Count; i++) { - var networkObject = NetworkConfig.NetworkPrefabs[i].Prefab.GetComponent(); - if (networkObject == null) + if (NetworkConfig.NetworkPrefabs[i] != null && NetworkConfig.NetworkPrefabs[i].Prefab != null) { - if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) + var networkObject = NetworkConfig.NetworkPrefabs[i].Prefab.GetComponent(); + if (networkObject == null) { - NetworkLog.LogWarning($"{nameof(NetworkPrefab)} [{i}] does not have a {nameof(NetworkObject)} component"); + if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) + { + NetworkLog.LogWarning($"{nameof(NetworkPrefab)} [{i}] does not have a {nameof(NetworkObject)} component"); + } } - } - else - { - // Default to the standard NetworkPrefab.Prefab's NetworkObject first - var globalObjectIdHash = networkObject.GlobalObjectIdHash; - - // Now check to see if it has an override - switch (NetworkConfig.NetworkPrefabs[i].Override) + else { - case NetworkPrefabOverride.Prefab: - { - if (NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride == null && NetworkConfig.NetworkPrefabs[i].Prefab != null) + // Default to the standard NetworkPrefab.Prefab's NetworkObject first + var globalObjectIdHash = networkObject.GlobalObjectIdHash; + + // Now check to see if it has an override + switch (NetworkConfig.NetworkPrefabs[i].Override) + { + case NetworkPrefabOverride.Prefab: { - NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride = NetworkConfig.NetworkPrefabs[i].Prefab; + if (NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride == null && NetworkConfig.NetworkPrefabs[i].Prefab != null) + { + NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride = NetworkConfig.NetworkPrefabs[i].Prefab; + } + globalObjectIdHash = NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride.GetComponent().GlobalObjectIdHash; } - globalObjectIdHash = NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride.GetComponent().GlobalObjectIdHash; - } - break; - case NetworkPrefabOverride.Hash: - globalObjectIdHash = NetworkConfig.NetworkPrefabs[i].SourceHashToOverride; - break; - } + break; + case NetworkPrefabOverride.Hash: + globalObjectIdHash = NetworkConfig.NetworkPrefabs[i].SourceHashToOverride; + break; + } - // Add to the NetworkPrefabOverrideLinks or handle a new (blank) entries - if (!NetworkConfig.NetworkPrefabOverrideLinks.ContainsKey(globalObjectIdHash)) - { - NetworkConfig.NetworkPrefabOverrideLinks.Add(globalObjectIdHash, NetworkConfig.NetworkPrefabs[i]); - } - else - { - // Duplicate entries can happen when adding a new entry into a list of existing entries - // Either this is user error or a new entry, either case we replace it with a new, blank, NetworkPrefab under this condition - NetworkConfig.NetworkPrefabs[i] = new NetworkPrefab(); + // Add to the NetworkPrefabOverrideLinks or handle a new (blank) entries + if (!NetworkConfig.NetworkPrefabOverrideLinks.ContainsKey(globalObjectIdHash)) + { + NetworkConfig.NetworkPrefabOverrideLinks.Add(globalObjectIdHash, NetworkConfig.NetworkPrefabs[i]); + } + else + { + // Duplicate entries can happen when adding a new entry into a list of existing entries + // Either this is user error or a new entry, either case we replace it with a new, blank, NetworkPrefab under this condition + NetworkConfig.NetworkPrefabs[i] = new NetworkPrefab(); + } } } } From b8d4a0b42ec99722301c895d1397a000f84e4c1d Mon Sep 17 00:00:00 2001 From: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Mon, 14 Jun 2021 14:37:49 -0500 Subject: [PATCH 2/3] style Updated the comment for better clarity and assuring we have at least two commits in order to pass Yamato requirement of >1 commit --- com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs index 2bbf73b9da..ac0f132721 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs @@ -261,7 +261,7 @@ private void OnValidate() }; } - // If the scene is dirty and the asset database is not updating then we update NetworkPrefab information + // If the scene is dirty and the asset database is not currently updating then we can update NetworkPrefab information if (activeScene.isDirty && !UnityEditor.EditorApplication.isUpdating) { // During OnValidate we will always clear out NetworkPrefabOverrideLinks and rebuild it From b5c610019c02b7c7a27969ceb9a0ec8a60c4da79 Mon Sep 17 00:00:00 2001 From: NoelStephensUnity <73188597+NoelStephensUnity@users.noreply.github.com> Date: Tue, 15 Jun 2021 17:20:39 -0500 Subject: [PATCH 3/3] refactor Updated logic to exit OnValidate as opposed to wrapping the NetworkPrefab related code for easier readability. --- .../Runtime/Core/NetworkManager.cs | 88 ++++++++++--------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs index ac0f132721..03bbf3eae0 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs @@ -261,62 +261,64 @@ private void OnValidate() }; } - // If the scene is dirty and the asset database is not currently updating then we can update NetworkPrefab information - if (activeScene.isDirty && !UnityEditor.EditorApplication.isUpdating) + // If the scene is not dirty or the asset database is currently updating then we can skip updating the NetworkPrefab information + if (!activeScene.isDirty || UnityEditor.EditorApplication.isUpdating) { - // During OnValidate we will always clear out NetworkPrefabOverrideLinks and rebuild it - NetworkConfig.NetworkPrefabOverrideLinks.Clear(); + return; + } + + // During OnValidate we will always clear out NetworkPrefabOverrideLinks and rebuild it + NetworkConfig.NetworkPrefabOverrideLinks.Clear(); - // Check network prefabs and assign to dictionary for quick look up - for (int i = 0; i < NetworkConfig.NetworkPrefabs.Count; i++) + // Check network prefabs and assign to dictionary for quick look up + for (int i = 0; i < NetworkConfig.NetworkPrefabs.Count; i++) + { + if (NetworkConfig.NetworkPrefabs[i] != null && NetworkConfig.NetworkPrefabs[i].Prefab != null) { - if (NetworkConfig.NetworkPrefabs[i] != null && NetworkConfig.NetworkPrefabs[i].Prefab != null) + var networkObject = NetworkConfig.NetworkPrefabs[i].Prefab.GetComponent(); + if (networkObject == null) { - var networkObject = NetworkConfig.NetworkPrefabs[i].Prefab.GetComponent(); - if (networkObject == null) + if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) { - if (NetworkLog.CurrentLogLevel <= LogLevel.Normal) - { - NetworkLog.LogWarning($"{nameof(NetworkPrefab)} [{i}] does not have a {nameof(NetworkObject)} component"); - } + NetworkLog.LogWarning($"{nameof(NetworkPrefab)} [{i}] does not have a {nameof(NetworkObject)} component"); } - else - { - // Default to the standard NetworkPrefab.Prefab's NetworkObject first - var globalObjectIdHash = networkObject.GlobalObjectIdHash; + } + else + { + // Default to the standard NetworkPrefab.Prefab's NetworkObject first + var globalObjectIdHash = networkObject.GlobalObjectIdHash; - // Now check to see if it has an override - switch (NetworkConfig.NetworkPrefabs[i].Override) - { - case NetworkPrefabOverride.Prefab: + // Now check to see if it has an override + switch (NetworkConfig.NetworkPrefabs[i].Override) + { + case NetworkPrefabOverride.Prefab: + { + if (NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride == null && NetworkConfig.NetworkPrefabs[i].Prefab != null) { - if (NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride == null && NetworkConfig.NetworkPrefabs[i].Prefab != null) - { - NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride = NetworkConfig.NetworkPrefabs[i].Prefab; - } - globalObjectIdHash = NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride.GetComponent().GlobalObjectIdHash; + NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride = NetworkConfig.NetworkPrefabs[i].Prefab; } - break; - case NetworkPrefabOverride.Hash: - globalObjectIdHash = NetworkConfig.NetworkPrefabs[i].SourceHashToOverride; - break; - } + globalObjectIdHash = NetworkConfig.NetworkPrefabs[i].SourcePrefabToOverride.GetComponent().GlobalObjectIdHash; + } + break; + case NetworkPrefabOverride.Hash: + globalObjectIdHash = NetworkConfig.NetworkPrefabs[i].SourceHashToOverride; + break; + } - // Add to the NetworkPrefabOverrideLinks or handle a new (blank) entries - if (!NetworkConfig.NetworkPrefabOverrideLinks.ContainsKey(globalObjectIdHash)) - { - NetworkConfig.NetworkPrefabOverrideLinks.Add(globalObjectIdHash, NetworkConfig.NetworkPrefabs[i]); - } - else - { - // Duplicate entries can happen when adding a new entry into a list of existing entries - // Either this is user error or a new entry, either case we replace it with a new, blank, NetworkPrefab under this condition - NetworkConfig.NetworkPrefabs[i] = new NetworkPrefab(); - } + // Add to the NetworkPrefabOverrideLinks or handle a new (blank) entries + if (!NetworkConfig.NetworkPrefabOverrideLinks.ContainsKey(globalObjectIdHash)) + { + NetworkConfig.NetworkPrefabOverrideLinks.Add(globalObjectIdHash, NetworkConfig.NetworkPrefabs[i]); + } + else + { + // Duplicate entries can happen when adding a new entry into a list of existing entries + // Either this is user error or a new entry, either case we replace it with a new, blank, NetworkPrefab under this condition + NetworkConfig.NetworkPrefabs[i] = new NetworkPrefab(); } } } - } + } } #endif