Skip to content

Commit

Permalink
feat: adding destroy function that takes network identity
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Aug 10, 2021
1 parent 641b2b0 commit e91f6d3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Assets/Mirage/Runtime/NetworkIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ void OnDestroy()
// if it is still true, then we need to unspawn it
if (IsServer)
{
ServerObjectManager.Destroy(gameObject);
ServerObjectManager.Destroy(this);
}
}

Expand Down
50 changes: 33 additions & 17 deletions Assets/Mirage/Runtime/ServerObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,39 @@ static void ThrowIfPrefab(GameObject obj)
#endif
}

/// <summary>
/// Destroys this object and corresponding objects on all clients.
/// <param name="gameObject">Game object to destroy.</param>
/// <param name="destroyServerObject">Sets if server object will also be destroyed</param>
/// </summary>
public void Destroy(GameObject gameObject, bool destroyServerObject = true)
{
if (gameObject == null)
{
logger.Log("NetworkServer DestroyObject is null");
return;
}

NetworkIdentity identity = gameObject.GetNetworkIdentity();
DestroyObject(identity, destroyServerObject);
}

/// <summary>
/// Destroys this object and corresponding objects on all clients.
/// <param name="identity">Game object to destroy.</param>
/// <param name="destroyServerObject">Sets if server object will also be destroyed</param>
/// </summary>
public void Destroy(NetworkIdentity identity, bool destroyServerObject = true)
{
if (identity == null)
{
logger.Log("NetworkServer DestroyObject is null");
return;
}

DestroyObject(identity, destroyServerObject);
}

void DestroyObject(NetworkIdentity identity, bool destroyServerObject)
{
if (logger.LogEnabled()) logger.Log("DestroyObject instance:" + identity.NetId);
Expand All @@ -571,23 +604,6 @@ void DestroyObject(NetworkIdentity identity, bool destroyServerObject)
}
}

/// <summary>
/// Destroys this object and corresponding objects on all clients.
/// <param name="obj">Game object to destroy.</param>
/// <param name="persistServerObject">In some cases it is useful to remove an object but not delete it on the server.</param>
/// </summary>
public void Destroy(GameObject obj, bool destroyServerObject = true)
{
if (obj == null)
{
logger.Log("NetworkServer DestroyObject is null");
return;
}

NetworkIdentity identity = obj.GetNetworkIdentity();
DestroyObject(identity, destroyServerObject);
}

internal bool ValidateSceneObject(NetworkIdentity identity)
{
if (identity.gameObject.hideFlags == HideFlags.NotEditable ||
Expand Down

0 comments on commit e91f6d3

Please sign in to comment.