Skip to content

Commit 3037031

Browse files
committed
Added UnSpawn
1 parent f9041ee commit 3037031

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class NetworkedObject : MonoBehaviour
1515
{
1616
private void OnValidate()
1717
{
18-
if(string.IsNullOrEmpty(NetworkedPrefabName))
18+
if (string.IsNullOrEmpty(NetworkedPrefabName))
1919
{
2020
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("The NetworkedObject " + gameObject.name + " does not have a NetworkedPrefabName. It has been set to the gameObject name");
2121
NetworkedPrefabName = gameObject.name;
@@ -226,6 +226,16 @@ public void Spawn()
226226
if (NetworkingManager.singleton != null)
227227
SpawnManager.SpawnObject(this);
228228
}
229+
230+
/// <summary>
231+
/// Unspawns this GameObject and destroys it for other clients. This should be used if the object should be kept on the server
232+
/// </summary>
233+
public void UnSpawn()
234+
{
235+
if (NetworkingManager.singleton != null)
236+
SpawnManager.UnSpawnObject(this);
237+
}
238+
229239
/// <summary>
230240
/// Spawns an object across the network with a given owner. Can only be called from server
231241
/// </summary>

MLAPI/NetworkingManagerComponents/Core/SpawnManager.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,27 @@ internal static GameObject CreateSpawnedObject(int networkedPrefabId, uint netwo
188188
return go;
189189
}
190190

191+
internal static void UnSpawnObject(NetworkedObject netObject)
192+
{
193+
if (!netObject.isSpawned)
194+
{
195+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Cannot unspawn objects that are not spawned");
196+
return;
197+
}
198+
else if(!netManager.isServer)
199+
{
200+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("Only server can unspawn objects");
201+
return;
202+
}
203+
else if (!netManager.NetworkConfig.HandleObjectSpawning)
204+
{
205+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("NetworkConfig is set to not handle object spawning");
206+
return;
207+
}
208+
209+
OnDestroyObject(netObject.networkId, false);
210+
}
211+
191212
internal static void SpawnObject(NetworkedObject netObject, uint? clientOwnerId = null)
192213
{
193214
if (netObject.isSpawned)
@@ -258,6 +279,8 @@ internal static void OnDestroyObject(uint networkId, bool destroyGameObject)
258279
NetworkingManager.singleton.connectedClients[spawnedObjects[networkId].OwnerClientId].OwnedObjects.RemoveAll(x => x.NetworkId == networkId);
259280
}
260281
GameObject go = spawnedObjects[networkId].gameObject;
282+
spawnedObjects[networkId]._isSpawned = false;
283+
261284
if (netManager != null && netManager.isServer)
262285
{
263286
releasedNetworkObjectIds.Push(networkId);

0 commit comments

Comments
 (0)