Skip to content

Commit c6f0d4b

Browse files
committed
fix: reset static fields for Fast Enter Play Mode
1 parent a31fee7 commit c6f0d4b

28 files changed

Lines changed: 185 additions & 63 deletions

com.unity.netcode.gameobjects/Runtime/Components/NetworkAnimator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,6 @@ protected virtual bool OnIsServerAuthoritative()
706706
private int[] m_TransitionHash;
707707
private int[] m_AnimationHash;
708708
private float[] m_LayerWeights;
709-
private static byte[] s_EmptyArray = new byte[] { };
710709
private List<int> m_ParametersToUpdate;
711710
private RpcParams m_RpcParams;
712711
private IGroupRpcTarget m_TargetGroup;

com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,13 +2221,11 @@ private bool CheckForStateChange(ref NetworkTransformState networkState, bool is
22212221
// values are applied.
22222222
var hasParentNetworkObject = false;
22232223

2224-
var parentNetworkObject = (NetworkObject)null;
2225-
22262224
// If the NetworkObject belonging to this NetworkTransform instance has a parent
22272225
// (i.e. this handles nested NetworkTransforms under a parent at some layer above)
22282226
if (NetworkObject.transform.parent != null)
22292227
{
2230-
parentNetworkObject = NetworkObject.transform.parent.GetComponent<NetworkObject>();
2228+
var parentNetworkObject = NetworkObject.transform.parent.GetComponent<NetworkObject>();
22312229

22322230
// In-scene placed NetworkObjects parented under a GameObject with no
22332231
// NetworkObject preserve their lossyScale when synchronizing.
@@ -4800,6 +4798,21 @@ public NetworkTransformTickRegistration(NetworkManager networkManager)
48004798
}
48014799
}
48024800
private static int s_TickSynchPosition;
4801+
4802+
#if UNITY_EDITOR
4803+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
4804+
private static void ResetStaticsOnLoad()
4805+
{
4806+
CurrentTick = 0;
4807+
TrackStateUpdateId = false;
4808+
AssignDefaultInterpolationType = false;
4809+
DefaultInterpolationType = default;
4810+
s_NetworkTickRegistration = new Dictionary<NetworkManager, NetworkTransformTickRegistration>();
4811+
InterpolationBufferTickOffset = 0;
4812+
s_TickSynchPosition = 0;
4813+
}
4814+
#endif
4815+
48034816
private int m_NextTickSync;
48044817

48054818
internal void RegisterForTickSynchronization()

com.unity.netcode.gameobjects/Runtime/Components/QuaternionCompressor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public static class QuaternionCompressor
4444

4545
// Used to store the absolute value of the 4 quaternion elements
4646
private static Quaternion s_QuatAbsValues = Quaternion.identity;
47+
#if UNITY_EDITOR
48+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
49+
private static void ResetStaticsOnLoad() => s_QuatAbsValues = Quaternion.identity;
50+
#endif
4751

4852
/// <summary>
4953
/// Compresses a Quaternion into an unsigned integer

com.unity.netcode.gameobjects/Runtime/Components/RigidbodyContactEventManager.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public struct ContactEventHandlerInfo
2020
/// </summary>
2121
public bool ProvideNonRigidBodyContactEvents;
2222
/// <summary>
23-
/// When set to true, the <see cref="RigidbodyContactEventManager"/> will prioritize invoking <see cref="IContactEventHandler.ContactEvent(ulong, Vector3, Rigidbody, Vector3, bool, Vector3)"/> <br /></br>
23+
/// When set to true, the <see cref="RigidbodyContactEventManager"/> will prioritize invoking <see cref="IContactEventHandler.ContactEvent(ulong, Vector3, Rigidbody, Vector3, bool, Vector3)"/> <br />
2424
/// if it is the 2nd colliding body in the contact pair being processed. With distributed authority, setting this value to true when a <see cref="NetworkObject"/> is owned by the local client <br />
2525
/// will assure <see cref="IContactEventHandler.ContactEvent(ulong, Vector3, Rigidbody, Vector3, bool, Vector3)"/> is only invoked on the authoritative side.
2626
/// </summary>
@@ -76,6 +76,10 @@ public interface IContactEventHandlerWithInfo : IContactEventHandler
7676
public class RigidbodyContactEventManager : MonoBehaviour
7777
{
7878
public static RigidbodyContactEventManager Instance { get; private set; }
79+
#if UNITY_EDITOR
80+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
81+
private static void ResetStaticsOnLoad() => Instance = null;
82+
#endif
7983

8084
private struct JobResultStruct
8185
{

com.unity.netcode.gameobjects/Runtime/Configuration/CommandLineOptions.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,32 @@ private set
3030
}
3131
private static CommandLineOptions s_Instance;
3232

33-
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
34-
private static void RuntimeInitializeOnLoad() => Instance = new CommandLineOptions();
35-
3633
// Contains the current application instance domain's command line arguments
37-
internal static List<string> CommandLineArguments = new List<string>();
34+
private static List<string> s_CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());
3835

39-
// Invoked upon application start, after scene load
40-
[RuntimeInitializeOnLoadMethod]
41-
private static void ParseCommandLineArguments()
36+
#if UNITY_EDITOR
37+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
38+
private static void ResetStaticsOnLoad()
4239
{
40+
Instance = new CommandLineOptions();
41+
s_Instance = new CommandLineOptions();
4342
// Get all the command line arguments to be parsed later and/or modified
4443
// prior to being parsed (for testing purposes).
45-
CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());
44+
s_CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());
4645
}
46+
#endif
4747

4848
/// <summary>
49-
/// Returns the value of an argument or null if there the argument is not present
49+
/// Returns the value of an argument or null if the argument is not present
5050
/// </summary>
5151
/// <param name="arg">The name of the argument</param>
5252
/// <returns><see cref="string"/>Value of the command line argument passed in.</returns>
5353
public string GetArg(string arg)
5454
{
55-
var argIndex = CommandLineArguments.IndexOf(arg);
56-
if (argIndex >= 0 && argIndex < CommandLineArguments.Count - 1)
55+
var argIndex = s_CommandLineArguments.IndexOf(arg);
56+
if (argIndex >= 0 && argIndex < s_CommandLineArguments.Count - 1)
5757
{
58-
return CommandLineArguments[argIndex + 1];
58+
return s_CommandLineArguments[argIndex + 1];
5959
}
6060
return null;
6161
}

com.unity.netcode.gameobjects/Runtime/Core/ComponentFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using UnityEngine;
34

45
namespace Unity.Netcode
56
{
@@ -14,6 +15,10 @@ internal static class ComponentFactory
1415
internal delegate object CreateObjectDelegate(NetworkManager networkManager);
1516

1617
private static Dictionary<Type, CreateObjectDelegate> s_Delegates = new Dictionary<Type, CreateObjectDelegate>();
18+
#if UNITY_EDITOR
19+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
20+
private static void ResetStaticsOnLoad() => s_Delegates = new Dictionary<Type, CreateObjectDelegate>();
21+
#endif
1722

1823
/// <summary>
1924
/// Instantiates an instance of a given interface

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,8 +1297,6 @@ internal void NetworkVariableUpdate(ulong targetClientId, bool forceSend = false
12971297
}
12981298
}
12991299

1300-
internal static bool LogSentVariableUpdateMessage;
1301-
13021300
private bool CouldHaveDirtyNetworkVariables()
13031301
{
13041302
// TODO: There should be a better way by reading one dirty variable vs. 'n'

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,19 @@ internal abstract class NetcodeAnalytics
18091809
internal delegate void ResetNetworkManagerDelegate(NetworkManager manager);
18101810

18111811
internal static ResetNetworkManagerDelegate OnNetworkManagerReset;
1812+
//We already are in an #if UNITY_ENGINE def
1813+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
1814+
private static void ResetStaticsOnLoad()
1815+
{
1816+
Singleton = null;
1817+
OnInstantiated = null;
1818+
OnDestroying = null;
1819+
OnSingletonReady = null;
1820+
OnNetworkManagerReset = null;
1821+
IsDistributedAuthority = false;
1822+
s_SerializedType = new List<Type>();
1823+
DisableNotOptimizedSerializedType = false;
1824+
}
18121825

18131826
private void Reset()
18141827
{

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,11 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI
16881688
}
16891689
}
16901690

1691+
private void OnDisable()
1692+
{
1693+
SceneManager.activeSceneChanged -= CurrentlyActiveSceneChanged;
1694+
}
1695+
16911696
private void OnDestroy()
16921697
{
16931698
var networkManager = NetworkManager;
@@ -2449,6 +2454,10 @@ private void OnTransformParentChanged()
24492454
// If you couldn't find your parent, we put you into OrphanChildren set and every time we spawn another NetworkObject locally due to replication,
24502455
// we call CheckOrphanChildren() method and quickly iterate over OrphanChildren set and see if we can reparent/adopt one.
24512456
internal static HashSet<NetworkObject> OrphanChildren = new HashSet<NetworkObject>();
2457+
#if UNITY_EDITOR
2458+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
2459+
private static void ResetStaticsOnLoad() => OrphanChildren = new HashSet<NetworkObject>();
2460+
#endif
24522461

24532462
internal bool ApplyNetworkParenting(bool removeParent = false, bool ignoreNotSpawned = false, bool orphanedChildPass = false, bool enableNotification = true)
24542463
{

com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ public static PlayerLoopSystem CreateLoopSystem()
291291
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
292292
private static void Initialize()
293293
{
294+
#if UNITY_EDITOR
295+
s_UpdateSystem_Sets = new Dictionary<NetworkUpdateStage, HashSet<INetworkUpdateSystem>>();
296+
s_UpdateSystem_Arrays = new Dictionary<NetworkUpdateStage, INetworkUpdateSystem[]>();
297+
foreach (NetworkUpdateStage updateStage in Enum.GetValues(typeof(NetworkUpdateStage)))
298+
{
299+
s_UpdateSystem_Sets.Add(updateStage, new HashSet<INetworkUpdateSystem>());
300+
s_UpdateSystem_Arrays.Add(updateStage, new INetworkUpdateSystem[k_UpdateSystem_InitialArrayCapacity]);
301+
}
302+
UpdateStage = default;
303+
#endif
294304
UnregisterLoopSystems();
295305
RegisterLoopSystems();
296306
}

0 commit comments

Comments
 (0)