Different help scripts for Unity engine.
Unity provides simple Random.Range(min, max) method for int and float numeric types. Real projects need more complex random behavior and number of different advanced methods. Methods from RandomUtil.cs are based on Random.Range and make live a little easier.
Return random bool value.
public static bool NextBool()
bool randomBool = RandomUtil.NextBool();
Return random item from item1, item2 or item3 items set.
public static T Next(T item1, T item2)
public static T Next(T item1, T item2, T item3)
string randomPerson = RandomUtil.Next("me", "you");
Pork randomPork =RandomUtil.Next(Pork.NifNif, Pork.NafNaf, Pork.NufNuf);
Return random item from array.
public static T NextItem(T[] array)
int[] intArray = new[] { 1, 3, 5, 7, 9 };
int randomInt = RandomUtil.NextItem(intArray);
Return random item from list.
public static T NextItem(List list)
List<int> list = new List<int> { 1, 3, 5, 7, 9 };
int randomInt = RandomUtil.NextItem(list);
Return random enum item.
public static T NextEnum()
enum Direction { Left, Right, Up, Down }
Direction randomDirection = RandomUtil.NextEnum<Direction>();
Return random index of passed array. Index random selection is based on array weights.
public static int NextWeightedInd(int[] weights)
public static int NextWeightedInd(float[] weights)
int[] weights = new int[] { 10, 10, 30, 50 };
int randomIndex = RandomUtil.NextWeightedInd(weights);
Return sub-list of random items from origin list without repeating.
public static List Take(List list, int count)
List<Card> deckCards = GetDeck();
List<Card> handCards = RandomUtil.Take(deckCards, 6);
Shuffle list of items.
public static void Shuffle(this List list)
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
RandomUtil.Shuffle(list);
Shuffle array of items.
public static void Shuffle(T[] array)
float[] array = new float[] { 1.1f, 1.2f, 1.3f, 1.5f, 1.6f };
RandomUtil.Shuffle(array);
Return random point on line.
Vector2 point1 = new Vector2(123f, 321f);
Vector2 point2 = new Vector2(0f, 0f);
Vector2 randomPoint2 = RandomUtil.NextPointOnLine(point1, point2);
Vector3 point3 = new Vector3(123f, 321f, 213f);
Vector3 point4 = new Vector3(0f, 0f, 0f);
Vector3 randomPoint3 = RandomUtil.NextPointOnLine(point3, point4);
Get a chance with given percentage (from 0 to 100). If percentage is 25 it will return true each 4th time on an average.
int percentage = 25;
bool isSuccess = RandomUtil.GetChance(percentage);
Gets a chance with given probability (from 0f to 1f). If probability is 0.25 it will return true each 4th time on an average.
float probability = 0.25f;
bool isSuccess = RandomUtil.GetChance(probability);
Different extensions sugar-methods for often used operations.
Type-safe versions of Invoke functions from MonoBehaviour.
Instead using
1. Invoke("InvokedFunc", 1f);
2. InvokeRepeating("InvokedFunc", 1f, 1f);
3. bool isInvoking = IsInvoking("InvokedFunc");
4. CancelInvoke("InvokedFunc");
we can use type-safe methods:
1. this.Invoke(() => InvokedFunc(), 1f);
2. this.InvokeRepeating(() => InvokedFunc(), 1f, 1f);
3. bool isInvoking = this.IsInvoking(() => InvokedFunc());
4. this.CancelInvoke(() => InvokedFunc());
RectTransform component contains properties which are not obvious and not easy to use (ex: offsetMin, offsetMax, sizeDelta etc). Unity editor provides more understandable input fields in Inspector for RectTransform like Left, Top, Right, Bottom, Width, Height, PosX, PosY etc.
Sometimes we want to set this fields from code and can't easy understand how to manipulate with RectTransform for that. Following wrappers allow us to do this:
rectTransform.SetLeft(fLeft);
rectTransform.SetRight(fRight);
rectTransform.SetTop(fTop);
rectTransform.SetBottom(fBottom);
rectTransform.SetLeftTopRightBottom(fLeft, fTop, fRight, fBottom);
rectTransform.SetPosX(fPosX);
rectTransform.SetPosY(fPosY);
rectTransform.SetPosXY(fPosX, fPosY);
rectTransform.SetWidth(fWidth);
rectTransform.SetHeight(fHeight);
rectTransform.SetWidthHeight(fWidth, fHeight);
rectTransform.SetPosAndSize(fPosX, fPosY, fWidth, fHeight);
RectTransform provides world coordinates of all four RectTransform corners. Method GetWorldCorners returns array of four Vector3 and we always have to read documentation as we can't determinate mapping of this Vector3 points to corners. Following methods wrap this to readable code:
worldTransform.position = rectTransform.GetWorldCenter();
worldTransform.position = rectTransform.GetWorldTopLeft();
worldTransform.position = rectTransform.GetWorldTopRight();
worldTransform.position = rectTransform.GetWorldBottomLeft();
worldTransform.position = rectTransform.GetWorldBottomRight();
worldTransform.position = new Vector2(rectTransform.GetWorldLeft(), rectTransform.GetWorldTop());
worldTransform.position = new Vector2(rectTransform.GetWorldRight(), rectTransform.GetWorldBottom());
-
Download snippets from releases.
-
Open Visual Studio Menu -> Tools -> Code Snippets Manager.
-
Add folder with snippets.
1.Write 'dlog' in VS editor and press 'tab' button. You will get:
Debug.Log("");
Also, you can use 'dlog1', 'dlog2', 'dlog3', 'dlog4' and 'dlog5' words for Debug.Log with parameters. Check following example:
2.Write 'glh' in VS editor and press 'tab' button. You will get:
GUILayout.BeginHorizontal();
GUILayout.EndHorizontal();
3.Write 'glv' in VS editor and press 'tab' button. You will get:
GUILayout.BeginVertical();
GUILayout.EndVertical();