Skip to content

Commit 7f22a2a

Browse files
fix: missed fixing unspawn method for mirror (#5)
* fix: missed conversion to handle unspawning.
1 parent cf5afba commit 7f22a2a

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

Assets/Object Pooler/Runtime/ObjectPooling.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
using System.Collections.Generic;
2-
using UnityEngine;
1+
#region Statements
2+
33
#if MIRAGE
44
using Mirage;
55
#else
66
using Mirror;
77
#endif
8+
using System.Collections.Generic;
9+
using UnityEngine;
10+
11+
#endregion
812

913
namespace Object_Pooler
1014
{
@@ -16,15 +20,15 @@ public struct PooledObject<T>
1620

1721
internal struct ObjectPooling<T> where T : Object
1822
{
19-
#region Fields
23+
#region Fields
2024

2125
private readonly Queue<PooledObject<T>> _inactiveGameObjects;
2226
private readonly T _objectPrefabToPool;
2327
private readonly NetworkIdentity _networkObjectPrefabToPool;
2428
internal readonly HashSet<int> PooledObjectIDs;
2529
private readonly GameObject _parentBackObject;
2630

27-
#endregion
31+
#endregion
2832

2933
/// <summary>
3034
/// Constructor for this object pool.
@@ -56,7 +60,7 @@ public ObjectPooling(NetworkIdentity poolObject, int initialPool, GameObject net
5660
_objectPrefabToPool = null;
5761
}
5862

59-
#region Network Pool Spawning
63+
#region Network Pool Spawning
6064

6165
/// <summary>
6266
/// Method to spawn network object from our inactive pool.
@@ -82,7 +86,9 @@ public NetworkIdentity NetworkSpawn(Vector3 position, Quaternion rotation)
8286
spawnedObject = _inactiveGameObjects.Dequeue().NetId;
8387

8488
if (spawnedObject is null)
89+
{
8590
continue;
91+
}
8692

8793
break;
8894
}
@@ -112,7 +118,9 @@ public NetworkIdentity NetworkSpawn(Transform parentTransform, bool worldStayPos
112118
spawnedObject = _inactiveGameObjects.Dequeue().NetId;
113119

114120
if (spawnedObject is null)
121+
{
115122
continue;
123+
}
116124

117125
spawnedObject.transform.SetParent(parentTransform, worldStayPosition);
118126

@@ -133,7 +141,10 @@ public NetworkIdentity NetworkSpawn(Transform parentTransform, bool worldStayPos
133141
/// <param name="obj">The object we want to return back to the pool.</param>
134142
internal void Despawn(NetworkIdentity obj)
135143
{
136-
if (!obj.gameObject.activeInHierarchy) return;
144+
if (!obj.gameObject.activeInHierarchy)
145+
{
146+
return;
147+
}
137148

138149
obj.gameObject.SetActive(false);
139150

@@ -147,9 +158,9 @@ internal void Despawn(NetworkIdentity obj)
147158
_inactiveGameObjects.Enqueue(recycle);
148159
}
149160

150-
#endregion
161+
#endregion
151162

152-
#region Normal Pool Spawning.
163+
#region Normal Pool Spawning.
153164

154165
/// <summary>
155166
/// Method to spawn an object from our inactive pool.
@@ -175,7 +186,9 @@ public object Spawn(Vector3 position, Quaternion rotation)
175186
spawnedObject = _inactiveGameObjects.Dequeue().Object;
176187

177188
if (spawnedObject is null)
189+
{
178190
continue;
191+
}
179192

180193
break;
181194
}
@@ -205,7 +218,9 @@ public T Spawn(Transform parentTransform, bool worldStayPosition)
205218
spawnedObject = _inactiveGameObjects.Dequeue().Object;
206219

207220
if (spawnedObject is null)
221+
{
208222
continue;
223+
}
209224

210225
(spawnedObject as MonoBehaviour)?.transform.SetParent(parentTransform, worldStayPosition);
211226

@@ -262,6 +277,6 @@ internal void Despawn(T obj)
262277
_inactiveGameObjects.Enqueue(recycle);
263278
}
264279

265-
#endregion
280+
#endregion
266281
}
267282
}

Assets/Object Pooler/Runtime/ObjectPoolingManager.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ internal GameObject SpawnObject(Vector3 position, Guid assetId)
9797
/// Spawn handler for all network unspawning of things.
9898
/// </summary>
9999
/// <param name="spawned">What network identity we are going to unspawn and return back to network pool.</param>
100+
#if MIRAGE
100101
internal void UnSpawnObject(NetworkIdentity spawned)
102+
#else
103+
internal void UnSpawnObject(GameObject spawned)
104+
#endif
101105
{
102106
ObjectPooling<NetworkIdentity> p = default;
103107

@@ -109,8 +113,12 @@ internal void UnSpawnObject(NetworkIdentity spawned)
109113

110114
break;
111115
}
116+
#if MIRAGE
112117

113118
p.Despawn(spawned);
119+
#else
120+
p.Despawn(spawned.GetComponent<NetworkIdentity>());
121+
#endif
114122
}
115123

116124
/// <summary>

0 commit comments

Comments
 (0)