Skip to content

Commit b8e1e92

Browse files
committed
Reworked Object spawning system
1 parent 5310c40 commit b8e1e92

File tree

8 files changed

+321
-164
lines changed

8 files changed

+321
-164
lines changed

MLAPI/Data/NetworkConfig.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ public class NetworkConfig
5050
/// <summary>
5151
/// A list of spawnable prefabs
5252
/// </summary>
53-
public List<GameObject> SpawnablePrefabs = new List<GameObject>();
53+
public List<NetworkedPrefab> NetworkedPrefabs = new List<NetworkedPrefab>();
54+
internal Dictionary<string, int> NetworkPrefabIds;
55+
internal Dictionary<int, string> NetworkPrefabNames;
5456
/// <summary>
5557
/// The default player prefab
5658
/// </summary>
57-
public GameObject PlayerPrefab;
59+
public string PlayerPrefabName;
5860
/// <summary>
5961
/// The size of the receive message buffer. This is the max message size.
6062
/// </summary>
@@ -132,7 +134,7 @@ public class NetworkConfig
132134
/// <summary>
133135
/// Wheter or not to enable scene switching
134136
/// </summary>
135-
public bool EnableSceneSwitching = false;
137+
public bool EnableSceneSwitching = true;
136138

137139
private byte[] ConfigHash = null;
138140
/// <summary>
@@ -172,7 +174,10 @@ public byte[] GetConfig(bool cache = true)
172174
}
173175
if(HandleObjectSpawning)
174176
{
175-
writer.Write(SpawnablePrefabs.Count);
177+
for (int i = 0; i < NetworkedPrefabs.Count; i++)
178+
{
179+
writer.Write(NetworkedPrefabs[i].name);
180+
}
176181
}
177182
writer.Write(HandleObjectSpawning);
178183
writer.Write(EnableEncryption);

MLAPI/Data/NetworkPool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ internal class NetworkPool
88
internal GameObject[] objects;
99
internal ushort poolId;
1010

11-
internal NetworkPool(int prefabIndex, uint size, ushort poolIndex)
11+
internal NetworkPool(int prefabId, uint size, ushort poolIndex)
1212
{
1313
objects = new GameObject[size];
1414
poolId = poolIndex;
1515
for (int i = 0; i < size; i++)
1616
{
17-
GameObject go = Object.Instantiate(NetworkingManager.singleton.NetworkConfig.SpawnablePrefabs[prefabIndex], Vector3.zero, Quaternion.identity);
17+
GameObject go = MonoBehaviour.Instantiate(NetworkingManager.singleton.NetworkConfig.NetworkedPrefabs[prefabId].prefab, Vector3.zero, Quaternion.identity);
1818
go.GetComponent<NetworkedObject>()._isPooledObject = true;
1919
go.GetComponent<NetworkedObject>().poolId = poolId;
2020
go.GetComponent<NetworkedObject>().Spawn();

MLAPI/Data/NetworkedPrefab.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace MLAPI.Data
5+
{
6+
[Serializable]
7+
public class NetworkedPrefab
8+
{
9+
public string name;
10+
public GameObject prefab;
11+
}
12+
}

MLAPI/MLAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<Compile Include="Data\FieldType.cs" />
7272
<Compile Include="Attributes\SyncedVar.cs" />
7373
<Compile Include="Data\NetworkConfig.cs" />
74+
<Compile Include="Data\NetworkedPrefab.cs" />
7475
<Compile Include="Data\NetworkPool.cs" />
7576
<Compile Include="Data\TrackedPointData.cs" />
7677
<Compile Include="Data\TransportHost.cs" />

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ namespace MLAPI.MonoBehaviours.Core
1212
[AddComponentMenu("MLAPI/NetworkedObject", -99)]
1313
public sealed class NetworkedObject : MonoBehaviour
1414
{
15+
private void OnValidate()
16+
{
17+
if(string.IsNullOrEmpty(NetworkedPrefabName))
18+
{
19+
Debug.LogWarning("MLAPI: The networked object " + gameObject.name + " has not been assigned a networkedPrefabName. Setting it to " + gameObject.name);
20+
NetworkedPrefabName = gameObject.name;
21+
}
22+
}
23+
1524
/// <summary>
1625
/// Gets the unique ID of this object that is synced across the network
1726
/// </summary>
@@ -35,16 +44,9 @@ public uint OwnerClientId
3544
}
3645
internal uint ownerClientId = new NetId(0, 0, false, true).GetClientId();
3746
/// <summary>
38-
/// The index of the prefab used to spawn this in the spawnablePrefabs list
47+
/// The name of the NetworkedPrefab
3948
/// </summary>
40-
public int SpawnablePrefabIndex
41-
{
42-
get
43-
{
44-
return spawnablePrefabIndex;
45-
}
46-
}
47-
internal int spawnablePrefabIndex;
49+
public string NetworkedPrefabName = string.Empty;
4850
/// <summary>
4951
/// Gets if this object is a player object
5052
/// </summary>
@@ -57,11 +59,6 @@ public bool isPlayerObject
5759
}
5860
internal bool _isPlayerObject = false;
5961
/// <summary>
60-
/// Gets or sets if this object should be replicated across the network. Can only be changed before the object is spawned
61-
/// </summary>
62-
[SerializeField]
63-
public bool ServerOnly = false;
64-
/// <summary>
6562
/// Gets if this object is part of a pool
6663
/// </summary>
6764
public bool isPooledObject
@@ -115,7 +112,7 @@ public bool isSpawned
115112
}
116113
}
117114
internal bool _isSpawned = false;
118-
internal bool sceneObject = false;
115+
internal bool? sceneObject = null;
119116

120117
private void OnDestroy()
121118
{
@@ -129,7 +126,7 @@ private void OnDestroy()
129126
public void Spawn()
130127
{
131128
if (NetworkingManager.singleton != null)
132-
SpawnManager.OnSpawnObject(this);
129+
SpawnManager.SpawnPrefabIndexServer(this);
133130
}
134131
/// <summary>
135132
/// Spawns an object across the network with a given owner. Can only be called from server
@@ -138,7 +135,7 @@ public void Spawn()
138135
public void SpawnWithOwnership(uint clientId)
139136
{
140137
if (NetworkingManager.singleton != null)
141-
SpawnManager.OnSpawnObject(this, clientId);
138+
SpawnManager.SpawnPrefabIndexServer(this, clientId);
142139
}
143140
/// <summary>
144141
/// Removes all ownership of an object from any client. Can only be called from server

0 commit comments

Comments
 (0)