forked from tmokmss/com.unity.multiplayer.samples.coop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkGuid.cs
36 lines (32 loc) · 1.12 KB
/
NetworkGuid.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using Unity.Netcode;
namespace Unity.BossRoom.Infrastructure
{
public class NetworkGuid : INetworkSerializable
{
public ulong FirstHalf;
public ulong SecondHalf;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref FirstHalf);
serializer.SerializeValue(ref SecondHalf);
}
}
public static class NetworkGuidExtensions
{
public static NetworkGuid ToNetworkGuid(this Guid id)
{
var networkId = new NetworkGuid();
networkId.FirstHalf = BitConverter.ToUInt64(id.ToByteArray(), 0);
networkId.SecondHalf = BitConverter.ToUInt64(id.ToByteArray(), 8);
return networkId;
}
public static Guid ToGuid(this NetworkGuid networkId)
{
var bytes = new byte[16];
Buffer.BlockCopy(BitConverter.GetBytes(networkId.FirstHalf), 0, bytes, 0, 8);
Buffer.BlockCopy(BitConverter.GetBytes(networkId.SecondHalf), 0, bytes, 8, 8);
return new Guid(bytes);
}
}
}