-
Notifications
You must be signed in to change notification settings - Fork 12
Common
DucNV_2000 edited this page Mar 11, 2024
·
33 revisions
-
IsInteger
checks the input value is an integer or not
static bool IsInteger(float value)
-
GetNumberInAString
Gets the first number that appears in the input string
static 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
static void CallActionAndClean(ref Action action)
Example CallActionAndClean
:
public Action completed;
public void Handle()
{
Common.CallActionAndClean(ref completed);
}
-
IsNullOrEmpty
check null or empty
static bool IsNullOrEmpty<T>(this List<T> source) // Check for null or empty list
static bool IsNullOrEmpty<T>(this T[] source) // Check for null or empty array
static 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
static void Shuffle<T>(this T[] source) // shuffle the elements in the array
static void Shuffle<T>(this List<T> source) // shuffle the elements in the list
static 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
static IDictionary<TKey, TValue> MakeDictionary<TKey, TValue>(this TKey[] keys, TValue[] values) // Make a dictionary from an array of keys and an array of values
static 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
static T PickRandom<T>(this T[] collection) // Randomly select an element of the array
static 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
static (T, int) PickRandomAndIndex<T>(this T[] collection) // Randomly select an element and index of the array
static (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
static 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
static 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
});
}
PlayAnim
static AnimancerComponent PlayAnim(this AnimancerComponent animancerComponent, ClipTransition clip,Action _endAnim = null, float _durationFade = .2f, FadeMode mode = default)
PauseClip
// Freeze a single animation on its current frame:
static AnimancerComponent PauseClip(this AnimancerComponent animancerComponent, ClipTransition clip)
PauseAll
// Freeze all animations on their current frame
static AnimancerComponent PauseAll(this AnimancerComponent animancerComponent)
StopClip
// Stop a single animation from affecting the character and rewind it to the start
static AnimancerComponent StopClip(this AnimancerComponent animancerComponent, ClipTransition clip)
StopAll
// Stop all animations from affecting the character and rewind them to the start
static AnimancerComponent StopAll(this AnimancerComponent animancerComponent)
Example
public AnimancerComponent animancerComponent;
public ClipTransition clip;
public void PlayAnimation()
{
animancerComponent.PlayAnim(clip);
}
public void PauseAnimation()
{
animancerComponent.PauseClip(clip);
}
public void PauseAllAnimation()
{
animancerComponent.PauseAll();
}
public void StopAnimation()
{
animancerComponent.StopClip(clip);
}
public void StopAllAnimation()
{
animancerComponent.StopAll();
}
IgnoreCollision
// Ignore Collision between a Collider and a list of Collider
static void IgnoreCollision(List<Collider> _listCollider, Collider _collider)
// Ignore Collision between a Collider and a list of Collider
static void IgnoreCollision(Collider _collider, List<Collider> _listCollider)
// Ignore Collision between a list of Collider and a list of Collider
static void IgnoreCollision(List<Collider> _listCollider1, List<Collider> _listCollider2)
Example IgnoreCollision
:
public Collider collider;
public List<Collider> listCollider;
public List<Collider> listCollider2;
public void IgnoreCollision()
{
Common.IgnoreCollision(collider, listCollider);
// Or
Common.IgnoreCollision(listCollider, collider);
}
public void IgnoreCollisionListColliderWithListCollider()
{
Common.IgnoreCollision(listCollider, listCollider2);
}
IgnoreCollision2D
// Ignore Collision 2D between a Collider and a list of Collider
static void IgnoreCollision2D(List<Collider2D> _listCollider, Collider2D _collider)
// Ignore Collision 2D between a Collider and a list of Collider
static void IgnoreCollision2D(Collider2D _collider, List<Collider2D> _listCollider)
// Ignore Collision 2D between a list of Collider and a list of Collider
static void IgnoreCollision2D(List<Collider2D> _listCollider1, List<Collider2D> _listCollider2)
Example IgnoreCollision2D
public Collider2D collider;
public List<Collider2D> listCollider;
public List<Collider2D> listCollider2;
public void IgnoreCollision2D()
{
Common.IgnoreCollision2D(collider, listCollider);
// Or
Common.IgnoreCollision2D(listCollider, collider);
}
public void IgnoreCollision2DListColliderWithListCollider()
{
Common.IgnoreCollision2D(listCollider, listCollider2);
}
-
FindComponentInChildWithTag
Find component by tag
static T FindComponentInChildWithTag<T>(this GameObject parent, string tag)
Example
public GameObject player;
private BoxCollider _boxCollider;
public void GetBoxColliderPlayer()
{
_boxCollider = player.FindComponentInChildWithTag<BoxCollider>("Player");
}
-
SetLayer
Set layer for this game object
static GameObject SetLayer(this GameObject obj, int layerIndex)
-
SetLayerForAllChildObject
Set layer for this game object and all child of game object
static GameObject SetLayerForAllChildObject(this GameObject obj, int layerIndex)
Example
public GameObject player;
public void SetLayerForAllChildPlayer()
{
player.SetLayerForAllChildObject(LayerMask.NameToLayer("Layer_Player"));
}
-
SetTag
Set tag for this game object
static GameObject SetTag(this GameObject obj, string tag)
-
SetTagForAllChildObject
Set tag for this game object and all child of game object
static GameObject SetTagForAllChildObject(this GameObject obj, string tag)
Example
public GameObject player;
public void SetTagForAllChildPlayer()
{
player.SetTagForAllChildObject("Tag_Player");
}
-
ClearTransform
clear all child game object of transform
static Transform ClearTransform(this Transform transform)
Example
public GameObject container;
public void ClearContainer()
{
container.transform.ClearTransform();
}
-
Duration
Get duration animation
static float Duration(this SkeletonAnimation skeletonAnimation, string animationName) // For skeleton animation
static float Duration(this SkeletonGraphic skeletonGraphic, string animationName) // For skeleton graphic
Example Duration
:
public SkeletonAnimation skeletonAnimation;
public string animAttack;
public float GetDurationAttack()
{
return skeletonAnimation.Duration(animAttack);
}
-
Play
Clear state, play and initialize skeleton
static SkeletonAnimation Play(this SkeletonAnimation skeletonAnimation, string animationName, bool loop = false) //for skeleton animation
static SkeletonGraphic Play(this SkeletonGraphic skeletonGraphic, string animationName, bool loop = false) //for skeleton graphic
Example Play
:
public SkeletonAnimation skeletonAnimation;
public string animAttack;
public void PlayAttack()
{
skeletonAnimation.Play(animAttack);
}
-
PlayOnly
Play only skeleton not clear state and not initialize skeleton
static SkeletonAnimation PlayOnly(this SkeletonAnimation skeletonAnimation, string animationName, bool loop = false) //for skeleton animation
static SkeletonGraphic PlayOnly(this SkeletonGraphic skeletonGraphic, string animationName, bool loop = false) //for skeleton graphic
Example PlayOnly
:
public SkeletonAnimation skeletonAnimation;
public string animAttack;
public void PlayOnlyAttack()
{
skeletonAnimation.PlayOnly(animAttack);
}
-
AddAnimation
Add animation
static SkeletonAnimation AddAnimation(this SkeletonAnimation skeletonAnimation, int trackIndex, string animationName, bool loop, float timeDelay = 0)
static SkeletonGraphic AddAnimation(this SkeletonGraphic skeletonGraphic, int trackIndex, string animationName, bool loop, float timeDelay = 0)
Example AddAnimation
: both run and attack
public SkeletonAnimation skeletonAnimation;
public string animRun;
public string animAttack;
public void PlayRun()
{
skeletonAnimation.PlayOnly(animRun, true);
}
public void AddAttack()
{
skeletonAnimation.AddAnimation(1, animAttack, false);
}
-
SetSkin
Set new skin for skeleton
static SkeletonAnimation SetSkin(this SkeletonAnimation skeletonAnimation, string skinName) //for skeleton animation
static SkeletonAnimation SetSkin(this SkeletonAnimation skeletonAnimation, List<string> skinNames) //for skeleton animation
static SkeletonGraphic SetSkin(this SkeletonGraphic skeletonGraphic, string skinName) //for skeleton graphic
static SkeletonGraphic SetSkin(this SkeletonGraphic skeletonGraphic, List<string> skinNames) //for skeleton graphic
Example SetSkin
:
public SkeletonAnimation skeletonAnimation;
public string skin1;
public List<string> listSkin;
public void SetSkin1()
{
skeletonAnimation.SetSkin(skin1);
}
public void SetListSkin()
{
skeletonAnimation.SetSkin(listSkin);
}
ChangeAttachment
static SkeletonAnimation ChangeAttachment(this SkeletonAnimation skeletonAnimation, string slotName, string attachmentName) //for skeleton animation
static SkeletonAnimation ChangeAttachment(this SkeletonAnimation skeletonAnimation, string slotName, List<string> attachmentNames) //for skeleton animation
static SkeletonGraphic ChangeAttachment(this SkeletonGraphic skeletonGraphic, string slotName, string attachmentName) //for skeleton graphic
static SkeletonGraphic ChangeAttachment(this SkeletonGraphic skeletonGraphic, string slotName, List<string> attachmentNames) //for skeleton graphic
Example ChangeAttachment
:
public SkeletonAnimation skeletonAnimation;
public string slotName;
public string attachmentName;
public List<string> listAttachmentName;
public void ChangeAttachment()
{
skeletonAnimation.ChangeAttachment(slotName, attachmentName);
}
public void ChangeListAttachment()
{
skeletonAnimation.ChangeAttachment(slotName, listAttachmentName);
}
MixSkin
static SkeletonAnimation MixSkin(this SkeletonAnimation skeletonAnimation, string mixSkinName) //for skeleton animation
static SkeletonAnimation MixSkin(this SkeletonAnimation skeletonAnimation, List<string> mixSkinNames) //for skeleton animation
static SkeletonGraphic MixSkin(this SkeletonGraphic skeletonGraphic, string mixSkinName) //for skeleton graphic
static SkeletonGraphic MixSkin(this SkeletonGraphic skeletonGraphic, List<string> mixSkinNames) //for skeleton graphic
Example MixSkin
:
public SkeletonAnimation skeletonAnimation;
public string mixSkin;
public List<string> listMixSkin;
public void MixSkin()
{
skeletonAnimation.MixSkin(mixSkin);
}
public void MixListSkin()
{
skeletonAnimation.MixSkin(listMixSkin);
}