Skip to content

Commit

Permalink
feat: spawn an object by network identity ref (#561)
Browse files Browse the repository at this point in the history
* feat: spawn an object by network identity ref

* add test
  • Loading branch information
uweeby committed Feb 3, 2021
1 parent 5639343 commit 87a520d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Assets/Mirror/Runtime/ServerObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,15 @@ public void Spawn(GameObject obj, GameObject ownerPlayer)
Spawn(obj, identity.ConnectionToClient);
}

/// <summary>
/// This spawns an object.
/// </summary>
/// <param name="identity">The identity to spawn.</param>
public void Spawn(NetworkIdentity identity)
{
Spawn(identity.gameObject, identity.ConnectionToClient);
}

/// <summary>
/// This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client.
/// <para>This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object.</para>
Expand Down
8 changes: 8 additions & 0 deletions Assets/Tests/Runtime/ClientServer/ServerObjectManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public void SpawnNoIdentExceptionTest()
Assert.That(ex.Message, Is.EqualTo("Player object has no NetworkIdentity"));
}

[UnityTest]
public IEnumerator SpawnByIdentityTest() => UniTask.ToCoroutine(async () =>
{
serverObjectManager.Spawn(serverIdentity);
await AsyncUtil.WaitUntilWithTimeout(() => serverIdentity.Server == server);
});

[Test]
public void SpawnNotPlayerExceptionTest()
{
Expand Down

0 comments on commit 87a520d

Please sign in to comment.