Skip to content

Commit 14704ee

Browse files
EmandMPitouGames
andauthored
feat: [UpPort] Add built-in serialization for UnityEngine.Pose (#3546)
fixes: #2675 continues: #2677 ## Changelog - Added: Built-in serialization for UnityEngine.Pose ## Testing and Documentation - Includes unit tests. ## Backport This is an up-port of #3540 --------- Co-authored-by: PitouGames <pitou.games@gmail.com>
1 parent 2a5de93 commit 14704ee

19 files changed

+448
-23
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Added
1212

13+
- Added serializer for `Pose` (#3546)
1314
- Added methods `GetDefaultNetworkSettings` and `GetDefaultPipelineConfigurations` to `UnityTransport`. These can be used to retrieve the default settings and pipeline stages that are used by `UnityTransport`. This is useful when providing a custom driver constructor through `UnityTransport.s_DriverConstructor`, since it allows reusing or tuning the existing configuration instead of trying to recreate it. This means a transport with a custom driver can now easily benefit from most of the features of `UnityTransport`, like integration with the Network Simulator and Network Profiler from the multiplayer tools package. (#3501)
1415
- Added mappings between `ClientId` and `TransportId`. (#3516)
1516
- Added `NetworkPrefabInstanceHandlerWithData<T>`, a variant of `INetworkPrefabInstanceHandler` that provides access to custom instantiation data directly within the `Instantiate()` method. (#3430)

com.unity.netcode.gameobjects/Editor/CodeGen/CodeGenHelpers.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ internal static class CodeGenHelpers
4444
public static readonly string UnityVector3_FullName = typeof(Vector3).FullName;
4545
public static readonly string UnityVector4_FullName = typeof(Vector4).FullName;
4646
public static readonly string UnityQuaternion_FullName = typeof(Quaternion).FullName;
47+
public static readonly string UnityPose_FullName = typeof(Pose).FullName;
4748
public static readonly string UnityRay_FullName = typeof(Ray).FullName;
4849
public static readonly string UnityRay2D_FullName = typeof(Ray2D).FullName;
4950

@@ -308,6 +309,11 @@ public static bool IsSerializable(this TypeReference typeReference)
308309
return true;
309310
}
310311

312+
if (typeReference.FullName == UnityPose_FullName)
313+
{
314+
return true;
315+
}
316+
311317
if (typeReference.FullName == UnityRay_FullName)
312318
{
313319
return true;

com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly,
563563
typeof(Vector3Int),
564564
typeof(Vector4),
565565
typeof(Quaternion),
566+
typeof(Pose),
566567
typeof(Color),
567568
typeof(Color32),
568569
typeof(Ray),

com.unity.netcode.gameobjects/Runtime/Serialization/BufferSerializer.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ public FastBufferWriter GetFastBufferWriter()
231231
/// <param name="value">The values to read/write</param>
232232
public void SerializeValue(ref Quaternion[] value) => m_Implementation.SerializeValue(ref value);
233233

234+
/// <summary>
235+
/// Read or write a Pose value
236+
/// </summary>
237+
/// <param name="value">The value to read/write</param>
238+
public void SerializeValue(ref Pose value) => m_Implementation.SerializeValue(ref value);
239+
240+
/// <summary>
241+
/// Read or write an array of Pose values
242+
/// </summary>
243+
/// <param name="value">The values to read/write</param>
244+
public void SerializeValue(ref Pose[] value) => m_Implementation.SerializeValue(ref value);
245+
234246
/// <summary>
235247
/// Read or write a Color value
236248
/// </summary>
@@ -553,6 +565,24 @@ public bool PreCheck(int amount)
553565
/// <param name="value">The value to read/write</param>
554566
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Implementation.SerializeValuePreChecked(ref value);
555567

568+
/// <summary>
569+
/// Serialize a Pose, "pre-checked", which skips buffer checks.
570+
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
571+
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
572+
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
573+
/// </summary>
574+
/// <param name="value">The value to read/write</param>
575+
public void SerializeValuePreChecked(ref Pose value) => m_Implementation.SerializeValuePreChecked(ref value);
576+
577+
/// <summary>
578+
/// Serialize a Pose array, "pre-checked", which skips buffer checks.
579+
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
580+
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
581+
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
582+
/// </summary>
583+
/// <param name="value">The value to read/write</param>
584+
public void SerializeValuePreChecked(ref Pose[] value) => m_Implementation.SerializeValuePreChecked(ref value);
585+
556586
/// <summary>
557587
/// Serialize a Color, "pre-checked", which skips buffer checks.
558588
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before

com.unity.netcode.gameobjects/Runtime/Serialization/BufferSerializerReader.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unus
7171
public void SerializeValue(ref Quaternion value) => m_Reader.ReadValueSafe(out value);
7272
public void SerializeValue(ref Quaternion[] value) => m_Reader.ReadValueSafe(out value);
7373

74+
public void SerializeValue(ref Pose value) => m_Reader.ReadValueSafe(out value);
75+
public void SerializeValue(ref Pose[] value) => m_Reader.ReadValueSafe(out value);
76+
7477
public void SerializeValue(ref Color value) => m_Reader.ReadValueSafe(out value);
7578
public void SerializeValue(ref Color[] value) => m_Reader.ReadValueSafe(out value);
7679

@@ -127,6 +130,9 @@ public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedSt
127130
public void SerializeValuePreChecked(ref Quaternion value) => m_Reader.ReadValue(out value);
128131
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Reader.ReadValue(out value);
129132

133+
public void SerializeValuePreChecked(ref Pose value) => m_Reader.ReadValue(out value);
134+
public void SerializeValuePreChecked(ref Pose[] value) => m_Reader.ReadValue(out value);
135+
130136
public void SerializeValuePreChecked(ref Color value) => m_Reader.ReadValue(out value);
131137
public void SerializeValuePreChecked(ref Color[] value) => m_Reader.ReadValue(out value);
132138

com.unity.netcode.gameobjects/Runtime/Serialization/BufferSerializerWriter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unus
7070
public void SerializeValue(ref Quaternion value) => m_Writer.WriteValueSafe(value);
7171
public void SerializeValue(ref Quaternion[] value) => m_Writer.WriteValueSafe(value);
7272

73+
public void SerializeValue(ref Pose value) => m_Writer.WriteValueSafe(value);
74+
public void SerializeValue(ref Pose[] value) => m_Writer.WriteValueSafe(value);
75+
7376
public void SerializeValue(ref Color value) => m_Writer.WriteValueSafe(value);
7477
public void SerializeValue(ref Color[] value) => m_Writer.WriteValueSafe(value);
7578

@@ -128,6 +131,9 @@ public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedSt
128131
public void SerializeValuePreChecked(ref Quaternion value) => m_Writer.WriteValue(value);
129132
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Writer.WriteValue(value);
130133

134+
public void SerializeValuePreChecked(ref Pose value) => m_Writer.WriteValue(value);
135+
public void SerializeValuePreChecked(ref Pose[] value) => m_Writer.WriteValue(value);
136+
131137
public void SerializeValuePreChecked(ref Color value) => m_Writer.WriteValue(value);
132138
public void SerializeValuePreChecked(ref Color[] value) => m_Writer.WriteValue(value);
133139

com.unity.netcode.gameobjects/Runtime/Serialization/BytePacker.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ public static void WriteValuePacked(FastBufferWriter writer, Quaternion rotation
259259
WriteValuePacked(writer, rotation.w);
260260
}
261261

262+
/// <summary>
263+
/// Writes the pose to the buffer.
264+
/// </summary>
265+
/// <param name="writer">The writer to write to</param>
266+
/// <param name="pose">Pose to write</param>
267+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
268+
public static void WriteValuePacked(FastBufferWriter writer, Pose pose)
269+
{
270+
WriteValuePacked(writer, pose.position);
271+
WriteValuePacked(writer, pose.rotation);
272+
}
273+
262274
/// <summary>
263275
/// Writes a string in a packed format
264276
/// </summary>

com.unity.netcode.gameobjects/Runtime/Serialization/ByteUnpacker.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,19 @@ public static void ReadValuePacked(FastBufferReader reader, out Quaternion rotat
278278
ReadValuePacked(reader, out rotation.w);
279279
}
280280

281+
/// <summary>
282+
/// Reads the pose from the stream.
283+
/// </summary>
284+
/// <param name="reader">The reader to read from</param>
285+
/// <param name="pose">Pose to read</param>
286+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
287+
public static void ReadValuePacked(FastBufferReader reader, out Pose pose)
288+
{
289+
ReadValuePacked(reader, out Vector3 position);
290+
ReadValuePacked(reader, out Quaternion rotation);
291+
pose = new Pose(position, rotation);
292+
}
293+
281294
/// <summary>
282295
/// Reads a string in a packed format
283296
/// </summary>

com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferReader.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,20 @@ internal void ReadValueSafeInPlace<TKey, TVal>(ref NativeHashMap<TKey, TVal> val
13591359
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13601360
public void ReadValue(out Quaternion[] value) => ReadUnmanaged(out value);
13611361

1362+
/// <summary>
1363+
/// Read a Pose
1364+
/// </summary>
1365+
/// <param name="value">the value to read</param>
1366+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1367+
public void ReadValue(out Pose value) => ReadUnmanaged(out value);
1368+
1369+
/// <summary>
1370+
/// Read a Pose array
1371+
/// </summary>
1372+
/// <param name="value">the values to read</param>
1373+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1374+
public void ReadValue(out Pose[] value) => ReadUnmanaged(out value);
1375+
13621376
/// <summary>
13631377
/// Read a Color
13641378
/// </summary>
@@ -1536,6 +1550,26 @@ internal void ReadValueSafeInPlace<TKey, TVal>(ref NativeHashMap<TKey, TVal> val
15361550
[MethodImpl(MethodImplOptions.AggressiveInlining)]
15371551
public void ReadValueSafe(out Quaternion[] value) => ReadUnmanagedSafe(out value);
15381552

1553+
/// <summary>
1554+
/// Read a Pose
1555+
///
1556+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1557+
/// for multiple reads at once by calling TryBeginRead.
1558+
/// </summary>
1559+
/// <param name="value">the value to read</param>
1560+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1561+
public void ReadValueSafe(out Pose value) => ReadUnmanagedSafe(out value);
1562+
1563+
/// <summary>
1564+
/// Read a Pose array
1565+
///
1566+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1567+
/// for multiple reads at once by calling TryBeginRead.
1568+
/// </summary>
1569+
/// <param name="value">the values to read</param>
1570+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1571+
public void ReadValueSafe(out Pose[] value) => ReadUnmanagedSafe(out value);
1572+
15391573
/// <summary>
15401574
/// Read a Color
15411575
///

com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferWriter.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,20 @@ public void WriteValueSafe<T>(NativeList<T> value, ForGeneric unused = default)
14881488
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14891489
public void WriteValue(Quaternion[] value) => WriteUnmanaged(value);
14901490

1491+
/// <summary>
1492+
/// Write a Pose
1493+
/// </summary>
1494+
/// <param name="value">the value to write</param>
1495+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1496+
public void WriteValue(in Pose value) => WriteUnmanaged(value);
1497+
1498+
/// <summary>
1499+
/// Write a Pose array
1500+
/// </summary>
1501+
/// <param name="value">the values to write</param>
1502+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1503+
public void WriteValue(Pose[] value) => WriteUnmanaged(value);
1504+
14911505
/// <summary>
14921506
/// Write a Color
14931507
/// </summary>
@@ -1664,6 +1678,26 @@ public void WriteValueSafe<T>(NativeList<T> value, ForGeneric unused = default)
16641678
[MethodImpl(MethodImplOptions.AggressiveInlining)]
16651679
public void WriteValueSafe(Quaternion[] value) => WriteUnmanagedSafe(value);
16661680

1681+
/// <summary>
1682+
/// Write a Pose
1683+
///
1684+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1685+
/// for multiple writes at once by calling TryBeginWrite.
1686+
/// </summary>
1687+
/// <param name="value">the value to write</param>
1688+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1689+
public void WriteValueSafe(in Pose value) => WriteUnmanagedSafe(value);
1690+
1691+
/// <summary>
1692+
/// Write a Pose array
1693+
///
1694+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1695+
/// for multiple writes at once by calling TryBeginWrite.
1696+
/// </summary>
1697+
/// <param name="value">the values to write</param>
1698+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1699+
public void WriteValueSafe(Pose[] value) => WriteUnmanagedSafe(value);
1700+
16671701
/// <summary>
16681702
/// Write a Color
16691703
///

0 commit comments

Comments
 (0)