forked from Unity-Technologies/com.unity.netcode.gameobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkPoolManager.cs
More file actions
84 lines (79 loc) · 3.18 KB
/
Copy pathNetworkPoolManager.cs
File metadata and controls
84 lines (79 loc) · 3.18 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using MLAPI.Data;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace MLAPI.NetworkingManagerComponents
{
public static class NetworkPoolManager
{
internal static Dictionary<ushort, NetworkPool> Pools;
private static ushort PoolIndex = 0;
internal static Dictionary<string, ushort> PoolNamesToIndexes;
//Server only
public static void CreatePool(string poolName, int spawnablePrefabIndex, uint size = 16)
{
if(!NetworkingManager.singleton.isServer)
{
Debug.LogWarning("MLAPI: Pools can only be created on the server");
return;
}
NetworkPool pool = new NetworkPool(spawnablePrefabIndex, size, PoolIndex);
PoolNamesToIndexes.Add(poolName, PoolIndex);
PoolIndex++;
}
public static void DestroyPool(string poolName)
{
if (!NetworkingManager.singleton.isServer)
{
Debug.LogWarning("MLAPI: Pools can only be destroyed on the server");
return;
}
for (int i = 0; i < Pools[PoolNamesToIndexes[poolName]].objects.Length; i++)
{
MonoBehaviour.Destroy(Pools[PoolNamesToIndexes[poolName]].objects[i]);
}
Pools.Remove(PoolNamesToIndexes[poolName]);
}
public static GameObject SpawnPoolObject(string poolName, Vector3 position, Quaternion rotation)
{
if (!NetworkingManager.singleton.isServer)
{
Debug.LogWarning("MLAPI: Object spawning can only occur on server");
return null;
}
GameObject go = Pools[PoolNamesToIndexes[poolName]].SpawnObject(position, rotation);
using (MemoryStream stream = new MemoryStream(28))
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(go.GetComponent<NetworkedObject>().NetworkId);
writer.Write(position.x);
writer.Write(position.y);
writer.Write(position.z);
writer.Write(rotation.eulerAngles.x);
writer.Write(rotation.eulerAngles.y);
writer.Write(rotation.eulerAngles.z);
}
NetworkingManager.singleton.Send("MLAPI_SPAWN_POOL_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer());
}
return go;
}
public static void DestroyPoolObject(NetworkedObject netObject)
{
if (!NetworkingManager.singleton.isServer)
{
Debug.LogWarning("MLAPI: Objects can only be destroyed on the server");
return;
}
netObject.gameObject.SetActive(false);
using (MemoryStream stream = new MemoryStream(4))
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(netObject.NetworkId);
}
NetworkingManager.singleton.Send("MLAPI_DESTROY_POOL_OBJECT", "MLAPI_INTERNAL", stream.GetBuffer());
}
}
}
}