Skip to content

Commit fb0e61a

Browse files
committed
unitycomponent pool 추가
1 parent bd0af4f commit fb0e61a

File tree

9 files changed

+97
-11
lines changed

9 files changed

+97
-11
lines changed

LICENSE.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/DataSender/DataSenderUnityExtension.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ namespace UNKO.Utils
66
{
77
public static class DataSenderUnityExtension
88
{
9+
public static DataSender<T> InitComponents<T>(this DataSender<T> target, MonoBehaviour owner)
10+
{
11+
IObserver<T>[] our = owner.GetComponents<IObserver<T>>();
12+
target.Subscribe(our);
13+
14+
return target;
15+
}
16+
917
public static DataSender<T> InitChildrenComponents<T>(this DataSender<T> target, MonoBehaviour owner)
1018
{
1119
IObserver<T>[] children = owner.GetComponentsInChildren<IObserver<T>>();

Runtime/SimplePool.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ namespace UNKO.Utils
77
public class SimplePool<T>
88
where T : class
99
{
10-
public int instanceCount => _allInstance.Count;
11-
public int useCount => _use.Count;
12-
public int notUseCount => _notUse.Count;
10+
protected List<T> _allInstance = new List<T>(); public IReadOnlyList<T> allInstance => _allInstance;
11+
protected List<T> _use = new List<T>(); public IReadOnlyList<T> use => _use;
12+
protected List<T> _notUse = new List<T>(); public IReadOnlyList<T> notUse => _notUse;
1313

14-
protected List<T> _allInstance = new List<T>();
15-
protected HashSet<T> _use = new HashSet<T>();
16-
protected List<T> _notUse = new List<T>();
1714
protected T _originItem { get; private set; }
1815
protected Func<T, T> _OnCreateInstance;
1916

Runtime/SingletonComponentBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public static T instance
2525
private static T s_instance;
2626
protected static bool s_isQuitApp { get; private set; } = false;
2727

28+
void Awake()
29+
{
30+
if (s_instance == null)
31+
InitSingleton();
32+
}
33+
2834
protected virtual void InitSingleton()
2935
{
3036
}

Runtime/UnitytComponentPool.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace UNKO.Utils
5+
{
6+
public class UnitytComponentPool<T> : SimplePool<T>
7+
where T : UnityEngine.Component
8+
{
9+
Transform _parent;
10+
11+
public UnitytComponentPool(T originItem, int initializeSize = 0) : base(originItem, initializeSize)
12+
{
13+
}
14+
15+
public UnitytComponentPool(Func<T> onCreateInstance, int initializeSize = 0) : base(onCreateInstance, initializeSize)
16+
{
17+
}
18+
19+
public UnitytComponentPool<T> SetParents(Transform parent)
20+
{
21+
_parent = parent;
22+
23+
return this;
24+
}
25+
26+
protected override T OnRequireNewInstance(T originItem)
27+
{
28+
T newInstance = base.OnRequireNewInstance(originItem);
29+
if (_parent != null)
30+
newInstance.transform.SetParent(_parent);
31+
32+
newInstance.gameObject.SetActive(false);
33+
return newInstance;
34+
}
35+
36+
protected override void OnSpawn(T spawnTarget)
37+
{
38+
base.OnSpawn(spawnTarget);
39+
40+
spawnTarget.gameObject.SetActive(true);
41+
}
42+
}
43+
}

Runtime/UnitytComponentPool.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SUMMARY.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/Runtime/SimplePoolTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void 생성자에서_미리풀에생성할수있습니다()
4040
int instanceCount = Random.Range(1, 10);
4141
SimplePool<SimplePoolTarget> pool = new SimplePool<SimplePoolTarget>(origin, instanceCount);
4242

43-
Assert.AreEqual(pool.instanceCount, instanceCount);
43+
Assert.AreEqual(pool.allInstance.Count, instanceCount);
4444
Assert.AreEqual(SimplePoolTarget.instanceCount, instanceCount);
4545
}
4646

@@ -62,9 +62,9 @@ public void 사용예시()
6262
pool.DeSpawn(item);
6363
}
6464

65-
Assert.AreEqual(pool.instanceCount, totalInstanceCount);
66-
Assert.AreEqual(pool.useCount, 0);
67-
Assert.AreEqual(pool.notUseCount, totalInstanceCount);
65+
Assert.AreEqual(pool.allInstance.Count, totalInstanceCount);
66+
Assert.AreEqual(pool.use.Count, 0);
67+
Assert.AreEqual(pool.notUse.Count, totalInstanceCount);
6868
}
6969

7070
public class PoolEx : SimplePool<SimplePoolTarget>
@@ -85,7 +85,7 @@ public void 사용예시_생성자Override()
8585
SimplePoolTarget.Reset_InstanceCount();
8686
int instanceCount = Random.Range(1, 10);
8787
PoolEx poolEx = new PoolEx(instanceCount);
88-
Assert.AreEqual(poolEx.instanceCount, instanceCount);
88+
Assert.AreEqual(poolEx.allInstance.Count, instanceCount);
8989

9090
SimplePoolTarget target = poolEx.Spawn();
9191
Assert.AreEqual(target.isCreateFromFactory, true);

0 commit comments

Comments
 (0)