Skip to content

Commit a81b5e1

Browse files
committed
기능 추가
1 parent 1b94634 commit a81b5e1

16 files changed

+279
-9
lines changed

Runtime/Extension/CollectionExtension.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static string ToStringCollection<T>(this T[] target)
3232
return _stringBuilder.ToString();
3333
}
3434

35-
public static IEnumerable<T> Foreach<T>(this IEnumerable<T> target, System.Action<T> OnEach)
35+
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> target, System.Action<T> OnEach)
3636
{
3737
foreach (var item in target)
3838
{
@@ -62,6 +62,11 @@ public static T Dequeue<T>(this List<T> target)
6262
return item;
6363
}
6464

65+
public static void Push<T>(this List<T> target, T addedItem)
66+
{
67+
target.Add(addedItem);
68+
}
69+
6570
public static T Pop<T>(this List<T> target)
6671
{
6772
int index = target.Count - 1;
@@ -71,6 +76,14 @@ public static T Pop<T>(this List<T> target)
7176
return item;
7277
}
7378

79+
public static T Peek<T>(this List<T> target)
80+
{
81+
int index = target.Count - 1;
82+
T item = target[index];
83+
84+
return item;
85+
}
86+
7487
public static Dictionary<TKey, TSource> ToDictionaryEx<TSource, TKey>(this IEnumerable<TSource> source, System.Func<TSource, TKey> keySelector)
7588
=> ToDictionaryEx(source, keySelector, UnityEngine.Debug.LogError);
7689

@@ -91,6 +104,12 @@ public static Dictionary<TKey, TSource> ToDictionaryEx<TSource, TKey>(this IEnum
91104
return dictionary;
92105
}
93106

107+
public static TValue GetValueEx<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> target, TKey key)
108+
{
109+
target.TryGetValueEx(key, out TValue value);
110+
return value;
111+
}
112+
94113
public static bool TryGetValueEx<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> target, TKey key, out TValue value)
95114
=> TryGetValueEx(target, key, out value, UnityEngine.Debug.LogError);
96115

@@ -117,5 +136,15 @@ public static bool TryGetValueEx<TKey, TValue>(this IReadOnlyDictionary<TKey, TV
117136

118137
return result;
119138
}
139+
140+
public static bool IsNullOrZeroCount<T>(this IEnumerable<T> target)
141+
{
142+
return target == null || target.Count() == 0;
143+
}
144+
145+
public static bool IsNullOrZeroCount(this Array target)
146+
{
147+
return target == null || target.Length == 0;
148+
}
120149
}
121150
}

Runtime/Extension/MonoBehaviourEx.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,17 @@ public static T GetOrAddComponent<T>(this GameObject target)
2525

2626
return target.AddComponent<T>();
2727
}
28+
29+
public static void SetActive(this Component target, bool active)
30+
=> target.gameObject.SetActive(active);
31+
32+
public static bool IsNull(this GameObject target)
33+
{
34+
return target is null;
35+
}
36+
37+
public static bool IsNull(this Component target)
38+
{
39+
return target is null;
40+
}
2841
}

Runtime/Extension/StringExtension.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace UNKO.Utils
5+
{
6+
public static class StringExtension
7+
{
8+
public static void CutString(this string cutTarget, System.Action<string> OnCutString)
9+
=> CutString(cutTarget, 300, OnCutString);
10+
11+
public static void CutString(this string cutTarget, int maxCutLength, System.Action<string> OnCutString)
12+
{
13+
if (cutTarget == null)
14+
{
15+
return;
16+
}
17+
18+
string cutTargetCopy = cutTarget;
19+
do
20+
{
21+
int cutLength = Mathf.Min(cutTargetCopy.Length, maxCutLength);
22+
string cutString = cutTargetCopy.Substring(0, cutLength);
23+
OnCutString(cutString);
24+
25+
cutTargetCopy = cutTargetCopy.Substring(cutLength, cutTargetCopy.Length - cutLength);
26+
} while (cutTargetCopy.Length > 0);
27+
}
28+
29+
/// <summary>
30+
/// Calculate percentage similarity of two strings
31+
/// <param name="source">Source String to Compare with</param>
32+
/// <param name="target">Targeted String to Compare</param>
33+
/// <returns>Return Similarity between two strings from 0 to 1.0</returns>
34+
/// <para><see cref="https://stackoverflow.com/a/2344347"/></para>
35+
/// </summary>
36+
public static float CalculateSimilarity(this string source, string target)
37+
{
38+
if ((source == null) || (target == null)) return 0.0f;
39+
if ((source.Length == 0) || (target.Length == 0)) return 0.0f;
40+
if (source == target) return 1.0f;
41+
42+
int stepsToSame = ComputeLevenshteinDistance(source, target);
43+
return (1.0f - (stepsToSame / (float)Math.Max(source.Length, target.Length)));
44+
}
45+
/// <summary>
46+
/// Returns the number of steps required to transform the source string
47+
/// into the target string.
48+
/// </summary>
49+
public static int ComputeLevenshteinDistance(string source, string target)
50+
{
51+
if ((source == null) || (target == null)) return 0;
52+
if ((source.Length == 0) || (target.Length == 0)) return 0;
53+
if (source == target) return source.Length;
54+
55+
int sourceWordCount = source.Length;
56+
int targetWordCount = target.Length;
57+
58+
// Step 1
59+
if (sourceWordCount == 0)
60+
return targetWordCount;
61+
62+
if (targetWordCount == 0)
63+
return sourceWordCount;
64+
65+
int[,] distance = new int[sourceWordCount + 1, targetWordCount + 1];
66+
67+
// Step 2
68+
for (int i = 0; i <= sourceWordCount; distance[i, 0] = i++) ;
69+
for (int j = 0; j <= targetWordCount; distance[0, j] = j++) ;
70+
71+
for (int i = 1; i <= sourceWordCount; i++)
72+
{
73+
for (int j = 1; j <= targetWordCount; j++)
74+
{
75+
// Step 3
76+
int cost = (target[j - 1] == source[i - 1]) ? 0 : 1;
77+
78+
// Step 4
79+
distance[i, j] = Math.Min(Math.Min(distance[i - 1, j] + 1, distance[i, j - 1] + 1), distance[i - 1, j - 1] + cost);
80+
}
81+
}
82+
83+
return distance[sourceWordCount, targetWordCount];
84+
}
85+
}
86+
}

Runtime/Extension/StringExtension.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/SimplePool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected void Init(T originItem, int initializeSize)
116116

117117
public virtual void Dispose()
118118
{
119-
_allInstance.Foreach(OnDisposeItem);
119+
CollectionExtension.ForEach(_allInstance, this.OnDisposeItem);
120120
_allInstance.Clear();
121121

122122
_use.Clear();

Runtime/SingletonBase.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma warning disable IDE1006
2+
namespace UNKO.Utils
3+
{
4+
public abstract class SingletonBase<T>
5+
where T : SingletonBase<T>, new()
6+
{
7+
public static T instance
8+
{
9+
get
10+
{
11+
if (_instance == null)
12+
{
13+
_instance = new T();
14+
_instance.InitSingleton();
15+
}
16+
17+
return _instance;
18+
}
19+
}
20+
21+
private static T _instance { get; set; }
22+
23+
public virtual void InitSingleton()
24+
{
25+
}
26+
27+
static public T CreateInstance()
28+
{
29+
return instance;
30+
}
31+
}
32+
}
33+
#pragma warning restore IDE1006

Runtime/SingletonBase.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/SingletonComponentBase.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using UnityEngine;
1+
using System;
2+
using UnityEngine;
23

34
#pragma warning disable IDE1006
45
namespace UNKO.Utils
56
{
6-
public abstract class SingletonComponentBase<T> : MonoBehaviour
7+
public abstract class SingletonComponentBase<T> : MonoBehaviour, IDisposable
78
where T : SingletonComponentBase<T>
89
{
910
public static T instance
@@ -18,6 +19,12 @@ public static T instance
1819
if (_instance == null)
1920
{
2021
_instance = FindObjectOfType<T>();
22+
if (_instance == null)
23+
{
24+
GameObject newObject = new GameObject(typeof(T).Name);
25+
_instance = newObject.AddComponent<T>();
26+
}
27+
2128
if (_isInitSingleton == false)
2229
{
2330
_instance.InitSingleton();
@@ -40,6 +47,16 @@ protected virtual void Awake()
4047
}
4148
}
4249

50+
public static T GetOrCreateInstance()
51+
{
52+
return instance;
53+
}
54+
55+
public static void DestroySingleton()
56+
{
57+
_instance?.Dispose();
58+
}
59+
4360
public virtual void InitSingleton()
4461
{
4562
_isInitSingleton = true;
@@ -49,6 +66,16 @@ private void OnApplicationQuit()
4966
{
5067
_isQuitApp = true;
5168
}
69+
70+
public void Dispose()
71+
{
72+
if ((this is null) == false)
73+
{
74+
Destroy(gameObject);
75+
}
76+
_instance = null;
77+
_isInitSingleton = false;
78+
}
5279
}
5380
}
5481
#pragma warning restore IDE1006

Runtime/SingletonSOBase.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using UnityEngine;
2+
3+
namespace UNKO.Utils
4+
{
5+
public abstract class SingletonSOBase<T> : ScriptableObject
6+
where T : SingletonSOBase<T>
7+
{
8+
public static T instance
9+
{
10+
get
11+
{
12+
if (_instance == null)
13+
{
14+
_instance = Resources.Load<T>("");
15+
if (_instance == null)
16+
{
17+
Debug.LogError($"{nameof(T)} instance is null");
18+
}
19+
}
20+
21+
return _instance;
22+
}
23+
}
24+
25+
private static T _instance { get; set; }
26+
}
27+
}

Runtime/SingletonSOBase.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/SpriteAnimation/SpriteAnimation.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ public enum UpdateMode
3434
bool _isPlayOnEnable = false;
3535
[SerializeField]
3636
bool _isLoop = true;
37+
[SerializeField]
38+
bool _isDeactivate_WhenFinish = false;
3739

3840
// NOTE unity event는 보통 인스펙터 최하단에 있기 때문에 여기에 배치
3941
public UnityEvent<SpriteAnimation> OnStartAnimation = new UnityEvent<SpriteAnimation>();
42+
public UnityEvent<int> OnChangeSpriteIndex = new UnityEvent<int>();
4043
public UnityEvent<SpriteAnimation> OnFinishAnimation = new UnityEvent<SpriteAnimation>();
4144

4245
public bool IsPlaying { get; private set; }
@@ -117,6 +120,7 @@ public void SetAnimatedSprite(params Sprite[] sprites)
117120
public void SetIsLoop(bool isLoop) => _isLoop = isLoop;
118121
public void SetPlayOnEnable(bool enable) => _isPlayOnEnable = enable;
119122
public void SetUpdateMode(UpdateMode updateMode) => _updateMode = updateMode;
123+
public void SetDeactivate_WhenFinish(bool deactivate) => _isDeactivate_WhenFinish = deactivate;
120124

121125
private void OnEnable()
122126
{
@@ -149,13 +153,19 @@ IEnumerator PlayCoroutine(System.Func<float, object> getYield)
149153
OnStartAnimation.Invoke(this);
150154
while (spriteIndex < spriteCount)
151155
{
152-
UpdateRenderer(_animatedSprites[spriteIndex++]);
156+
OnChangeSpriteIndex.Invoke(spriteIndex);
157+
UpdateRenderer(_animatedSprites[spriteIndex]);
153158
yield return getYield(waitSecondsPerFrame);
159+
spriteIndex++;
154160
}
155161
OnFinishAnimation.Invoke(this);
156162
} while (IsLoop);
157163

158164
IsPlaying = false;
165+
if (_isDeactivate_WhenFinish)
166+
{
167+
// gameObject.SetActive(false);
168+
}
159169
}
160170

161171
private void UpdateRenderer(Sprite sprite)

Runtime/StateMachineGeneric.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ public void EnqueueToWaitQueue(params STATE_ID[] nextStates)
144144
{
145145
Debug.LogWarning($"{_owner.name} _waitQueue.Count > 10, wait:{_waitQueue.ToStringCollection()}", _owner);
146146
}
147-
nextStates.Foreach(state => _waitQueue.Add(state));
147+
nextStates.ForEach(state => _waitQueue.Add(state));
148148
}
149149

150150
public StateMachineGeneric<STATE_ID, TSTATE> ForEachState(System.Action<TSTATE> OnEach)
151151
{
152-
_stateInstance.Values.Foreach(OnEach);
152+
_stateInstance.Values.ForEach(OnEach);
153153

154154
return this;
155155
}

0 commit comments

Comments
 (0)