Skip to content

Commit 63be06e

Browse files
committed
edit
1 parent 4e2c05d commit 63be06e

File tree

8 files changed

+80
-8
lines changed

8 files changed

+80
-8
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/DataSender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace UNKO.Utils
77
public class DataSender<T> : IObservable<T>, IDisposable
88
{
99
protected HashSet<IObserver<T>> _observers = new HashSet<IObserver<T>>();
10-
protected SimplePool<Unsubscriber<T>> _pool = new SimplePool<Unsubscriber<T>>();
10+
protected SimplePool<Unsubscriber<T>> _pool = new SimplePool<Unsubscriber<T>>(new Unsubscriber<T>());
1111
protected T _lastSendedData = default;
1212

1313
public void SendData(T data)

Runtime/SimplePool.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ public class SimplePool<T>
1515
protected HashSet<T> _use = new HashSet<T>();
1616
protected List<T> _notUse = new List<T>();
1717
protected T _originItem { get; private set; }
18+
protected Func<T, T> _OnCreateInstance;
1819

19-
public SimplePool(int initializeSize = 0)
20+
public SimplePool(T originItem, int initializeSize = 0)
2021
{
21-
Init(Activator.CreateInstance<T>(), initializeSize);
22+
_OnCreateInstance = (origin) => Activator.CreateInstance<T>();
23+
Init(originItem, initializeSize);
2224
}
2325

24-
public SimplePool(T originItem, int initializeSize = 0)
26+
public SimplePool(Func<T> onCreateInstance, int initializeSize = 0)
2527
{
26-
Init(originItem, initializeSize);
28+
_OnCreateInstance = (origin) => onCreateInstance();
29+
Init(onCreateInstance(), initializeSize);
2730
}
2831

2932
public T Spawn()
@@ -62,7 +65,7 @@ public void DeSpawnAll()
6265
DeSpawn(_use.Last());
6366
}
6467

65-
protected virtual T OnRequireNewInstance(T originItem) => Activator.CreateInstance<T>();
68+
protected virtual T OnRequireNewInstance(T originItem) => _OnCreateInstance(originItem);
6669
protected virtual void OnSpawn(T spawnTarget) { }
6770
protected virtual void OnDespawn(T despawnTarget) { }
6871

Runtime/SingletonComponentBase.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using UnityEngine;
2+
3+
namespace UNKO.Utils
4+
{
5+
public abstract class SingletonComponentBase<T> : MonoBehaviour
6+
where T : SingletonComponentBase<T>
7+
{
8+
public static T instance
9+
{
10+
get
11+
{
12+
if (s_isQuitApp)
13+
return default;
14+
15+
if (s_instance == null)
16+
{
17+
s_instance = FindObjectOfType<T>();
18+
s_instance.InitSingleton();
19+
}
20+
21+
return s_instance;
22+
}
23+
}
24+
25+
private static T s_instance;
26+
protected static bool s_isQuitApp { get; private set; } = false;
27+
28+
protected virtual void InitSingleton()
29+
{
30+
}
31+
32+
private void OnApplicationQuit()
33+
{
34+
s_isQuitApp = true;
35+
}
36+
}
37+
}

Runtime/SingletonComponentBase.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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void 사용예시()
4949
{
5050
SimplePoolTarget.Reset_InstanceCount();
5151
int totalInstanceCount = 10;
52-
SimplePool<SimplePoolTarget> pool = new SimplePool<SimplePoolTarget>(totalInstanceCount);
52+
SimplePool<SimplePoolTarget> pool = new SimplePool<SimplePoolTarget>(new SimplePoolTarget(), totalInstanceCount);
5353
int loopCount = Random.Range(3, 10);
5454
for (int i = 0; i < loopCount; i++)
5555
{
@@ -69,7 +69,7 @@ public void 사용예시()
6969

7070
public class PoolEx : SimplePool<SimplePoolTarget>
7171
{
72-
public PoolEx(int initializeSize = 0) : base(initializeSize)
72+
public PoolEx(int initializeSize = 0) : base(new SimplePoolTarget(), initializeSize)
7373
{
7474
}
7575

0 commit comments

Comments
 (0)