Skip to content

Commit a3aa11a

Browse files
authored
Despawn method now 4 times faster in terms of performance than before
1 parent dfd245f commit a3aa11a

File tree

1 file changed

+3
-19
lines changed

1 file changed

+3
-19
lines changed

Runtime/PoolExtensions.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ public static T Spawn<T>(this GameObject prefab, Vector3 position, Quaternion ro
5454
public static T Spawn<T>(this GameObject prefab, Vector3 position, Quaternion rotation, Transform parent, bool spawnInWorldSpace) where T : Component =>
5555
prefab.Spawn(position, rotation, parent, spawnInWorldSpace).GetComponent<T>();
5656

57-
/// <summary>
58-
/// Use this method only with Instances of Prefab
59-
/// </summary>
6057
public static void Despawn(this Poolable instance)
6158
{
6259
if (!instance.IsPooled)
@@ -68,27 +65,14 @@ public static void Despawn(this Poolable instance)
6865
instance.ReturnToPool();
6966
}
7067

71-
/// <summary>
72-
/// Use this method only with Instances of Prefab. Slow in terms of perforamance, use Despawn with Poolable param instead
73-
/// </summary>
7468
public static void Despawn(this GameObject instance)
7569
{
76-
var poolable = instance.GetComponent<Poolable>();
70+
var isPooled = Poolable.IsInstancePooled(instance);
7771

78-
if (poolable != null)
79-
{
80-
if (!poolable.IsPooled)
81-
{
82-
Object.Destroy(instance);
83-
return;
84-
}
85-
86-
poolable.ReturnToPool();
87-
}
72+
if (isPooled)
73+
instance.GetComponent<Poolable>().ReturnToPool();
8874
else
89-
{
9075
Object.Destroy(instance);
91-
}
9276
}
9377
}
9478
}

0 commit comments

Comments
 (0)