Skip to content

Commit 280320f

Browse files
committed
small changes
1 parent 92ee7f7 commit 280320f

6 files changed

Lines changed: 119 additions & 105 deletions

File tree

com.unity.netcode.gameobjects/Tests/Editor/DocumentationCodeSamples/Serialization.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.netcode.gameobjects/Tests/Editor/DocumentationCodeSamples/Serialization/SerializationCustomization.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using NUnit.Framework;
22
using Unity.Collections;
3-
using UnityEngine;
43

54
namespace Unity.Netcode.EditorTests.Documentation.Serialization
65
{
@@ -27,7 +26,7 @@ public static void ReadValueSafe(this FastBufferReader reader, out Health health
2726
{
2827
reader.ReadValueSafe(out uint max);
2928
reader.ReadValueSafe(out int current);
30-
health = new Health{ MaxHealth = max, CurrentHealth = current };
29+
health = new Health { MaxHealth = max, CurrentHealth = current };
3130
}
3231
}
3332
#endregion
@@ -36,7 +35,7 @@ public static void ReadValueSafe(this FastBufferReader reader, out Health health
3635
// The class name doesn't matter here.
3736
public static class BufferSerializerExtensions
3837
{
39-
public static void SerializeValue<TReaderWriter>(this BufferSerializer<TReaderWriter> serializer, ref Health health) where TReaderWriter: IReaderWriter
38+
public static void SerializeValue<TReaderWriter>(this BufferSerializer<TReaderWriter> serializer, ref Health health) where TReaderWriter : IReaderWriter
4039
{
4140
// Because the BufferSerializer already knows how to read and write the primitive types
4241
// We can use the existing BufferSerializer serialization.

com.unity.netcode.gameobjects/Tests/Runtime/DocumentationCodeSamples/NetworkVariable.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.netcode.gameobjects/Tests/Runtime/DocumentationCodeSamples/NetworkVariable/CustomNetworkVariable.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System;
2-
using System.Collections.Generic;
3-
using Unity.Netcode;
42
using System.Collections;
3+
using System.Collections.Generic;
54
using NUnit.Framework;
5+
using Unity.Netcode;
66
using Unity.Netcode.TestHelpers.Runtime;
77
using UnityEngine;
88
using UnityEngine.TestTools;
99

1010
namespace DocumentationCodeSamples
1111
{
1212
#region TestMyCustomNetworkVariable
13-
/// Using MyCustomNetworkVariable within a NetworkBehaviour
13+
// Using MyCustomNetworkVariable within a NetworkBehaviour
1414
internal class TestMyCustomNetworkVariable : NetworkBehaviour
1515
{
1616
public MyCustomNetworkVariable CustomNetworkVariable = new MyCustomNetworkVariable();
@@ -22,9 +22,11 @@ public override void OnNetworkSpawn()
2222
{
2323
for (int i = 0; i < 4; i++)
2424
{
25-
var someData = new SomeData();
26-
someData.SomeFloatData = (float)i;
27-
someData.SomeIntData = i;
25+
var someData = new SomeData
26+
{
27+
SomeFloatData = i,
28+
SomeIntData = i
29+
};
2830
someData.SomeListOfValues.Add((ulong)i + 1000000);
2931
someData.SomeListOfValues.Add((ulong)i + 2000000);
3032
someData.SomeListOfValues.Add((ulong)i + 3000000);
@@ -38,11 +40,11 @@ public override void OnNetworkSpawn()
3840
}
3941
}
4042

41-
/// Bare minimum example of NetworkVariableBase derived class
43+
// Bare minimum example of NetworkVariableBase derived class
4244
[Serializable]
4345
public class MyCustomNetworkVariable : NetworkVariableBase
4446
{
45-
/// Managed list of class instances
47+
// Managed list of class instances
4648
public List<SomeData> SomeDataToSynchronize = new List<SomeData>();
4749

4850
/// <summary>
@@ -72,15 +74,15 @@ public override void WriteField(FastBufferWriter writer)
7274
public override void ReadField(FastBufferReader reader)
7375
{
7476
// De-Serialize the data being synchronized
75-
var itemsToUpdate = (int)0;
77+
var itemsToUpdate = 0;
7678
reader.ReadValueSafe(out itemsToUpdate);
7779
SomeDataToSynchronize.Clear();
7880
for (int i = 0; i < itemsToUpdate; i++)
7981
{
8082
var newEntry = new SomeData();
8183
reader.ReadValueSafe(out newEntry.SomeIntData);
8284
reader.ReadValueSafe(out newEntry.SomeFloatData);
83-
var itemsCount = (int)0;
85+
var itemsCount = 0;
8486
var tempValue = (ulong)0;
8587
reader.ReadValueSafe(out itemsCount);
8688
newEntry.SomeListOfValues.Clear();
@@ -107,12 +109,12 @@ public override void WriteDelta(FastBufferWriter writer)
107109
}
108110
}
109111

110-
/// Bare minimum example of generic NetworkVariableBase derived class
112+
// Bare minimum example of generic NetworkVariableBase derived class
111113
[Serializable]
112114
[GenerateSerializationForGenericParameter(0)]
113115
public class MyCustomGenericNetworkVariable<T> : NetworkVariableBase
114116
{
115-
/// Managed list of class instances
117+
// Managed list of class instances
116118
public List<T> SomeDataToSynchronize = new List<T>();
117119

118120
/// <summary>
@@ -138,7 +140,7 @@ public override void WriteField(FastBufferWriter writer)
138140
public override void ReadField(FastBufferReader reader)
139141
{
140142
// De-Serialize the data being synchronized
141-
var itemsToUpdate = (int)0;
143+
var itemsToUpdate = 0;
142144
reader.ReadValueSafe(out itemsToUpdate);
143145
SomeDataToSynchronize.Clear();
144146
for (int i = 0; i < itemsToUpdate; i++)
@@ -163,8 +165,8 @@ public override void WriteDelta(FastBufferWriter writer)
163165
}
164166
}
165167

166-
/// Example managed class used as the item type in the
167-
/// MyCustomNetworkVariable.SomeDataToSynchronize list
168+
// Example managed class used as the item type in the
169+
// MyCustomNetworkVariable.SomeDataToSynchronize list
168170
[Serializable]
169171
public class SomeData
170172
{
@@ -208,7 +210,7 @@ public IEnumerator CustomNetworkVariableCodeWorks()
208210
var testBehaviour = localObject.GetComponent<TestMyCustomNetworkVariable>();
209211
Assert.NotNull(testBehaviour);
210212
Assert.AreEqual(authorityBehaviour.CustomNetworkVariable.SomeDataToSynchronize.Count, testBehaviour.CustomNetworkVariable.SomeDataToSynchronize.Count, $"[Client-{networkManager.LocalClientId}] Incorrect length found for {nameof(MyCustomNetworkVariable)}");
211-
// Assert.AreEqual(authorityBehaviour.CustomNetworkVariable.SomeDataToSynchronize.Count, testBehaviour.CustomNetworkVariable.SomeDataToSynchronize.Count, $"[Client-{networkManager.LocalClientId}] Incorrect length found for {nameof(MyCustomNetworkVariable)}");
213+
Assert.AreEqual(authorityBehaviour.CustomGenericNetworkVariable.SomeDataToSynchronize, testBehaviour.CustomGenericNetworkVariable.SomeDataToSynchronize.Count, $"[Client-{networkManager.LocalClientId}] Incorrect length found for {nameof(MyCustomNetworkVariable)}");
212214
}
213215
}
214216
}

com.unity.netcode.gameobjects/Tests/Runtime/DocumentationCodeSamples/NetworkVariable/NetworkVariableSerialization.cs

Lines changed: 88 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -9,123 +9,125 @@
99

1010
namespace DocumentationCodeSamples
1111
{
12-
public static class FastBufferExtensions
12+
internal static class FastBufferExtensions
1313
{
14-
public static void WriteValueSafe(this FastBufferWriter writer, in Health health)
14+
public static void WriteValueSafe(this FastBufferWriter writer, in TestSerializationDocs.Health health)
1515
{
1616
writer.WriteValueSafe(health.MaxHealth);
1717
writer.WriteValueSafe(health.CurrentHealth);
1818
}
1919

20-
public static void ReadValueSafe(this FastBufferReader reader, out Health health)
20+
public static void ReadValueSafe(this FastBufferReader reader, out TestSerializationDocs.Health health)
2121
{
2222
reader.ReadValueSafe(out uint max);
2323
reader.ReadValueSafe(out int current);
24-
health = new Health{ MaxHealth = max, CurrentHealth = current };
24+
health = new TestSerializationDocs.Health { MaxHealth = max, CurrentHealth = current };
2525
}
2626
}
2727

28-
#region HealthExample
29-
public struct Health
30-
{
31-
public uint MaxHealth;
32-
public int CurrentHealth;
33-
34-
// Register our custom serialization on load
35-
[InitializeOnLoadMethod]
36-
public static void RegisterHealthSerialization()
37-
{
38-
// You can reuse the FastBufferWriter and FastBufferReader extension methods we wrote above
39-
UserNetworkVariableSerialization<Health>.WriteValue = FastBufferExtensions.WriteValueSafe;
40-
UserNetworkVariableSerialization<Health>.ReadValue = FastBufferExtensions.ReadValueSafe;
41-
42-
// Here is where you register your custom delta handling.
43-
UserNetworkVariableSerialization<Health>.WriteDelta = WriteDelta;
44-
UserNetworkVariableSerialization<Health>.ReadDelta = ReadDelta;
4528

46-
// You can also use lambda expressions to register functions
47-
UserNetworkVariableSerialization<Health>.DuplicateValue = (in Health value, ref Health duplicatedValue) => { duplicatedValue = value; };
48-
}
29+
internal class TestSerializationDocs : NetcodeIntegrationTest
30+
{
31+
#region HealthExample
4932

50-
// We can use an enum to indicate which field has changed for the delta change.
51-
// This lets us save bandwidth when only one value has changed.
52-
// In the case of Health, we expect CurrentHealth to change much more often than MaxHealth
53-
// Implementing WriteDelta saves us bandwidth on not sending the MaxHealth every time CurrentHealth changes.
54-
internal enum ChangeType : byte
33+
public struct Health
5534
{
56-
MaxHealth,
57-
CurrentHealth,
58-
All,
59-
}
35+
public uint MaxHealth;
36+
public int CurrentHealth;
6037

61-
public static void WriteDelta(FastBufferWriter writer, in Health value, in Health previousValue)
62-
{
63-
if (value.MaxHealth == previousValue.MaxHealth && value.CurrentHealth != previousValue.CurrentHealth)
38+
// Register our custom serialization on load
39+
[InitializeOnLoadMethod]
40+
public static void RegisterHealthSerialization()
6441
{
65-
// If only our CurrentHealth has changed, we can send the CurrentHealth enum with only the updated CurrentHealth value
66-
writer.WriteValueSafe(ChangeType.CurrentHealth);
67-
writer.WriteValueSafe(value.CurrentHealth);
68-
}
69-
else if (value.CurrentHealth == previousValue.CurrentHealth && value.MaxHealth != previousValue.MaxHealth)
70-
{
71-
// If only our MaxHealth has changed, we can send the MaxHealth enum with only the updated MaxHealth value
72-
writer.WriteValueSafe(ChangeType.MaxHealth);
73-
writer.WriteValueSafe(value.MaxHealth);
42+
// You can reuse the FastBufferWriter and FastBufferReader extension methods we wrote above
43+
UserNetworkVariableSerialization<Health>.WriteValue = FastBufferExtensions.WriteValueSafe;
44+
UserNetworkVariableSerialization<Health>.ReadValue = FastBufferExtensions.ReadValueSafe;
45+
46+
// Here is where you register your custom delta handling.
47+
UserNetworkVariableSerialization<Health>.WriteDelta = WriteDelta;
48+
UserNetworkVariableSerialization<Health>.ReadDelta = ReadDelta;
49+
50+
// You can also use lambda expressions to register functions
51+
UserNetworkVariableSerialization<Health>.DuplicateValue = (in Health value, ref Health duplicatedValue) => { duplicatedValue = value; };
7452
}
75-
else
53+
54+
// We can use an enum to indicate which field has changed for the delta change.
55+
// This lets us save bandwidth when only one value has changed.
56+
// In the case of Health, we expect CurrentHealth to change much more often than MaxHealth
57+
// Implementing WriteDelta saves us bandwidth on not sending the MaxHealth every time CurrentHealth changes.
58+
internal enum ChangeType : byte
7659
{
77-
// If both values have changed, we need to serialize both values.
78-
writer.WriteValueSafe(ChangeType.All);
79-
writer.WriteValueSafe(value.MaxHealth);
80-
writer.WriteValueSafe(value.CurrentHealth);
60+
MaxHealth,
61+
CurrentHealth,
62+
All,
8163
}
82-
}
83-
84-
public static void ReadDelta(FastBufferReader reader, ref Health value)
85-
{
86-
// First we read what type of change we've received
87-
reader.ReadValueSafe(out ChangeType changeType);
8864

89-
// Then we read the data in our delta message, based on what type of change we've received.
90-
switch (changeType)
65+
public static void WriteDelta(FastBufferWriter writer, in Health value, in Health previousValue)
9166
{
92-
case ChangeType.CurrentHealth:
67+
if (value.MaxHealth == previousValue.MaxHealth && value.CurrentHealth != previousValue.CurrentHealth)
9368
{
94-
reader.ReadValueSafe(out value.CurrentHealth);
95-
break;
69+
// If only our CurrentHealth has changed, we can send the CurrentHealth enum with only the updated CurrentHealth value
70+
writer.WriteValueSafe(ChangeType.CurrentHealth);
71+
writer.WriteValueSafe(value.CurrentHealth);
9672
}
97-
case ChangeType.MaxHealth:
73+
else if (value.CurrentHealth == previousValue.CurrentHealth && value.MaxHealth != previousValue.MaxHealth)
9874
{
99-
reader.ReadValueSafe(out value.MaxHealth);
100-
break;
75+
// If only our MaxHealth has changed, we can send the MaxHealth enum with only the updated MaxHealth value
76+
writer.WriteValueSafe(ChangeType.MaxHealth);
77+
writer.WriteValueSafe(value.MaxHealth);
10178
}
102-
case ChangeType.All:
79+
else
10380
{
104-
reader.ReadValueSafe(out value.MaxHealth);
105-
reader.ReadValueSafe(out value.CurrentHealth);
106-
break;
81+
// If both values have changed, we need to serialize both values.
82+
writer.WriteValueSafe(ChangeType.All);
83+
writer.WriteValueSafe(value.MaxHealth);
84+
writer.WriteValueSafe(value.CurrentHealth);
10785
}
10886
}
10987

110-
}
111-
}
112-
#endregion
88+
public static void ReadDelta(FastBufferReader reader, ref Health value)
89+
{
90+
// First we read what type of change we've received
91+
reader.ReadValueSafe(out ChangeType changeType);
11392

114-
internal class TestHealthBehaviour : NetworkBehaviour
115-
{
116-
internal readonly NetworkVariable<Health> HealthVar = new();
93+
// Then we read the data in our delta message, based on what type of change we've received.
94+
switch (changeType)
95+
{
96+
case ChangeType.CurrentHealth:
97+
{
98+
reader.ReadValueSafe(out value.CurrentHealth);
99+
break;
100+
}
101+
case ChangeType.MaxHealth:
102+
{
103+
reader.ReadValueSafe(out value.MaxHealth);
104+
break;
105+
}
106+
case ChangeType.All:
107+
{
108+
reader.ReadValueSafe(out value.MaxHealth);
109+
reader.ReadValueSafe(out value.CurrentHealth);
110+
break;
111+
}
112+
}
113+
}
114+
}
117115

118-
internal Health ReceivedFromRpc;
116+
#endregion
119117

120-
[Rpc(SendTo.Everyone)]
121-
public void SendHealthRpc(Health health)
118+
internal class TestHealthBehaviour : NetworkBehaviour
122119
{
123-
ReceivedFromRpc = health;
120+
internal readonly NetworkVariable<Health> HealthVar = new();
121+
122+
internal Health ReceivedFromRpc;
123+
124+
[Rpc(SendTo.Everyone)]
125+
public void SendHealthRpc(Health health)
126+
{
127+
ReceivedFromRpc = health;
128+
}
124129
}
125-
}
126130

127-
internal class TestSerializationDocs : NetcodeIntegrationTest
128-
{
129131
protected override int NumberOfClients => 1;
130132
private GameObject m_PrefabToSpawn;
131133

@@ -139,6 +141,7 @@ protected override void OnServerAndClientsCreated()
139141
private ulong m_NetworkObjectIdToTest;
140142

141143
private bool m_TestingRpc;
144+
142145
private bool ValidateAllAreEqual(StringBuilder errorLog)
143146
{
144147
foreach (var networkManager in m_NetworkManagers)
@@ -148,6 +151,7 @@ private bool ValidateAllAreEqual(StringBuilder errorLog)
148151
errorLog.Append($"[Client-{networkManager.LocalClientId}] SpawnedObject not found!");
149152
return false;
150153
}
154+
151155
var healthInstance = localInstance.GetComponent<TestHealthBehaviour>();
152156
if (healthInstance == null)
153157
{
@@ -168,6 +172,7 @@ private bool ValidateAllAreEqual(StringBuilder errorLog)
168172
return false;
169173
}
170174
}
175+
171176
return true;
172177
}
173178

@@ -181,7 +186,7 @@ public IEnumerator TestHealthCode()
181186
yield return WaitForSpawnedOnAllOrTimeOut(authorityInstance.NetworkObjectId);
182187
AssertOnTimeout("Failed to spawn network object");
183188

184-
var healthToTest = new Health{ MaxHealth = 456, CurrentHealth = 23 };
189+
var healthToTest = new Health { MaxHealth = 456, CurrentHealth = 23 };
185190
m_ExpectedHealth = healthToTest;
186191
m_TestingRpc = true;
187192

@@ -191,7 +196,7 @@ public IEnumerator TestHealthCode()
191196
AssertOnTimeout("RPC send failed");
192197

193198
m_TestingRpc = false;
194-
healthToTest = new Health{ MaxHealth = 123, CurrentHealth = 45 };
199+
healthToTest = new Health { MaxHealth = 123, CurrentHealth = 45 };
195200
m_ExpectedHealth = healthToTest;
196201

197202
authorityInstance.HealthVar.Value = healthToTest;

0 commit comments

Comments
 (0)