-
Notifications
You must be signed in to change notification settings - Fork 2
Common
DucNV_2000 edited this page Jul 10, 2024
·
2 revisions
-
IsInteger
checks the input value is an integer or not
bool IsInteger(float value)
-
GetNumberInAString
Gets the first number that appears in the input string
int GetNumberInAString(string str)
// exp: str = ab234dc => result: 234
// exp: str = ab23df45 => result: 23
Example GetNumberInAString
:
private string text = "abe123";
private int number;
public void GetNumber()
{
number = Common.GetNumberInAString(text);
// Or
number = text.GetNumberInAString();
// result: number = 123;
}
-
CallActionAndClean
execute the call action and remove the original reference
void CallActionAndClean(ref Action action)
Example CallActionAndClean
:
public Action completed;
public void Handle()
{
Common.CallActionAndClean(ref completed);
}
-
IsNullOrEmpty
check null or empty
bool IsNullOrEmpty<T>(this List<T> source) // Check for null or empty list
bool IsNullOrEmpty<T>(this T[] source) // Check for null or empty array
bool IsNullOrEmpty<TKey, TValue>(this Dictionary<TKey, TValue> source) // Check for null or empty dictionary
Example IsNullOrEmpty
:
public List<int> listInt;
public int[] arrayInt;
public Dictionary<int, string> dict;
public void CheckNullOrEmpty()
{
// isListInt = true => listInt Null or Empty
bool isListNull = listInt.IsNullOrEmpty();
// isArrayNull = true => arrayInt Null or Empty
bool isArrayNull = arrayInt.IsNullOrEmpty();
// isDictNull = true => dict Null or Empty
bool isDictNull = dict.IsNullOrEmpty();
}
Shuffle
void Shuffle<T>(this T[] source) // shuffle the elements in the array
void Shuffle<T>(this List<T> source) // shuffle the elements in the list
IDictionary<T1, T2> Shuffle<T1, T2>(this IDictionary<T1, T2> source) // shuffle the elements in the dictionary
Example Shuffle
:
public List<int> listInt;
public int[] arrayInt;
public Dictionary<int, string> dict;
public void HandleShuffle()
{
// shuffle list
listInt.Shuffle();
//shuffle array
arrayInt.Shuffle();
// shuffle dict
dict.Shuffle();
}
MakeDictionary
IDictionary<TKey, TValue> MakeDictionary<TKey, TValue>(this TKey[] keys, TValue[] values) // Make a dictionary from an array of keys and an array of values
IDictionary<TKey, TValue> MakeDictionary<TKey, TValue>(this IList<TKey> keys, IList<TValue> values) // Make a dictionary from a list of keys and an array of values
Example MakeDictionary
:
public int[] arrayKey;
public string[] arrayValue;
public List<int> listKey;
public List<string> listValue;
public void HandleMakeDict()
{
IDictionary<int, string> dictMakeByArray = arrayKey.MakeDictionary(arrayValue);
IDictionary<int, string> dictMakeByList = listKey.MakeDictionary(listValue);
}
PickRandom
T PickRandom<T>(this T[] collection) // Randomly select an element of the array
T PickRandom<T>(this List<T> collection) // Randomly select an element of the list
Example PickRandom
:
public string[] arrayTest;
public List<string> listTest;
public void HandleRandom()
{
string valueRandomArray = arrayTest.PickRandom();
string valueRandomList = listTest.PickRandom();
}
PickRandomAndIndex
(T, int) PickRandomAndIndex<T>(this T[] collection) // Randomly select an element and index of the array
(T, int) PickRandomWithIndex<T>(this List<T> collection) // Randomly select an element and index of the list
Example PickRandomAndIndex
:
public string[] arrayTest;
public List<string> listTest;
public void HandleRandom()
{
(string valueRandomArray, int indexArray) = arrayTest.PickRandomAndIndex();
(string valueRandomList, int indexList) = listTest.PickRandomWithIndex();
}
InternetConnection
IEnumerator InternetConnection(Action<bool> action)
// if request internet success => action(true)
// if request internet fail => action(false)
Example InternetConnection
:
public void CheckConnectInternet()
{
StartCoroutine(Common.InternetConnection(isConnect =>
{
if (isConnect)
{
// handle has connected internet
}
else
{
// handle lost internet
}
}));
}
// use Coroutine of MonoGlobal to check
void CheckInternetConnection(Action actionConnected, Action actionDisconnected)
// actionConnected => Action when connected to the internet
// actionDisconnected => Action when losing internet connection
Example CheckInternetConnection
:
public void CheckConnectInternet()
{
Common.CheckInternetConnection(() =>
{
// handle connected internet
}, () =>
{
// handle lose internet
});
}