Table of contents:
- Array Extensions
- Camera Extensions
- Color Extensions
- Dictionary Extensions
- List Extensions
- Number Extensions
- Random Extensions
- Range Extensions
- String Extensions
- Transform Extensions
- Vector Extensions
using BaseTool;
using UnityEngine;
GameObject[] objects = new GameObject[5];
var obj = objects.GetRandom();
using BaseTool;
using System;
using UnityEngine;
Random rand = new Random();
GameObject[] objects = new GameObject[5];
var obj = objects.GetRandom(rand);
using BaseTool;
Button[] buttons = new Buttons[5];
buttons.ForEach(button =>
{
button.onClick.AddListener(DoSomething);
});
using BaseTool;
Button[] buttons = new Buttons[5];
buttons.ForEach((button, index) =>
{
button.onClick.AddListener(() => DoSomething(index));
});
Methods that extends from Camera
:
GetOrthographicBounds()
Changes the Color.r
, Color.g
, Color.b
, Color.a
value from a Color
and return the new object. Why do these extensions exist? Because you can't modify a struct (like color). You always need to replace the reference.
using BaseTool;
using UnityEngine;
public class Test
{
public Color Color = Color.blue;
}
Test test = new();
// If I want to change the red value, I can't do:
test.Color.r = 1; // CS1612 error
// But I can:
test.Color = test.Color.ChangeRed(1);
// Which is basically the same as:
var tempColor = test.Color;
tempColor.r = 1;
test.Color = tempColor;
Returns a hex code from a color object:
using BaseTool;
Color red = Color.red;
red.ToHex(); // #FF0000
Color.blue.ToHex(); // #0000FF
Returns a hex code including alpha from a color object:
using BaseTool;
using UnityEngine;
Color red = Color.red;
red.ToHexAlpha(); // #FF0000FF
Color.blue.ToHexAlpha(); // #0000FFFF
Color green = new Color(0, 1, 0, 0.5f);
green.ToHexAlpha(); // #00FF007F
Returns a SerializableDictionary
from a Dictionary
. This is mostly used to
converts a Dictionary
into a class that can be displayed, read and serialized
in the inspector.
See SerializableDictionary for more informations.
using BaseTool;
using System.Collections.Generic;
public SerializableDictionary<int, string> SerializedDictionary;
public Dictionary<int, string> Dictionary;
SerializedDictionary = Dictionary.ToSerializableDictionary();
Returns a random element from a List<T>
using the UnityEngine.Random
class.
using BaseTool;
using System.Collections.Generic;
using UnityEngine;
List<MonoBehaviour> behaviours = new();
// filling the list
MonoBehaviour random = behaviours.Random();
Same as GetRandom()
but using the System.Random
object passed by parameter instead of UnityEngine.Random
.
Returns a random element from a List<T>
using the UnityEngine.Random
class and removes it from the list.
using BaseTool;
using System.Collections.Generic;
using UnityEngine;
List<MonoBehaviour> behaviours = new();
// filling 5 elements in the list
Debug.Log(behaviours.Length); // 5
MonoBehaviour random = behaviours.ExtractRandom();
Debug.Log(behaviours.Length); // 4
Same as ExtractRandom()
but using the System.Random
object passed by parameter instead of UnityEngine.Random
.
Determines if an int
or a float
is one of the numbers passed by parameter.
using BaseTool;
int element = 4;
element.IsIn(1, 2, 3, 4, 5); // true
element.IsIn(6, 7, 8); // false
float floatElement = 4.5f; // will be rounded by IsIn()
floatElement.IsIn(1, 2, 3, 4); // false
floatElement.IsIn(5, 6, 7, 8); // true
Determines if an int
or a float
is between a range of two numbers.
using BaseTool;
int element = 4;
element.IsBetween(3, 5); // true
element.IsBetween(1, 3); // false
element.IsBetween(2f, 4f); // true
float floatElement = 4.5f;
floatElement.IsBetween(4, 5); // true
floatElement.IsBetween(1.3f, 4.5f); // true
Same as IsBetween()
but excluding both min and max.
using BaseTool;
int element = 4;
element.IsBetween(3, 5); // true
element.IsBetween(1, 3); // false
element.IsBetween(2f, 4f); // false
float floatElement = 4.5f;
floatElement.IsBetween(4, 5); // true
floatElement.IsBetween(1.3f, 4.5f); // false
Works the same as the UnityEngine.Random.Range()
but for System.Random
.
using BaseTool;
using System;
Random rand = new();
rand.Next(1.2f, 55.5f);
With this extension, you can use range as enumerator instead of for int loop. The range in min inclusive and max exclusive. The following example will iterate through [0;5[
using BaseTool;
foreach (int i in 0..5)
{
// do something with i variable
}
You can get a number from a string by using of these extensions:
.ToSbyte()
.ToShort()
.ToInt()
.ToLong()
.ToFloat()
.ToDouble()
.ToByte()
.ToUShort()
.ToUint()
.ToUlong()
Returns the text after the first occurence of the match.
string text = "This is a big text";
Debug.Log(text.AfterFirst("is")); // is a big text
Returns the text after the last occurence of the match.
string text = "This is a big text";
Debug.Log(text.AfterLast("is")); // a big text
Returns the text before the first occurence of the match.
string text = "This is a big text";
Debug.Log(text.BeforeFirst("is")); // Th
Returns the text before the last occurence of the match.
string text = "This is a big text";
Debug.Log(text.BeforeLast("is")); // This
Returns the index after the prefix that matches the string.
string text = "This is a big text";
Debug.Log(text.PrefixMatch("This is")); // 7
Will create a string
that repeats count
times based on the string used.
string text = "abc";
string repeat = text.Repeat(3);
Debug.Log(repeat); // abcabcabc
Destroy every children of a transform, using delay or not.
using BaseTool;
using UnityEngine;
Transform scrollView; // let's admit it's initialized
scrollView.Clear();
foreach(int i in 0..5)
Instantiate(prefab, scrollView);
Destroy every children of a transform, using the Object.DestroyImmediate()
method.
using BaseTool;
using UnityEngine;
Transform scrollView; // let's admit it's initialized
scrollView.ClearImmediate();
foreach(int i in 0..5)
Instantiate(prefab, scrollView);
Many of vector extensions exist for both Vector2
and Vector3
classes.
Changes the Vector3.x
, Vector3.y
or Vector3.z
value from a Vector3
and return the new object. Why do these extensions exist? Because you can't modify a struct. You always need to replace the reference.
Also exist as Vector2.ChangeX(float x)
and Vector2.ChangeY(float y)
.
using BaseTool;
Transform player; // let's admit it's initialized
// If I want to change the x value, I can't do:
player.position.x = 1f; // CS1612 error
// But I can:
player.position = player.position.ChangeX(1f);
// Which is basically the same as:
var tempPos = player.position;
tempPos.x = 1;
player.position = tempPos;
Works like the Mathf.Clamp()
for float
, but using two vectors as min and max.
using BaseTool;
using UnityEngine;
Vector3 pos = new(1, 2, 3);
pos = pos.Clamp(new(2, -1, -2), new (4, 1, 0));
Debug.Log(pos); // (2.00, 1.00, 0.00)
Works like the Vector3.Lerp()
but using a vector as the ratio.
using BaseTool;
using UnityEngine;
Vector3 ratio = new(0, 0.5f, 1);
Vector3 newPos = ratio.Lerp(
new(-5, -14, -30),
new(4, 20, 29)
);
Debug.Log(newPos); // (-5.00, 3.00, 29.00)
Works like the Vector3.InverseLerp()
but using a vector as the position to ratio.
using BaseTool;
using UnityEngine;
Vector3 pos = new(-5f, 3f, 29f);
Vector3 ratio = pos.InverseLerp(
new(-5, -14, -30),
new(4, 20, 29)
);
Debug.Log(newPos); // (0.00, 0.50, 1.00)
GetClosestPointOnVector(Vector3 begin, Vector3 end)
and GetClosestPointOnVector(Vector2 begin, Vector2 end)
[WIP]
[WIP]
Returns the vector with a maximum length by limiting its length to length
.
You can use it if you want to normalize a vector but only if its magnitude go further a limit value.
using BaseTool;
using UnityEngine;
Vector3 pos1 = new(2f, 0, 2f);
Vector3 pos2 = new(0.5f, 0, 0);
Vector3 pos3 = new(4f, 0, 3f);
Debug.Log(pos1.LimitLength()); // (0.71, 0.00, 0.71)
Debug.Log(pos2.LimitLength()); // (0.50, 0.00, 0.00)
Debug.Log(pos3.LimitLength(4f)); // (3.20, 0.00, 2.40)