-
-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7766ed2
commit ea2af34
Showing
6 changed files
with
153 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,103 @@ | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
|
||
namespace Mirror.Examples.NetworkRoom | ||
{ | ||
internal class Spawner | ||
{ | ||
static GameObject prefab; | ||
static byte poolSize = 10; | ||
static Pool<GameObject> pool; | ||
static ushort counter; | ||
|
||
internal static void InitializePool(GameObject poolPrefab, byte count) | ||
{ | ||
prefab = poolPrefab; | ||
poolSize = count; | ||
|
||
NetworkClient.RegisterPrefab(prefab, SpawnHandler, UnspawnHandler); | ||
pool = new Pool<GameObject>(CreateNew, poolSize); | ||
} | ||
|
||
internal static void ClearPool() | ||
{ | ||
if (prefab == null) return; | ||
|
||
NetworkClient.UnregisterPrefab(prefab); | ||
|
||
if (pool == null) return; | ||
|
||
// destroy all objects in pool | ||
while (pool.Count > 0) | ||
Object.Destroy(pool.Get()); | ||
|
||
counter = 0; | ||
pool = null; | ||
} | ||
|
||
static GameObject SpawnHandler(SpawnMessage msg) => Get(msg.position, msg.rotation); | ||
|
||
static void UnspawnHandler(GameObject spawned) => Return(spawned); | ||
|
||
static GameObject CreateNew() | ||
{ | ||
// use this object as parent so that objects dont crowd hierarchy | ||
GameObject next = Object.Instantiate(prefab); | ||
counter++; | ||
next.name = $"{prefab.name}_pooled_{counter:00}"; | ||
next.SetActive(false); | ||
return next; | ||
} | ||
|
||
public static GameObject Get(Vector3 position, Quaternion rotation) | ||
{ | ||
GameObject next = pool.Get(); | ||
|
||
// set position/rotation and set active | ||
next.transform.position = position; | ||
next.transform.rotation = rotation; | ||
next.SetActive(true); | ||
return next; | ||
} | ||
|
||
// Used to put object back into pool so they can b | ||
// Should be used on server after unspawning an object | ||
// Used on client by NetworkClient to unspawn objects | ||
public static void Return(GameObject spawned) | ||
{ | ||
// disable object | ||
spawned.SetActive(false); | ||
|
||
// add back to pool | ||
pool.Return(spawned); | ||
} | ||
|
||
[ServerCallback] | ||
internal static void InitialSpawn() | ||
{ | ||
for (int i = 0; i < 10; i++) | ||
for (byte i = 0; i < poolSize; i++) | ||
SpawnReward(); | ||
} | ||
|
||
[ServerCallback] | ||
internal static void SpawnReward() | ||
{ | ||
Vector3 spawnPosition = new Vector3(Random.Range(-19, 20), 1, Random.Range(-19, 20)); | ||
NetworkServer.Spawn(Object.Instantiate(NetworkRoomManagerExt.singleton.rewardPrefab, spawnPosition, Quaternion.identity)); | ||
NetworkServer.Spawn(Get(spawnPosition, Quaternion.identity)); | ||
} | ||
|
||
[ServerCallback] | ||
internal static async void RecycleReward(GameObject reward) | ||
{ | ||
NetworkServer.UnSpawn(reward); | ||
Return(reward); | ||
await DelayedSpawn(); | ||
} | ||
|
||
static async Task DelayedSpawn() | ||
{ | ||
await Task.Delay(new System.TimeSpan(0, 0, 1)); | ||
SpawnReward(); | ||
} | ||
} | ||
} |