Extensions - Hertzole/HertzLib GitHub Wiki
HertzLib has a few extension methods you can use by just adding using Hertzole.HertzLib;
at the top of your class.
Applies to GameObject
Checks if there's a component on the target game object. If there is, it will be returned. But if there's no component, it will instead add it, thus always making sure it exists on the game object.
Example Usage:
Rigidbody myRigidbody = gameObject.GetOrAddComponent<Rigidbody>();
Applies to List and Array
Returns true of the list or array is either null or has 0 items in it.
Example Usage:
GameObject[] allObjects = GameObject.FindObjectsOfType<GameObject>();
if(allObjects.IsNullOrEmpty())
return;
Applies to List and Array
Shuffles an array or list. Simple as that.
Example Usage:
int[] numbers = new int[6] { 0, 1, 2, 3, 4, 5 };
numbers.Shuffle();
Applies to Array
Identical to List.ForEach. Simple executes a function for every item in the array.
Example Usage:
int[] numbers = new int[6] { 0, 1, 2, 3, 4, 5 };
numbers.ForEach(x =>
{
// x is the array item.
Debug.Log(x);
});
Applies to Vector2 and Vector3
Simply converts a Vector2 or Vector3 into their int counterpart.
Example Usage:
Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2Int tilePosition = mousePosition.ToInt();
Applies to string
This one is a quite handy one. If you aren't familiar with how floats are being parsed depending on culture, it may mess you up at some point. Some cultures parse floats with ,
and some with .
for decimal points and some UI components, or similar, might only accept one of those when writing. More often than not, it's .
that is the only accepted decimal point. This allows you to easily try to parse a float/int from a string and you don't have to worry about the culture. It takes care of that for you.
It also works with ints, for good measure. At least to have the shortcut to easily parse ints from strings.
Example Usage:
string decmialNumbersString = "1234.45";
float result = 0;
if (!decmialNumbersString.TryFloatParse(out result))
{
Debug.LogError("Not a valid string.");
}
Applies to string
Parses a string into a float/int. If it isn't a valid string, it will simply return 0.
Example Usage:
string decmialNumbersString = "1234.45";
float result = decmialNumbersString.ParseFloat();
Applies to float, Vector2, Vector3, Vector4, and Quaternion
Returns true if the value has a NaN value in it.
Example Usage:
Vector3 position = transform.position;
if (position.IsNaN())
return;