Skip to content

fix: do not override GlobalObjectIdHash in Editor #744

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
merged 3 commits into from
Apr 16, 2021
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
6 changes: 4 additions & 2 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ public ulong LocalClientId

internal static event Action OnSingletonReady;

#if UNITY_EDITOR
internal static bool IsTestRun = false;

private void OnValidate()
{
if (NetworkConfig == null)
Expand All @@ -226,7 +229,6 @@ private void OnValidate()
}

NetworkConfig.RegisteredScenes.Add(activeSceneName);
#if UNITY_EDITOR
UnityEditor.EditorApplication.delayCall += () =>
{
if (!UnityEditor.EditorApplication.isPlaying)
Expand All @@ -235,7 +237,6 @@ private void OnValidate()
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(activeScene);
}
};
#endif
}

for (int i = 0; i < NetworkConfig.NetworkPrefabs.Count; i++)
Expand Down Expand Up @@ -272,6 +273,7 @@ private void OnValidate()
var networkPrefab = NetworkConfig.NetworkPrefabs.FirstOrDefault(x => x.IsPlayer);
NetworkConfig.PlayerPrefabHash = networkPrefab?.Hash ?? (uint)0;
}
#endif

private void Init(bool server)
{
Expand Down
9 changes: 7 additions & 2 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using MLAPI.Logging;
using MLAPI.Messaging;
using MLAPI.Serialization.Pooled;
using MLAPI.Spawning;
using MLAPI.Transports;
using UnityEngine;

Expand All @@ -27,6 +26,12 @@ public sealed class NetworkObject : MonoBehaviour
#if UNITY_EDITOR
private void OnValidate()
{
if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode && !NetworkManager.IsTestRun)
{
// do NOT override GlobalObjectIdHash while getting into PlayMode in the Editor
return;
}

var globalObjectIdString = UnityEditor.GlobalObjectId.GetGlobalObjectIdSlow(this).ToString();
GlobalObjectIdHash = XXHash.Hash32(globalObjectIdString);
}
Expand Down Expand Up @@ -333,7 +338,7 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI

private void OnDestroy()
{
if (NetworkManager.Singleton != null && NetworkManager.Singleton.SpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId))
if (NetworkManager.Singleton != null && NetworkManager.Singleton.SpawnManager != null && NetworkManager.Singleton.SpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you found the same bug I found earlier where the SpawnManager no longer exists but objects are still being destroyed.

{
NetworkManager.Singleton.SpawnManager.OnDestroyObject(NetworkObjectId, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
[UnitySetUp]
public IEnumerator Setup()
{
NetworkManager.IsTestRun = true;

SceneManager.sceneLoaded += OnSceneLoaded;

var execAssembly = Assembly.GetExecutingAssembly();
Expand All @@ -43,6 +45,8 @@ public IEnumerator Teardown()
{
yield return SceneManager.UnloadSceneAsync(m_TestScene);
}

NetworkManager.IsTestRun = false;
}

[Test]
Expand Down