diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs index f1638fb95d..0c83ecfc84 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs @@ -444,6 +444,10 @@ internal void InitializeVariables() } instance.SetNetworkBehaviour(this); + + var instanceNameProperty = fieldType.GetProperty(nameof(INetworkVariable.Name)); + instanceNameProperty?.SetValue(instance, sortedFields[i].Name); + NetworkVariableFields.Add(instance); } } diff --git a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkDictionary.cs b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkDictionary.cs index 807379fbe3..a6cf76b3d0 100644 --- a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkDictionary.cs +++ b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkDictionary.cs @@ -74,6 +74,12 @@ public NetworkDictionary(IDictionary value) m_Dictionary = value; } + /// + /// Gets or sets the name of the network variable's instance + /// (MemberInfo) where it was declared. + /// + public string Name { get; internal set; } + /// public void ResetDirty() { diff --git a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkList.cs b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkList.cs index b204cc8ba2..a391812395 100644 --- a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkList.cs +++ b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkList.cs @@ -72,6 +72,8 @@ public NetworkList(IList value) m_List = value; } + public string Name { get; internal set; } + /// public void ResetDirty() { diff --git a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkSet.cs b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkSet.cs index 429856d8d6..4b3675766c 100644 --- a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkSet.cs +++ b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/Collections/NetworkSet.cs @@ -73,6 +73,12 @@ public NetworkSet(ISet value) m_Set = value; } + /// + /// Gets or sets the name of the network variable's instance + /// (MemberInfo) where it was declared. + /// + public string Name { get; internal set; } + /// public void ResetDirty() { diff --git a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/INetworkVariable.cs b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/INetworkVariable.cs index 41f3538538..f999c44eda 100644 --- a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/INetworkVariable.cs +++ b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/INetworkVariable.cs @@ -8,6 +8,12 @@ namespace MLAPI.NetworkVariable /// public interface INetworkVariable { + /// + /// Gets or sets the name of the network variable's instance + /// (MemberInfo) where it was declared. + /// + string Name { get; } + /// /// Returns the name of the channel to be used for syncing /// @@ -73,6 +79,5 @@ public interface INetworkVariable /// /// The behaviour the container behaves to void SetNetworkBehaviour(NetworkBehaviour behaviour); - } } diff --git a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/NetworkVariable.cs b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/NetworkVariable.cs index 42016b0e77..f337e018f9 100644 --- a/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/NetworkVariable.cs +++ b/com.unity.multiplayer.mlapi/Runtime/NetworkVariable/NetworkVariable.cs @@ -90,6 +90,12 @@ public T Value private bool m_IsDirty = false; + /// + /// Gets or sets the name of the network variable's instance + /// (MemberInfo) where it was declared. + /// + public string Name { get; internal set; } + /// /// Sets whether or not the variable needs to be delta synced /// diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/Profiling.meta b/com.unity.multiplayer.mlapi/Tests/Runtime/Profiling.meta new file mode 100644 index 0000000000..617845c000 --- /dev/null +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/Profiling.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6b613c8dd4ba7d046b62971a3cb631ed +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/Profiling/NetworkVariableNameTests.cs b/com.unity.multiplayer.mlapi/Tests/Runtime/Profiling/NetworkVariableNameTests.cs new file mode 100644 index 0000000000..46f3f1130c --- /dev/null +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/Profiling/NetworkVariableNameTests.cs @@ -0,0 +1,52 @@ +using System; +using MLAPI.NetworkVariable; +using MLAPI.NetworkVariable.Collections; +using NUnit.Framework; +using UnityEngine; + +namespace MLAPI.RuntimeTests.Profiling +{ + public sealed class NetworkVariableNameTests + { + private NetworkVariableNameComponent m_NetworkVariableNameComponent; + + [SetUp] + public void SetUp() + { + NetworkManagerHelper.StartNetworkManager(out _); + + var gameObjectId = NetworkManagerHelper.AddGameNetworkObject(Guid.NewGuid().ToString()); + m_NetworkVariableNameComponent= NetworkManagerHelper.AddComponentToObject(gameObjectId); + NetworkManagerHelper.SpawnNetworkObject(gameObjectId); + } + + [TearDown] + public void TearDown() + { + NetworkManagerHelper.ShutdownNetworkManager(); + } + + [Test] + public void VerifyNetworkVariableNameInitialization() + { + // Properties have the following name format: "k__BackingField" + StringAssert.Contains(nameof(NetworkVariableNameComponent.NetworkVarString), m_NetworkVariableNameComponent.NetworkVarString.Name); + StringAssert.Contains(nameof(NetworkVariableNameComponent.NetworkVarSet), m_NetworkVariableNameComponent.NetworkVarSet.Name); + + // Fields have regular naming + Assert.AreEqual(nameof(NetworkVariableNameComponent.NetworkVarList), m_NetworkVariableNameComponent.NetworkVarList.Name); + Assert.AreEqual(nameof(NetworkVariableNameComponent.NetworkVarDictionary), m_NetworkVariableNameComponent.NetworkVarDictionary.Name); + } + + private class NetworkVariableNameComponent : NetworkBehaviour + { + public NetworkVariableString NetworkVarString { get; } = new NetworkVariableString(); + + public NetworkSet NetworkVarSet { get; } = new NetworkSet(); + + public NetworkList NetworkVarList = new NetworkList(); + + public NetworkDictionary NetworkVarDictionary = new NetworkDictionary(); + } + } +} \ No newline at end of file diff --git a/com.unity.multiplayer.mlapi/Tests/Runtime/Profiling/NetworkVariableNameTests.cs.meta b/com.unity.multiplayer.mlapi/Tests/Runtime/Profiling/NetworkVariableNameTests.cs.meta new file mode 100644 index 0000000000..6ddaf9dffb --- /dev/null +++ b/com.unity.multiplayer.mlapi/Tests/Runtime/Profiling/NetworkVariableNameTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e10a16550007020409234a1efb1fa383 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: