forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkPrefabInstanceHandlerWithData.cs
More file actions
41 lines (34 loc) · 1.9 KB
/
NetworkPrefabInstanceHandlerWithData.cs
File metadata and controls
41 lines (34 loc) · 1.9 KB
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
37
38
39
40
41
using UnityEngine;
namespace Unity.Netcode
{
/// <summary>
/// Specialized version of <see cref="INetworkPrefabInstanceHandler"/> that receives
/// custom instantiation data injected by the server before spawning.
/// </summary>
public abstract class NetworkPrefabInstanceHandlerWithData<T> : INetworkPrefabInstanceHandlerWithData where T : struct, INetworkSerializable
{
public abstract NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation, T instantiationData);
public abstract void Destroy(NetworkObject networkObject);
bool INetworkPrefabInstanceHandlerWithData.HandlesDataType<TK>() => typeof(T) == typeof(TK);
NetworkObject INetworkPrefabInstanceHandlerWithData.Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation, FastBufferReader reader)
{
var startPosition = reader.Position;
reader.ReadValueSafe(out T payload);
var length = reader.Position - startPosition;
NetworkObject networkObject = Instantiate(ownerClientId, position, rotation, payload);
reader.Seek(startPosition);
if (networkObject.InstantiationData == null || networkObject.InstantiationData.Length != length)
{
networkObject.InstantiationData = new byte[length];
}
reader.ReadBytesSafe(ref networkObject.InstantiationData, length);
return networkObject;
}
NetworkObject INetworkPrefabInstanceHandler.Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation) => Instantiate(ownerClientId, position, rotation, default);
}
internal interface INetworkPrefabInstanceHandlerWithData : INetworkPrefabInstanceHandler
{
bool HandlesDataType<T>();
NetworkObject Instantiate(ulong ownerClientId, Vector3 position, Quaternion rotation, FastBufferReader reader);
}
}