Skip to content

Commit 72e40e0

Browse files
committed
feat: Add built-in serialization for UnityEngine.Pose (Unity-Technologies#2675)
1 parent 332d9c1 commit 72e40e0

22 files changed

+410
-25
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Additional documentation and release notes are available at [Multiplayer Documen
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- Added serializer for `Pose` (#2675)
14+
1115
### Fixed
1216

1317
- Removed anoying warnings when using NetworkVariable in offline mode. (#2279)

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

+6
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

+1
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly,
562562
typeof(Vector3Int),
563563
typeof(Vector4),
564564
typeof(Quaternion),
565+
typeof(Pose),
565566
typeof(Color),
566567
typeof(Color32),
567568
typeof(Ray),

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

+30
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>
@@ -555,6 +567,24 @@ public bool PreCheck(int amount)
555567
/// <param name="value">The value to read/write</param>
556568
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Implementation.SerializeValuePreChecked(ref value);
557569

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

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

+6
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

+6
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

+17
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,23 @@ 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.x);
271+
WriteValuePacked(writer, pose.position.y);
272+
WriteValuePacked(writer, pose.position.z);
273+
WriteValuePacked(writer, pose.rotation.x);
274+
WriteValuePacked(writer, pose.rotation.y);
275+
WriteValuePacked(writer, pose.rotation.z);
276+
WriteValuePacked(writer, pose.rotation.w);
277+
}
278+
262279
/// <summary>
263280
/// Writes a string in a packed format
264281
/// </summary>

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

+18
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,24 @@ 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+
pose = new Pose();
290+
ReadValuePacked(reader, out pose.position.x);
291+
ReadValuePacked(reader, out pose.position.y);
292+
ReadValuePacked(reader, out pose.position.z);
293+
ReadValuePacked(reader, out pose.rotation.x);
294+
ReadValuePacked(reader, out pose.rotation.y);
295+
ReadValuePacked(reader, out pose.rotation.z);
296+
ReadValuePacked(reader, out pose.rotation.w);
297+
}
298+
281299
/// <summary>
282300
/// Reads a string in a packed format
283301
/// </summary>

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

+34
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,20 @@ internal void ReadValueSafeInPlace<TKey, TVal>(ref NativeHashMap<TKey, TVal> val
12791279
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12801280
public void ReadValue(out Quaternion[] value) => ReadUnmanaged(out value);
12811281

1282+
/// <summary>
1283+
/// Read a Pose
1284+
/// </summary>
1285+
/// <param name="value">the value to read</param>
1286+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1287+
public void ReadValue(out Pose value) => ReadUnmanaged(out value);
1288+
1289+
/// <summary>
1290+
/// Read a Pose array
1291+
/// </summary>
1292+
/// <param name="value">the values to read</param>
1293+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1294+
public void ReadValue(out Pose[] value) => ReadUnmanaged(out value);
1295+
12821296
/// <summary>
12831297
/// Read a Color
12841298
/// </summary>
@@ -1456,6 +1470,26 @@ internal void ReadValueSafeInPlace<TKey, TVal>(ref NativeHashMap<TKey, TVal> val
14561470
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14571471
public void ReadValueSafe(out Quaternion[] value) => ReadUnmanagedSafe(out value);
14581472

1473+
/// <summary>
1474+
/// Read a Pose
1475+
///
1476+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1477+
/// for multiple reads at once by calling TryBeginRead.
1478+
/// </summary>
1479+
/// <param name="value">the value to read</param>
1480+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1481+
public void ReadValueSafe(out Pose value) => ReadUnmanagedSafe(out value);
1482+
1483+
/// <summary>
1484+
/// Read a Pose array
1485+
///
1486+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1487+
/// for multiple reads at once by calling TryBeginRead.
1488+
/// </summary>
1489+
/// <param name="value">the values to read</param>
1490+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1491+
public void ReadValueSafe(out Pose[] value) => ReadUnmanagedSafe(out value);
1492+
14591493
/// <summary>
14601494
/// Read a Color
14611495
///

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

+34
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,20 @@ public void WriteValueSafe<T>(NativeList<T> value, ForGeneric unused = default)
14661466
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14671467
public void WriteValue(Quaternion[] value) => WriteUnmanaged(value);
14681468

1469+
/// <summary>
1470+
/// Write a Pose
1471+
/// </summary>
1472+
/// <param name="value">the value to write</param>
1473+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1474+
public void WriteValue(in Pose value) => WriteUnmanaged(value);
1475+
1476+
/// <summary>
1477+
/// Write a Pose array
1478+
/// </summary>
1479+
/// <param name="value">the values to write</param>
1480+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1481+
public void WriteValue(Pose[] value) => WriteUnmanaged(value);
1482+
14691483
/// <summary>
14701484
/// Write a Color
14711485
/// </summary>
@@ -1642,6 +1656,26 @@ public void WriteValueSafe<T>(NativeList<T> value, ForGeneric unused = default)
16421656
[MethodImpl(MethodImplOptions.AggressiveInlining)]
16431657
public void WriteValueSafe(Quaternion[] value) => WriteUnmanagedSafe(value);
16441658

1659+
/// <summary>
1660+
/// Write a Pose
1661+
///
1662+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1663+
/// for multiple writes at once by calling TryBeginWrite.
1664+
/// </summary>
1665+
/// <param name="value">the value to write</param>
1666+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1667+
public void WriteValueSafe(in Pose value) => WriteUnmanagedSafe(value);
1668+
1669+
/// <summary>
1670+
/// Write a Pose array
1671+
///
1672+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1673+
/// for multiple writes at once by calling TryBeginWrite.
1674+
/// </summary>
1675+
/// <param name="value">the values to write</param>
1676+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1677+
public void WriteValueSafe(Pose[] value) => WriteUnmanagedSafe(value);
1678+
16451679
/// <summary>
16461680
/// Write a Color
16471681
///

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

+30
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,18 @@ void SerializeValue<T>(ref NativeList<T> value)
233233
/// <param name="value">The values to read/write</param>
234234
void SerializeValue(ref Quaternion[] value);
235235

236+
/// <summary>
237+
/// Read or write a Pose value
238+
/// </summary>
239+
/// <param name="value">The value to read/write</param>
240+
void SerializeValue(ref Pose value);
241+
242+
/// <summary>
243+
/// Read or write an array of Pose values
244+
/// </summary>
245+
/// <param name="value">The values to read/write</param>
246+
void SerializeValue(ref Pose[] value);
247+
236248
/// <summary>
237249
/// Read or write a Color value
238250
/// </summary>
@@ -529,6 +541,24 @@ void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedStrings u
529541
/// <param name="value">The value to read/write</param>
530542
void SerializeValuePreChecked(ref Quaternion[] value);
531543

544+
/// <summary>
545+
/// Serialize a Pose, "pre-checked", which skips buffer checks.
546+
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
547+
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
548+
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
549+
/// </summary>
550+
/// <param name="value">The value to read/write</param>
551+
void SerializeValuePreChecked(ref Pose value);
552+
553+
/// <summary>
554+
/// Serialize a Pose array, "pre-checked", which skips buffer checks.
555+
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
556+
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
557+
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
558+
/// </summary>
559+
/// <param name="value">The value to read/write</param>
560+
void SerializeValuePreChecked(ref Pose[] value);
561+
532562
/// <summary>
533563
/// Serialize a Color, "pre-checked", which skips buffer checks.
534564
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before

0 commit comments

Comments
 (0)