Skip to content

Commit 9c829fc

Browse files
committed
refactor: organize module
1 parent 769c732 commit 9c829fc

File tree

2 files changed

+96
-98
lines changed

2 files changed

+96
-98
lines changed

Assets/JCSUnity/Scripts/Util/JCS_Util.cs

Lines changed: 83 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static int EnumSize<T>()
111111

112112
#endregion
113113

114-
#region Array
114+
#region List
115115

116116
/// <summary>
117117
/// Check the value within the range plus acceptable range.
@@ -341,6 +341,86 @@ public static T ListPopBack<T>(List<T> list)
341341
return data;
342342
}
343343

344+
/// <summary>
345+
/// Fill slots with initialize value type by length.
346+
/// </summary>
347+
/// <typeparam name="T"> Type from `inArray`. </typeparam>
348+
/// <param name="inArray"> Array you would like to fill out. </param>
349+
/// <param name="len"> Target length to initialize. </param>
350+
/// <param name="with"> Initialize object type. </param>
351+
/// <returns> Return filled array. </returns>
352+
public static T[] FillSlot<T>(T[] inArray, int len, T with)
353+
{
354+
return FillSlot(inArray.ToList(), len, with).ToArray();
355+
}
356+
357+
public static List<T> FillSlot<T>(List<T> inList, int len, T with)
358+
{
359+
for (int index = inList.Count; index < len; ++index)
360+
inList.Add(with);
361+
return inList;
362+
}
363+
364+
/// <summary>
365+
/// Remove the empty slot in the array.
366+
/// </summary>
367+
/// <typeparam name="T"> Type of the List. </typeparam>
368+
/// <param name="inArray"> Array list. </param>
369+
/// <returns> Cleaned up Array object. </returns>
370+
public static T[] RemoveEmptySlot<T>(T[] inArray)
371+
{
372+
return RemoveEmptySlot(inArray.ToList()).ToArray();
373+
}
374+
public static List<T> RemoveEmptySlot<T>(List<T> inList)
375+
{
376+
List<T> newArray = new List<T>(inList.Count);
377+
378+
for (int index = 0; index < inList.Count; ++index)
379+
{
380+
// Add itself if exists.
381+
if (inList[index] != null)
382+
newArray.Add(inList[index]);
383+
}
384+
385+
return newArray;
386+
}
387+
388+
/// <summary>
389+
/// Remove the empty slot in the list including remove
390+
/// the missing gameobject too.
391+
///
392+
/// I guess Unity do the CG collection later a while when
393+
/// you call 'Destory()' function. Before scripting layer
394+
/// acknowledge this game object is destory might be too
395+
/// late in some situation. This will avoid this type of
396+
/// issue/circumstance.
397+
/// </summary>
398+
/// <typeparam name="T"> Type of the List. </typeparam>
399+
/// <param name="inList"> List object. </param>
400+
/// <returns> Cleaned up List object. </returns>
401+
public static T[] RemoveEmptySlotIncludeMissing<T>(T[] inArray)
402+
where T : UnityEngine.Object
403+
{
404+
return RemoveEmptySlotIncludeMissing(inArray.ToList()).ToArray();
405+
}
406+
public static List<T> RemoveEmptySlotIncludeMissing<T>(List<T> inList)
407+
where T : UnityEngine.Object
408+
{
409+
List<T> newArray = new List<T>(inList.Count);
410+
411+
for (int index = 0; index < inList.Count; ++index)
412+
{
413+
// Add itself if exists.
414+
//
415+
// SOURCE(jenchieh): https://answers.unity.com/questions/131158/how-can-i-check-if-an-object-is-null.html
416+
// INFORMATION(jenchieh): https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/
417+
if (inList[index] ?? false)
418+
newArray.Add(inList[index]);
419+
}
420+
421+
return newArray;
422+
}
423+
344424
#endregion
345425

346426
#region String
@@ -1086,90 +1166,6 @@ public static TweenDelegate GetEasing(JCS_TweenType type)
10861166

10871167
#endregion
10881168

1089-
#region List
1090-
1091-
/// <summary>
1092-
/// Fill slots with initialize value type by length.
1093-
/// </summary>
1094-
/// <typeparam name="T"> Type from `inArray`. </typeparam>
1095-
/// <param name="inArray"> Array you would like to fill out. </param>
1096-
/// <param name="len"> Target length to initialize. </param>
1097-
/// <param name="with"> Initialize object type. </param>
1098-
/// <returns> Return filled array. </returns>
1099-
public static T[] FillSlot<T>(T[] inArray, int len, T with)
1100-
{
1101-
return FillSlot(inArray.ToList(), len, with).ToArray();
1102-
}
1103-
1104-
public static List<T> FillSlot<T>(List<T> inList, int len, T with)
1105-
{
1106-
for (int index = inList.Count; index < len; ++index)
1107-
inList.Add(with);
1108-
return inList;
1109-
}
1110-
1111-
/// <summary>
1112-
/// Remove the empty slot in the array.
1113-
/// </summary>
1114-
/// <typeparam name="T"> Type of the List. </typeparam>
1115-
/// <param name="inArray"> Array list. </param>
1116-
/// <returns> Cleaned up Array object. </returns>
1117-
public static T[] RemoveEmptySlot<T>(T[] inArray)
1118-
{
1119-
return RemoveEmptySlot(inArray.ToList()).ToArray();
1120-
}
1121-
public static List<T> RemoveEmptySlot<T>(List<T> inList)
1122-
{
1123-
List<T> newArray = new List<T>(inList.Count);
1124-
1125-
for (int index = 0; index < inList.Count; ++index)
1126-
{
1127-
// Add itself if exists.
1128-
if (inList[index] != null)
1129-
newArray.Add(inList[index]);
1130-
}
1131-
1132-
return newArray;
1133-
}
1134-
1135-
/// <summary>
1136-
/// Remove the empty slot in the list including remove
1137-
/// the missing gameobject too.
1138-
///
1139-
/// I guess Unity do the CG collection later a while when
1140-
/// you call 'Destory()' function. Before scripting layer
1141-
/// acknowledge this game object is destory might be too
1142-
/// late in some situation. This will avoid this type of
1143-
/// issue/circumstance.
1144-
/// </summary>
1145-
/// <typeparam name="T"> Type of the List. </typeparam>
1146-
/// <param name="inList"> List object. </param>
1147-
/// <returns> Cleaned up List object. </returns>
1148-
public static T[] RemoveEmptySlotIncludeMissing<T>(T[] inArray)
1149-
where T : UnityEngine.Object
1150-
{
1151-
return RemoveEmptySlotIncludeMissing(inArray.ToList()).ToArray();
1152-
}
1153-
public static List<T> RemoveEmptySlotIncludeMissing<T>(List<T> inList)
1154-
where T : UnityEngine.Object
1155-
{
1156-
List<T> newArray = new List<T>(inList.Count);
1157-
1158-
for (int index = 0; index < inList.Count; ++index)
1159-
{
1160-
// Add itself if exists.
1161-
//
1162-
// SOURCE(jenchieh): https://answers.unity.com/questions/131158/how-can-i-check-if-an-object-is-null.html
1163-
// INFORMATION(jenchieh): https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/
1164-
if (inList[index] ?? false)
1165-
newArray.Add(inList[index]);
1166-
}
1167-
1168-
return newArray;
1169-
}
1170-
1171-
#endregion
1172-
11731169
#region Scene
11741170

11751171
/// <summary>
@@ -1212,7 +1208,7 @@ public static bool IsSceneExists(string name)
12121208
#region Animation
12131209

12141210
/// <summary>
1215-
/// Sets the weight of the layer at the given index.
1211+
/// Sets the weight of the layer at the given name.
12161212
///
12171213
/// Similar to `Animationr.SetLayerWeight` but accept string name.
12181214
/// </summary>
@@ -1229,7 +1225,7 @@ public static void SetLayerWeight(Animator ator, int index, float val)
12291225
}
12301226

12311227
/// <summary>
1232-
/// Sets the weight of the layer at the given index.
1228+
/// Returns the weight of the layer at the specified name.
12331229
///
12341230
/// Similar to `Animationr.SetLayerWeight` but accept string name.
12351231
/// </summary>

docs/ScriptReference/Util/JCS_Util.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,35 @@ All code utility is stored here.
2222
| IsArrayEmpty | Check if the string array is empty. |
2323
| ListPopFront | Pop the first value from the list. |
2424
| ListPopBack | Pop the last value from the list. |
25+
| FillSlot | Fill slots with initialize value type by length. |
26+
| RemoveEmptySlot | Remove the null value from a list/array. |
27+
| RemoveEmptySlotIncludeMissing | Remove all the null value including missing reference in the list/array. |
2528
| BytesToString | Convert byte array to string by charset type. |
2629
| StringToBytes | Convert string to byte array by charset type. |
2730
| EscapeURL | Simple version of escape url. |
2831
| ToJson | Serialize object to JSON string. |
2932
| EnableComponent | Do enable/distance component. |
3033
| ForceGetComponent | Force to get a component, if not found add one new then. |
31-
| Instantiate | Spawn a game object. |
32-
| FindObjectByType | Retrieves the first active loaded object of Type type. |
33-
| FindObjectsByType | Retrieves a list of all loaded objects of Type type. |
34-
| SpawnAnimateObject | Spawn a gameobject with animation attached. |
35-
| SpawnAnimateObjectDeathEvent | Spawn a gameobject with the animator and death event on it. |
3634
| SetActiveToAllChildren | Active all the child in a transform. |
3735
| MoveToTheLastChild | Make the transform to the last transform of the current parent transform. |
3836
| SetParentWithoutLosingInfo | Set the transform to another transform without losing it's info. (position, rotation, scale) |
3937
| SetEnableAllComponents | Set enable/disable to all component on this transform. |
38+
| DetachChildren | Detach all the child from a transform. |
39+
| ForceDetachChildren | Force detach all the child from a transform. |
40+
| AttachChildren | Attach all the childs to this transform. |
41+
| Instantiate | Spawn a game object. |
42+
| SpawnAnimateObject | Spawn a gameobject with animation attached. |
43+
| SpawnAnimateObjectDeathEvent | Spawn a gameobject with the animator and death event on it. |
4044
| DestroyAllTypeObjectInScene | Destory all gameobjects in the scene with the type passed in. |
4145
| DestroyImmediateAllTypeObjectInScene | Destroy all the gameobject in the scene immediately with the type passed in. |
46+
| FindObjectByType | Retrieves the first active loaded object of Type type. |
47+
| FindObjectsByType | Retrieves a list of all loaded objects of Type type. |
4248
| FindCloneObjectsOfTypeAll | Find all cloned gameobjects in the scene with the type passed in. |
4349
| FindNotCloneObjectsOfTypeAll | Find all gameobjects that are not clones in the scene with the type passed in. |
4450
| FindObjectsOfTypeAllInHierarchy | Find all the gameobject that are only active in the hierarchy with the type passed in. |
4551
| GetEasing | Returns the easing function pointer base on the tweener type/enum. |
46-
| FillSlot | Fill slots with initialize value type by length. |
47-
| RemoveEmptySlot | Remove the null value from a list/array. |
48-
| RemoveEmptySlotIncludeMissing | Remove all the null value including missing reference in the list/array. |
49-
| DetachChildren | Detach all the child from a transform. |
50-
| ForceDetachChildren | Force detach all the child from a transform. |
51-
| AttachChildren | Attach all the childs to this transform. |
5252
| IsScene | Check current scene's with NAME. |
5353
| IsSceneExists | Returns true if the scene 'name' exists and is in your Build settings, false otherwise. |
54+
| SetLayerWeight | Sets the weight of the layer at the given name. |
55+
| GetLayerWeight | Returns the weight of the layer at the specified name. |
5456
| IsSameTribe | Check if the live object is the same tribe. |

0 commit comments

Comments
 (0)