public void DestroyPlayerObjects(int playerId, bool localOnly)
{
if (playerId <= 0)
{
Debug.LogError("Failed to Destroy objects of playerId: " + playerId);
return;
}
if (!localOnly)
{
// clean server's Instantiate and RPC buffers
this.OpRemoveFromServerInstantiationsOfPlayer(playerId);
this.OpCleanRpcBuffer(playerId);
// send Destroy(player) to anyone else
this.SendDestroyOfPlayer(playerId);
}
// locally cleaning up that player's objects
HashSet<GameObject> playersGameObjects = new HashSet<GameObject>();
foreach (PhotonView view in this.photonViewList.Values)
{
if (view.CreatorActorNr == playerId)
{
playersGameObjects.Add(view.gameObject);
}
}
// any non-local work is already done, so with the list of that player's objects, we can clean up (locally only)
foreach (GameObject gameObject in playersGameObjects)
{
this.RemoveInstantiatedGO(gameObject, true);
}
// with ownership transfer, some objects might lose their owner.
// in that case, the creator becomes the owner again. every client can apply this. done below.
foreach (PhotonView view in this.photonViewList.Values)
{
if (view.ownerId == playerId)
{
view.ownerId = view.CreatorActorNr;
//Debug.Log("Creator is: " + view.ownerId);
}
}
}