Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,6 @@ protected virtual bool OnIsServerAuthoritative()
private int[] m_TransitionHash;
private int[] m_AnimationHash;
private float[] m_LayerWeights;
private static byte[] s_EmptyArray = new byte[] { };
private List<int> m_ParametersToUpdate;
private RpcParams m_RpcParams;
private IGroupRpcTarget m_TargetGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ public enum AuthorityModes
/// <summary>
/// When set each state update will contain a state identifier
/// </summary>
internal static bool TrackStateUpdateId = false;
internal static bool TrackStateUpdateId;

/// <summary>
/// Enabled by default.
Expand Down Expand Up @@ -2221,13 +2221,11 @@ private bool CheckForStateChange(ref NetworkTransformState networkState, bool is
// values are applied.
var hasParentNetworkObject = false;

var parentNetworkObject = (NetworkObject)null;

// If the NetworkObject belonging to this NetworkTransform instance has a parent
// (i.e. this handles nested NetworkTransforms under a parent at some layer above)
if (NetworkObject.transform.parent != null)
{
parentNetworkObject = NetworkObject.transform.parent.GetComponent<NetworkObject>();
var parentNetworkObject = NetworkObject.transform.parent.GetComponent<NetworkObject>();

// In-scene placed NetworkObjects parented under a GameObject with no
// NetworkObject preserve their lossyScale when synchronizing.
Expand Down Expand Up @@ -4694,7 +4692,7 @@ internal static void UpdateNetworkTick(NetworkManager networkManager)
/// The default value is 1 tick (plus the tick latency). When running on a local network, reducing this to 0 is recommended.<br />
/// <see cref="NetworkTimeSystem.TickLatency"/>
/// </remarks>
public static int InterpolationBufferTickOffset = 0;
public static int InterpolationBufferTickOffset;
internal static float GetTickLatency(NetworkManager networkManager)
{
if (networkManager.IsListening)
Expand Down Expand Up @@ -4800,6 +4798,21 @@ public NetworkTransformTickRegistration(NetworkManager networkManager)
}
}
private static int s_TickSynchPosition;

#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad()
{
CurrentTick = 0;
TrackStateUpdateId = false;
AssignDefaultInterpolationType = false;
DefaultInterpolationType = default;
s_NetworkTickRegistration = new Dictionary<NetworkManager, NetworkTransformTickRegistration>();
InterpolationBufferTickOffset = 0;
s_TickSynchPosition = 0;
}
#endif

private int m_NextTickSync;

internal void RegisterForTickSynchronization()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public static class QuaternionCompressor

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

/// <summary>
/// Compresses a Quaternion into an unsigned integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct ContactEventHandlerInfo
/// </summary>
public bool ProvideNonRigidBodyContactEvents;
/// <summary>
/// When set to true, the <see cref="RigidbodyContactEventManager"/> will prioritize invoking <see cref="IContactEventHandler.ContactEvent(ulong, Vector3, Rigidbody, Vector3, bool, Vector3)"/> <br /></br>
/// When set to true, the <see cref="RigidbodyContactEventManager"/> will prioritize invoking <see cref="IContactEventHandler.ContactEvent(ulong, Vector3, Rigidbody, Vector3, bool, Vector3)"/> <br />
/// 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 />
/// will assure <see cref="IContactEventHandler.ContactEvent(ulong, Vector3, Rigidbody, Vector3, bool, Vector3)"/> is only invoked on the authoritative side.
/// </summary>
Expand Down Expand Up @@ -76,6 +76,10 @@ public interface IContactEventHandlerWithInfo : IContactEventHandler
public class RigidbodyContactEventManager : MonoBehaviour
{
public static RigidbodyContactEventManager Instance { get; private set; }
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => Instance = null;
#endif

private struct JobResultStruct
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,31 @@ private set
}
private static CommandLineOptions s_Instance;

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void RuntimeInitializeOnLoad() => Instance = new CommandLineOptions();

// Contains the current application instance domain's command line arguments
internal static List<string> CommandLineArguments = new List<string>();

// Invoked upon application start, after scene load
[RuntimeInitializeOnLoadMethod]
private static void ParseCommandLineArguments()
private static List<string> s_CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad()
{
Instance = new CommandLineOptions();
s_Instance = new CommandLineOptions();
// Get all the command line arguments to be parsed later and/or modified
// prior to being parsed (for testing purposes).
CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());
s_CommandLineArguments = new List<string>(Environment.GetCommandLineArgs());
}
#endif

/// <summary>
/// Returns the value of an argument or null if there the argument is not present
/// Returns the value of an argument or null if the argument is not present
/// </summary>
/// <param name="arg">The name of the argument</param>
/// <returns><see cref="string"/>Value of the command line argument passed in.</returns>
public string GetArg(string arg)
{
var argIndex = CommandLineArguments.IndexOf(arg);
if (argIndex >= 0 && argIndex < CommandLineArguments.Count - 1)
var argIndex = s_CommandLineArguments.IndexOf(arg);
if (argIndex >= 0 && argIndex < s_CommandLineArguments.Count - 1)
{
return CommandLineArguments[argIndex + 1];
return s_CommandLineArguments[argIndex + 1];
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEngine;
#endif

namespace Unity.Netcode
{
Expand All @@ -14,6 +17,10 @@ internal static class ComponentFactory
internal delegate object CreateObjectDelegate(NetworkManager networkManager);

private static Dictionary<Type, CreateObjectDelegate> s_Delegates = new Dictionary<Type, CreateObjectDelegate>();
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => s_Delegates = new Dictionary<Type, CreateObjectDelegate>();
#endif

/// <summary>
/// Instantiates an instance of a given interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1333,8 +1333,6 @@ internal void NetworkVariableUpdate(ulong targetClientId, bool forceSend = false
}
}

internal static bool LogSentVariableUpdateMessage;

private bool CouldHaveDirtyNetworkVariables()
{
// TODO: There should be a better way by reading one dirty variable vs. 'n'
Expand Down
13 changes: 13 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,19 @@ internal abstract class NetcodeAnalytics
internal delegate void ResetNetworkManagerDelegate(NetworkManager manager);

internal static ResetNetworkManagerDelegate OnNetworkManagerReset;
//We already are in an #if UNITY_ENGINE def
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad()
{
Singleton = null;
OnInstantiated = null;
OnDestroying = null;
OnSingletonReady = null;
OnNetworkManagerReset = null;
IsDistributedAuthority = false;
s_SerializedType = new List<Type>();
DisableNotOptimizedSerializedType = false;
}

private void Reset()
{
Expand Down
9 changes: 9 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,11 @@ internal void SetIsDestroying()
IsDestroying = true;
}

private void OnDisable()
{
SceneManager.activeSceneChanged -= CurrentlyActiveSceneChanged;
}

private void OnDestroy()
{
// Apply the is destroying flag
Expand Down Expand Up @@ -2506,6 +2511,10 @@ private void OnTransformParentChanged()
// If you couldn't find your parent, we put you into OrphanChildren set and every time we spawn another NetworkObject locally due to replication,
// we call CheckOrphanChildren() method and quickly iterate over OrphanChildren set and see if we can reparent/adopt one.
internal static HashSet<NetworkObject> OrphanChildren = new HashSet<NetworkObject>();
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => OrphanChildren = new HashSet<NetworkObject>();
#endif

internal bool ApplyNetworkParenting(bool removeParent = false, bool ignoreNotSpawned = false, bool orphanedChildPass = false, bool enableNotification = true)
{
Expand Down
36 changes: 23 additions & 13 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ public enum NetworkUpdateStage : byte
/// </summary>
public static class NetworkUpdateLoop
{
private static Dictionary<NetworkUpdateStage, HashSet<INetworkUpdateSystem>> s_UpdateSystem_Sets;
private static Dictionary<NetworkUpdateStage, INetworkUpdateSystem[]> s_UpdateSystem_Arrays;
private const int k_UpdateSystem_InitialArrayCapacity = 1024;
private static Dictionary<NetworkUpdateStage, HashSet<INetworkUpdateSystem>> s_UpdateSystemSets;
private static Dictionary<NetworkUpdateStage, INetworkUpdateSystem[]> s_UpdateSystemArrays;
private const int k_UpdateSystemInitialArrayCapacity = 1024;

static NetworkUpdateLoop()
{
s_UpdateSystem_Sets = new Dictionary<NetworkUpdateStage, HashSet<INetworkUpdateSystem>>();
s_UpdateSystem_Arrays = new Dictionary<NetworkUpdateStage, INetworkUpdateSystem[]>();
s_UpdateSystemSets = new Dictionary<NetworkUpdateStage, HashSet<INetworkUpdateSystem>>();
s_UpdateSystemArrays = new Dictionary<NetworkUpdateStage, INetworkUpdateSystem[]>();

foreach (NetworkUpdateStage updateStage in Enum.GetValues(typeof(NetworkUpdateStage)))
{
s_UpdateSystem_Sets.Add(updateStage, new HashSet<INetworkUpdateSystem>());
s_UpdateSystem_Arrays.Add(updateStage, new INetworkUpdateSystem[k_UpdateSystem_InitialArrayCapacity]);
s_UpdateSystemSets.Add(updateStage, new HashSet<INetworkUpdateSystem>());
s_UpdateSystemArrays.Add(updateStage, new INetworkUpdateSystem[k_UpdateSystemInitialArrayCapacity]);
}
}

Expand All @@ -105,19 +105,19 @@ public static void RegisterAllNetworkUpdates(this INetworkUpdateSystem updateSys
/// <param name="updateStage">The <see cref="NetworkUpdateStage"/> being registered for the <see cref="INetworkUpdateSystem"/> implementation</param>
public static void RegisterNetworkUpdate(this INetworkUpdateSystem updateSystem, NetworkUpdateStage updateStage = NetworkUpdateStage.Update)
{
var sysSet = s_UpdateSystem_Sets[updateStage];
var sysSet = s_UpdateSystemSets[updateStage];
if (!sysSet.Contains(updateSystem))
{
sysSet.Add(updateSystem);

int setLen = sysSet.Count;
var sysArr = s_UpdateSystem_Arrays[updateStage];
var sysArr = s_UpdateSystemArrays[updateStage];
int arrLen = sysArr.Length;

if (setLen > arrLen)
{
// double capacity
sysArr = s_UpdateSystem_Arrays[updateStage] = new INetworkUpdateSystem[arrLen *= 2];
sysArr = s_UpdateSystemArrays[updateStage] = new INetworkUpdateSystem[arrLen *= 2];
}

sysSet.CopyTo(sysArr);
Expand Down Expand Up @@ -149,13 +149,13 @@ public static void UnregisterAllNetworkUpdates(this INetworkUpdateSystem updateS
/// <param name="updateStage">The <see cref="NetworkUpdateStage"/> to be deregistered from the <see cref="INetworkUpdateSystem"/> implementation</param>
public static void UnregisterNetworkUpdate(this INetworkUpdateSystem updateSystem, NetworkUpdateStage updateStage = NetworkUpdateStage.Update)
{
var sysSet = s_UpdateSystem_Sets[updateStage];
var sysSet = s_UpdateSystemSets[updateStage];
if (sysSet.Contains(updateSystem))
{
sysSet.Remove(updateSystem);

int setLen = sysSet.Count;
var sysArr = s_UpdateSystem_Arrays[updateStage];
var sysArr = s_UpdateSystemArrays[updateStage];
int arrLen = sysArr.Length;

sysSet.CopyTo(sysArr);
Expand All @@ -177,7 +177,7 @@ internal static void RunNetworkUpdateStage(NetworkUpdateStage updateStage)
{
UpdateStage = updateStage;

var sysArr = s_UpdateSystem_Arrays[updateStage];
var sysArr = s_UpdateSystemArrays[updateStage];
int arrLen = sysArr.Length;
for (int curIdx = 0; curIdx < arrLen; curIdx++)
{
Expand Down Expand Up @@ -291,6 +291,16 @@ public static PlayerLoopSystem CreateLoopSystem()
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void Initialize()
{
#if UNITY_EDITOR
s_UpdateSystemSets = new Dictionary<NetworkUpdateStage, HashSet<INetworkUpdateSystem>>();
s_UpdateSystemArrays = new Dictionary<NetworkUpdateStage, INetworkUpdateSystem[]>();
foreach (NetworkUpdateStage updateStage in Enum.GetValues(typeof(NetworkUpdateStage)))
{
s_UpdateSystemSets.Add(updateStage, new HashSet<INetworkUpdateSystem>());
s_UpdateSystemArrays.Add(updateStage, new INetworkUpdateSystem[k_UpdateSystemInitialArrayCapacity]);
}
UpdateStage = default;
#endif
UnregisterLoopSystems();
RegisterLoopSystems();
}
Expand Down
4 changes: 4 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Logging/NetworkLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public static class NetworkLog
public static void LogErrorServer(string message) => LogServer(message, LogType.Error);

internal static NetworkManager NetworkManagerOverride;
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => NetworkManagerOverride = null;
#endif

private static void LogServer(string message, LogType logType)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Generic;
using Unity.Collections;
#if UNITY_EDITOR
using UnityEngine;
#endif

namespace Unity.Netcode
{
Expand Down Expand Up @@ -96,6 +99,10 @@ public virtual unsafe void CleanupStaleTriggers()
/// Used for testing purposes
/// </summary>
internal static bool IncludeMessageType = true;
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => IncludeMessageType = true;
#endif

private string GetWarningMessage(IDeferredNetworkMessageManager.TriggerType triggerType, ulong key, TriggerInfo triggerInfo, float spawnTimeout)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
#endif

Expand Down Expand Up @@ -53,6 +54,10 @@ internal struct ILPPMessageProvider : INetworkMessageProvider

// Enable this for integration tests that need no message types defined
internal static bool IntegrationTestNoMessages;
#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticsOnLoad() => IntegrationTestNoMessages = false;
#endif

/// <summary>
/// Returns a table of message type to NetworkMessageTypes enum value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

namespace Unity.Netcode
{
/// <summary>
Expand Down
Loading