Skip to content

Commit 96dc2fa

Browse files
committed
update api
1 parent a5cdeab commit 96dc2fa

23 files changed

+753
-148
lines changed

Runtime/Extension/CollectionExtension.cs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Text;
45

@@ -31,11 +32,6 @@ public static string ToStringCollection<T>(this T[] target)
3132
return _stringBuilder.ToString();
3233
}
3334

34-
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> target)
35-
{
36-
return new HashSet<T>(target);
37-
}
38-
3935
public static IEnumerable<T> Foreach<T>(this IEnumerable<T> target, System.Action<T> OnEach)
4036
{
4137
foreach (var item in target)
@@ -46,6 +42,17 @@ public static IEnumerable<T> Foreach<T>(this IEnumerable<T> target, System.Actio
4642
return target;
4743
}
4844

45+
public static bool IsEmpty<T>(this T[] target)
46+
{
47+
return target == null || target.Length == 0;
48+
}
49+
50+
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> target)
51+
{
52+
return new HashSet<T>(target);
53+
}
54+
55+
4956
public static T Dequeue<T>(this List<T> target)
5057
{
5158
int index = 0;
@@ -63,5 +70,52 @@ public static T Pop<T>(this List<T> target)
6370

6471
return item;
6572
}
73+
74+
public static Dictionary<TKey, TSource> ToDictionaryEx<TSource, TKey>(this IEnumerable<TSource> source, System.Func<TSource, TKey> keySelector)
75+
=> ToDictionaryEx(source, keySelector, UnityEngine.Debug.LogError);
76+
77+
public static Dictionary<TKey, TSource> ToDictionaryEx<TSource, TKey>(this IEnumerable<TSource> source, System.Func<TSource, TKey> keySelector, System.Action<string> OnError)
78+
{
79+
Dictionary<TKey, TSource> dictionary = new Dictionary<TKey, TSource>();
80+
foreach (var eachSource in source)
81+
{
82+
TKey key = keySelector(eachSource);
83+
if (dictionary.ContainsKey(key))
84+
{
85+
OnError($"{nameof(ToDictionaryEx)} already ContainKey, key:{key}, added Source:{dictionary[key]}, try add source:{eachSource}");
86+
continue;
87+
}
88+
dictionary.Add(key, eachSource);
89+
}
90+
91+
return dictionary;
92+
}
93+
94+
public static bool TryGetValueEx<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> target, TKey key, out TValue value)
95+
=> TryGetValueEx(target, key, out value, UnityEngine.Debug.LogError);
96+
97+
public static bool TryGetValueEx<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> target, TKey key, out TValue value, System.Action<string> OnError)
98+
{
99+
value = default;
100+
if (target == null)
101+
{
102+
OnError($"Dictionary is null, key:{key}");
103+
return false;
104+
}
105+
106+
if (key == null)
107+
{
108+
OnError($"key is null value");
109+
return false;
110+
}
111+
112+
bool result = target.TryGetValue(key, out value);
113+
if (!result)
114+
{
115+
OnError($"key:{key} not found in dictionary");
116+
}
117+
118+
return result;
119+
}
66120
}
67-
}
121+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using UnityEngine;
2+
3+
public static class JsonUtilityEx
4+
{
5+
public static bool TryFromJson<T>(string json, out T result)
6+
=> TryFromJson(json, out result, Debug.LogError);
7+
8+
public static bool TryFromJson<T>(string json, out T result, System.Action<string> OnError)
9+
{
10+
result = default;
11+
bool isSuccess = true;
12+
13+
try
14+
{
15+
result = JsonUtility.FromJson<T>(json);
16+
}
17+
catch (System.Exception error)
18+
{
19+
OnError(error.ToString());
20+
isSuccess = false;
21+
}
22+
23+
return isSuccess;
24+
}
25+
}

Runtime/UnitytComponentPool.cs.meta renamed to Runtime/Extension/JsonUtilityExtension.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Extension/MonoBehaviourEx.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public static class MonoBehaviourEx
6+
{
7+
public static void StartCoroutineNotOverlap(this MonoBehaviour target, string coroutineName)
8+
{
9+
target.StopCoroutine(coroutineName);
10+
target.StartCoroutine(coroutineName);
11+
}
12+
13+
public static T GetOrAddComponent<T>(this Component target)
14+
where T : Component
15+
=> target.gameObject.GetOrAddComponent<T>();
16+
17+
public static T GetOrAddComponent<T>(this GameObject target)
18+
where T : Component
19+
{
20+
T returnComponent = target.GetComponent<T>();
21+
if (returnComponent != null)
22+
{
23+
return returnComponent;
24+
}
25+
26+
return target.AddComponent<T>();
27+
}
28+
}

Runtime/Extension/MonoBehaviourEx.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.

Runtime/Extension/RandomExtension.cs

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,72 @@ public static T Random<T>(this IEnumerable<T> target)
1515
return target.ElementAt(randomIndex);
1616
}
1717

18-
public static T Random<T>(this IEnumerable<T> target, System.Func<T, int> getPercentage)
18+
public static T Random<T>(this T[] target, System.Func<T, int, int> getPercentage)
1919
{
20-
int totalvariable = 0;
21-
target.Foreach(item => totalvariable += getPercentage(item));
22-
int random = Next(0, totalvariable);
20+
int totalVariable = 0;
21+
for (int i = 0; i < target.Length; i++)
22+
{
23+
totalVariable += getPercentage(target[i], i);
24+
}
25+
int random = Next(totalVariable);
2326

24-
totalvariable = 0;
25-
foreach (T item in target)
27+
totalVariable = 0;
28+
for (int i = 0; i < target.Length; i++)
2629
{
27-
totalvariable += getPercentage(item);
28-
if (random < totalvariable)
30+
totalVariable += getPercentage(target[i], i);
31+
if (random < totalVariable)
2932
{
30-
return item;
33+
return target[i];
3134
}
3235
}
3336

3437
return default;
3538
}
3639

37-
public static T Random<T>(this IEnumerable<T> target, System.Func<T, bool> onFilter)
40+
public static T Random<T>(this IReadOnlyList<T> target, System.Func<T, int, int> getPercentage)
41+
{
42+
int totalVariable = 0;
43+
for (int i = 0; i < target.Count; i++)
44+
{
45+
totalVariable += getPercentage(target[i], i);
46+
}
47+
int random = Next(totalVariable);
48+
49+
totalVariable = 0;
50+
for (int i = 0; i < target.Count; i++)
51+
{
52+
totalVariable += getPercentage(target[i], i);
53+
if (random < totalVariable)
54+
{
55+
return target[i];
56+
}
57+
}
58+
59+
return default;
60+
}
61+
62+
public static T Random<T>(this IReadOnlyList<T> target, System.Func<T, int, float> getPercentage)
63+
{
64+
float totalVariable = 0;
65+
for (int i = 0; i < target.Count; i++)
66+
{
67+
totalVariable += getPercentage(target[i], i);
68+
}
69+
float random = Next(totalVariable);
70+
71+
totalVariable = 0;
72+
for (int i = 0; i < target.Count; i++)
73+
{
74+
totalVariable += getPercentage(target[i], i);
75+
if (random < totalVariable)
76+
{
77+
return target[i];
78+
}
79+
}
80+
81+
return default;
82+
}
83+
public static T Random<T>(this IReadOnlyList<T> target, System.Func<T, bool> onFilter)
3884
{
3985
IEnumerable<T> filteredTarget = target.Where(onFilter);
4086
int randomIndex = Next(0, filteredTarget.Count());
@@ -65,11 +111,7 @@ public static int Next()
65111
/// </summary>
66112
/// <param name="max">최대값, 랜덤값은 이 값이 될 수 없음</param>
67113
/// <returns></returns>
68-
public static int Next(int max)
69-
{
70-
InitRandom();
71-
return _local.Next(max);
72-
}
114+
public static int Next(int max) => Next(0, max);
73115

74116
/// <summary>
75117
/// 범위형 int 랜덤
@@ -83,6 +125,24 @@ public static int Next(int min, int max)
83125
return _local.Next(min, max);
84126
}
85127

128+
/// <summary>
129+
/// 범위형 float 랜덤
130+
/// </summary>
131+
/// <param name="max">최대값, 랜덤값은 이 값이 될 수 없음</param>
132+
/// <returns></returns>
133+
public static float Next(float max) => Next(0f, max);
134+
135+
/// <summary>
136+
/// 범위형 float 랜덤
137+
/// </summary>
138+
/// <param name="max">최대값, 랜덤값은 이 값이 될 수 없음</param>
139+
/// <returns></returns>
140+
public static float Next(float min, float max)
141+
{
142+
InitRandom();
143+
return (float)_local.NextDouble() * (max - min) + min;
144+
}
145+
86146
private static void InitRandom()
87147
{
88148
if (_local == null)

Runtime/SimplePool.cs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using UnityEngine;
45

56
namespace UNKO.Utils
67
{
7-
public class SimplePool<T>
8+
public class SimplePool<T> : IDisposable
89
where T : class
910
{
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;
11+
[SerializeField]
12+
protected List<T> _allInstance = new List<T>(); public IReadOnlyList<T> AllInstance => _allInstance;
13+
[SerializeField]
14+
protected List<T> _use = new List<T>(); public IReadOnlyList<T> Use => _use;
15+
[SerializeField]
16+
protected List<T> _notUse = new List<T>(); public IReadOnlyList<T> NotUse => _notUse;
1317

14-
protected T _originItem { get; private set; }
18+
[SerializeField]
19+
private T _originItem = null; public T OriginItem => _originItem;
1520
protected Func<T, T> _OnCreateInstance;
1621

1722
public SimplePool(T originItem)
@@ -38,9 +43,22 @@ public SimplePool(Func<T> onCreateInstance, int initializeSize)
3843
Init(onCreateInstance(), initializeSize);
3944
}
4045

46+
public void PrePooling(int prePoolCount)
47+
{
48+
for (int i = 0; i < prePoolCount; i++)
49+
{
50+
Spawn();
51+
}
52+
53+
DeSpawnAll();
54+
}
55+
56+
public bool IsEmptyPool()
57+
=> _notUse.Count == 0 && _allInstance.Count > 0;
58+
4159
public T Spawn()
4260
{
43-
T spawnItem = null;
61+
T spawnItem;
4462
if (_notUse.Count > 0)
4563
{
4664
int lastIndex = _notUse.Count - 1;
@@ -49,7 +67,7 @@ public T Spawn()
4967
}
5068
else
5169
{
52-
spawnItem = OnRequireNewInstance(_originItem);
70+
spawnItem = OnRequireNewInstance(OriginItem);
5371
_allInstance.Add(spawnItem);
5472
}
5573

@@ -60,6 +78,12 @@ public T Spawn()
6078

6179
public void DeSpawn(T item)
6280
{
81+
if (item == null)
82+
{
83+
Debug.LogError($"despawn item is null");
84+
return;
85+
}
86+
6387
if (_use.Contains(item) == false)
6488
{
6589
return;
@@ -78,19 +102,25 @@ public void DeSpawnAll()
78102
}
79103
}
80104

105+
public virtual void OnDisposeItem(T item) { }
106+
81107
protected virtual T OnRequireNewInstance(T originItem) => _OnCreateInstance(originItem);
82108
protected virtual void OnSpawn(T spawnTarget) { }
83109
protected virtual void OnDespawn(T despawnTarget) { }
84110

85111
protected void Init(T originItem, int initializeSize)
86112
{
87113
_originItem = originItem;
114+
PrePooling(initializeSize);
115+
}
88116

89-
for (int i = 0; i < initializeSize; i++)
90-
{
91-
Spawn();
92-
}
93-
DeSpawnAll();
117+
public virtual void Dispose()
118+
{
119+
_allInstance.Foreach(OnDisposeItem);
120+
_allInstance.Clear();
121+
122+
_use.Clear();
123+
_notUse.Clear();
94124
}
95125
}
96126
}

0 commit comments

Comments
 (0)