@@ -7,73 +7,78 @@ namespace MLAPI.NetworkingManagerComponents
7
7
{
8
8
public static class NetworkPoolManager
9
9
{
10
- internal static Dictionary < string , NetworkPool > Pools ;
11
- //We want to keep the pool indexes incrementing, this is to prevent new pools getting old names and the wrong objects being spawned.
10
+ internal static Dictionary < ushort , NetworkPool > Pools ;
12
11
private static ushort PoolIndex = 0 ;
12
+ internal static Dictionary < string , ushort > PoolNamesToIndexes ;
13
13
14
- internal static Dictionary < ushort , string > PoolIndexToPoolName = new Dictionary < ushort , string > ( ) ;
15
- internal static Dictionary < string , ushort > PoolNamesToIndexes = new Dictionary < string , ushort > ( ) ;
16
-
17
- public static void CreatePool ( string poolName , GameObject poolPrefab , uint size = 16 )
14
+ //Server only
15
+ public static void CreatePool ( string poolName , int spawnablePrefabIndex , uint size = 16 )
18
16
{
19
- if ( Pools . ContainsKey ( poolName ) )
17
+ if ( ! NetworkingManager . singleton . isServer )
20
18
{
21
- Debug . LogWarning ( "MLAPI: A pool with the name " + poolName + " already exists ") ;
19
+ Debug . LogWarning ( "MLAPI: Pools can only be created on the server " ) ;
22
20
return ;
23
21
}
24
- else if ( poolPrefab == null )
25
- {
26
- Debug . LogWarning ( "MLAPI: A pool prefab is required" ) ;
27
- }
28
- PoolIndexToPoolName . Add ( PoolIndex , poolName ) ;
22
+ NetworkPool pool = new NetworkPool ( spawnablePrefabIndex , size , PoolIndex ) ;
29
23
PoolNamesToIndexes . Add ( poolName , PoolIndex ) ;
30
24
PoolIndex ++ ;
31
- Pools . Add ( poolName , new NetworkPool ( poolPrefab , size , poolName ) ) ;
32
25
}
33
26
34
-
35
- public static void CreatePool ( string poolName , GameObject [ ] poolPrefabs )
27
+ public static void DestroyPool ( string poolName )
36
28
{
37
- if ( Pools . ContainsKey ( poolName ) )
29
+ if ( ! NetworkingManager . singleton . isServer )
38
30
{
39
- Debug . LogWarning ( "MLAPI: A pool with the name " + poolName + " already exists ") ;
31
+ Debug . LogWarning ( "MLAPI: Pools can only be destroyed on the server " ) ;
40
32
return ;
41
33
}
42
- else if ( poolPrefabs == null )
34
+ for ( int i = 0 ; i < Pools [ PoolNamesToIndexes [ poolName ] ] . objects . Length ; i ++ )
43
35
{
44
- Debug . LogWarning ( "MLAPI: A pool prefab array is required" ) ;
36
+ MonoBehaviour . Destroy ( Pools [ PoolNamesToIndexes [ poolName ] ] . objects [ i ] ) ;
45
37
}
46
- PoolIndexToPoolName . Add ( PoolIndex , poolName ) ;
47
- PoolNamesToIndexes . Add ( poolName , PoolIndex ) ;
48
- PoolIndex ++ ;
49
- Pools . Add ( poolName , new NetworkPool ( poolPrefabs , poolName ) ) ;
38
+ Pools . Remove ( PoolNamesToIndexes [ poolName ] ) ;
50
39
}
51
40
52
41
public static GameObject SpawnPoolObject ( string poolName , Vector3 position , Quaternion rotation )
53
42
{
54
- if ( NetworkingManager . singleton . isServer )
43
+ if ( ! NetworkingManager . singleton . isServer )
44
+ {
45
+ Debug . LogWarning ( "MLAPI: Object spawning can only occur on server" ) ;
46
+ return null ;
47
+ }
48
+ GameObject go = Pools [ PoolNamesToIndexes [ poolName ] ] . SpawnObject ( position , rotation ) ;
49
+ using ( MemoryStream stream = new MemoryStream ( 28 ) )
55
50
{
56
- using ( MemoryStream stream = new MemoryStream ( 26 ) )
51
+ using ( BinaryWriter writer = new BinaryWriter ( stream ) )
57
52
{
58
- using ( BinaryWriter writer = new BinaryWriter ( stream ) )
59
- {
60
- writer . Write ( PoolNamesToIndexes [ poolName ] ) ;
61
- writer . Write ( position . x ) ;
62
- writer . Write ( position . y ) ;
63
- writer . Write ( position . z ) ;
64
- writer . Write ( rotation . eulerAngles . x ) ;
65
- writer . Write ( rotation . eulerAngles . y ) ;
66
- writer . Write ( rotation . eulerAngles . z ) ;
67
- }
68
- NetworkingManager . singleton . Send ( "MLAPI_SPAWN_POOL_OBJECT" , "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED" , stream . GetBuffer ( ) ) ;
53
+ writer . Write ( go . GetComponent < NetworkedObject > ( ) . NetworkId ) ;
54
+ writer . Write ( position . x ) ;
55
+ writer . Write ( position . y ) ;
56
+ writer . Write ( position . z ) ;
57
+ writer . Write ( rotation . eulerAngles . x ) ;
58
+ writer . Write ( rotation . eulerAngles . y ) ;
59
+ writer . Write ( rotation . eulerAngles . z ) ;
69
60
}
61
+ NetworkingManager . singleton . Send ( "MLAPI_SPAWN_POOL_OBJECT" , "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED" , stream . GetBuffer ( ) ) ;
70
62
}
71
- return Pools [ poolName ] . SpawnObject ( position , rotation ) ;
63
+ return go ;
72
64
}
73
65
74
- public static void DestroyPoolObject ( GameObject gameObject )
66
+ public static void DestroyPoolObject ( NetworkedObject netObject )
75
67
{
76
- gameObject . SetActive ( false ) ;
68
+ if ( ! NetworkingManager . singleton . isServer )
69
+ {
70
+ Debug . LogWarning ( "MLAPI: Objects can only be destroyed on the server" ) ;
71
+ return ;
72
+ }
73
+ netObject . gameObject . SetActive ( false ) ;
74
+ using ( MemoryStream stream = new MemoryStream ( 4 ) )
75
+ {
76
+ using ( BinaryWriter writer = new BinaryWriter ( stream ) )
77
+ {
78
+ writer . Write ( netObject . NetworkId ) ;
79
+ }
80
+ NetworkingManager . singleton . Send ( "MLAPI_DESTROY_POOL_OBJECT" , "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED" , stream . GetBuffer ( ) ) ;
81
+ }
77
82
}
78
83
}
79
84
}
0 commit comments