Skip to content

Commit d3c7b2b

Browse files
committed
duplicate key pool fix
1 parent ebacbb5 commit d3c7b2b

File tree

4 files changed

+14
-19
lines changed

4 files changed

+14
-19
lines changed

Packages/ZBase.Foundation.Pooling/GameObjectLazyPool.Addressables/GlobalPools/GlobalAssetRefGameObjectPool.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ namespace ZBase.Foundation.Pooling.GameObjectItem.LazyPool
99
{
1010
public class GlobalAssetRefGameObjectPool : IPool, IShareable
1111
{
12-
private readonly Dictionary<AssetRefGameObjectPrefab, AssetRefGameObjectItemPool> _pools =
13-
new(new AssetRefGameObjectPrefabEqualityComparer());
14-
12+
private readonly Dictionary<AssetRefGameObjectPrefab, AssetRefGameObjectItemPool> _pools = new(new AssetRefGameObjectPrefabEqualityComparer());
1513
private readonly Dictionary<int, AssetRefGameObjectItemPool> _dicTrackingInstancePools = new();
16-
1714
private readonly Dictionary<AssetReferenceGameObject, AssetRefGameObjectPrefab> _poolKeyCache = new();
1815
private readonly Dictionary<string, AssetRefGameObjectPrefab> _poolStringKeyCache = new();
1916

@@ -28,9 +25,10 @@ public async UniTask<GameObject> Rent(string address)
2825

2926
public async UniTask<GameObject> Rent(AssetReferenceGameObject gameObjectReference)
3027
{
31-
if (!_poolKeyCache.TryGetValue(gameObjectReference, out var key))
32-
_poolKeyCache.Add(gameObjectReference,
33-
key = new AssetRefGameObjectPrefab { Source = gameObjectReference, });
28+
if (_poolKeyCache.TryGetValue(gameObjectReference, out var key))
29+
return await Rent(key);
30+
key = new AssetRefGameObjectPrefab { Source = gameObjectReference };
31+
this._poolKeyCache[gameObjectReference] = key;
3432
return await Rent(key);
3533
}
3634

@@ -44,7 +42,10 @@ public async UniTask<GameObject> Rent(AssetRefGameObjectPrefab gameObjectReferen
4442
this._pools.Add(gameObjectReference, pool);
4543
}
4644
GameObject item = await pool.Rent();
47-
_dicTrackingInstancePools.Add(item.GetInstanceID(), pool);
45+
var keyInstance = item.GetInstanceID();
46+
if (_dicTrackingInstancePools.ContainsKey(keyInstance))
47+
Debug.LogError("Duplicate key pool");
48+
_dicTrackingInstancePools[keyInstance]= pool;
4849
return item;
4950
}
5051

Packages/ZBase.Foundation.Pooling/ZBase.Foundation.Pooling.Addressables/ScriptablePools/ScriptableAssetRefGameObjectSource.cs

+3-9
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,11 @@ public override async UniTask<Object> InstantiateAsync(Transform parent,
1919
CancellationToken cancelToken = default)
2020
{
2121
var source = Source;
22-
2322
if (source == null)
2423
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
25-
26-
AsyncOperationHandle<GameObject> handle;
27-
28-
if (parent)
29-
handle = source.InstantiateAsync(parent, true);
30-
else
31-
handle = source.InstantiateAsync();
32-
24+
AsyncOperationHandle<GameObject> handle = default;
25+
if (source != null)
26+
handle = parent ? source.InstantiateAsync(parent, true) : source.InstantiateAsync();
3327
return await handle.WithCancellation(cancelToken);
3428
}
3529

Packages/ZBase.Foundation.Pooling/ZBase.Foundation.Pooling/ScriptablePools/Common/UnityObjectPrefab.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace ZBase.Foundation.Pooling.ScriptablePools
1010
{
1111
[Serializable]
12-
public class UnityObjectPrefab : IPrefab<UnityEngine.Object, ScriptableSource>
12+
public class UnityObjectPrefab : IPrefab<Object, ScriptableSource>
1313
{
1414
[SerializeField]
1515
private ScriptableSource _source;

Packages/ZBase.Foundation.Pooling/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.zbase.foundation.pooling",
33
"displayName": "ZBase.Foundation.Pooling",
4-
"version": "2.3.16",
4+
"version": "2.3.17",
55
"unity": "2021.3",
66
"documentationUrl": "https://github.com/WolffunGame/Unity.Pooling/blob/main/README.md",
77
"dependencies": {

0 commit comments

Comments
 (0)