Extension Methods are methods within a static class which are functionally added to other existing classes, without adjusting those classes internally. They can be used to modify or add functionality to classes you'd normally have no access to, such as the base List or MonoBehaviour classes. Within the Extension Methods category, there are a variety of extensions to the base classes provided by Unity.
All methods are documented with code summaries, so the best way to learn about each individual method is to look into the code. This page provides a summarized description for each script, and what functionality it offers. There are examples available within the demo scene.
Adds the Add(params Action[] others) method to all Actions, so you can easily add arrays of Actions together. For example:
Action combined = a.Add(b, c, d);
Adds angular calculation methods to vectors and floats, which are essential when programming 2D rotations and directions. It is designed for use in 2D space, and the methods are relatively useless in 3D games, for which other methods are recommended.
Vector2.GetAngle() returns the angle in a range of (0, 360). Useful for converting velocity or directional vectors into an angle.
Vector2.RotateVector2(float degrees) rotates a Vector2 with the given angle. Useful for adjusting velocity or directional vectors.
float.CorrectAngle() brings any angle value within the range of (0, 360), without changing the actual direction of the angle.
float.ComparisonAngle(float comparison) brings the angle value it is executed on to within range of (-180, 180) of the comparison angle. It is useful for finding the shortest delta rotation between two angles.
Adds the methods Color.WithR(float r), Color.WithG(float g), Color.WithB(float b), Color.WithA(float a). They simply return the color with a modified color value. Useful for writing shorthand color adjustments.
Instead of writing:
Color c = spriteRenderer.color;
c.a = 0.5f;
spriteRenderer.color = c;
You can write:
spriteRndr.color = spriteRndr.color.WithA(0.5f);
Adds a few useful methods to all List<T> instances. They provide some easily accessible functionality for gameplay randomization, for example, when selecting random enemies, environment parts, or pickups to spawn in the game world.
List<T>.Shuffle() simply shuffles the list randomly.
List<T>.GetRandom() returns a random entry from within the list.
List<T>.RemoveRandom() returns a random entry from within the list, which is also removed from the list.
List<T>.GetRandomCollection(int size, bool duplicates) returns a new List<T> containing a random selection of items from the original list. If duplicates is set to true, it will allow picking the same entry more than once.
List<T>.RemoveRandomCollection(int size) returns a new List<T> containing a random selection of items from the original list. Every entry that is selected is removed from the original list.
Adds a single method to all strings: Truncate(int maxLength), which returns the string with a forced maximum string length.
Adds a few useful methods to floats and ints.
float.LinearRemap(float valueRangeMin, float valueRangeMax, float newRangeMin, float newRangeMax) is an incredibly useful method that brings the float it is used on within a new range. For example, the value 4 in a range of (0, 8) can be brought to a range of (50, 200), which returns 125.
int.WithRandomSign(float negativeProbability = 0.5f) returns the same int, but with a chance it is inverted. This is useful for certain effects or level generation.
float.WithRandomSign(float negativeProbability = 0.5f) does the same as above, but for floats.
float.TimeString(bool twoDigitMinutes) converts a float, which represents time in seconds, to a 1:51 time string. Useful for displaying running time or time left for gameplay mechanics.
Adds several methods which add upon the functionality of Unity and C#, providing a variety of useful utilities that assist programming of any kind.
GameObject.IsInLayerMask(LayerMask mask) returns whether the game object is present within the given Unity LayerMask. LayerMasks can be set in the inspector to include or exclude any layer. This method is very useful for checking hits, input clicks and collisions beyond filtering layers in the physics engine.
GameObject.GetComponentInParentInactive<T>() returns the first component of T it finds in itself, and it's parents. This method works even if the object or its parents are inactive, which the original method does not.
GameObject.GetComponentCached<T>(ref T local) gets and returns the required component, and immediately saves it in the referenced local variable. Use this to cache the search results for easier getters that only actually search for the component once.
object.IsOfType(System.Type type) returns whether the object is of the given type, or inherits from it and is a subclass. Useful to check whether certain objects are of a specific class before casting them.
anything.DeepClone() makes an exact duplicate of the serializable object using a memory stream. Not a performance friendly method, but useful in copying serializable data objects.
Adds a variety of methods to all Vector2 and Vector3 instances, to more easily change their coordinates, or complete more complex calculations with them.
The methods Vector3.XY() and Vector3.XZ() convert to a Vector2 using the respective coordinates.
The methods Vector3.WithX(float x), Vector3.WithY(float y) and Vector3.WithZ(float z) return a new vector where one coordinate is changed. Vector2 has likewise methods, including one where it is turned into a Vector3 by adding an z axis.
The method Vector3.Multiply(params Vector3[] a) multiplies the x, y and z of all vectors together. Vector2 has a likewise method.
Vector3.NearestPointOnAxis(Vector3 point) is to be executed on a directional vector; a line. It returns the point on the line, closest to the given point.
More will be added in time, and any suggestions are welcome.