Skip to content

Extensions

John 'Snowdrama edited this page Feb 24, 2026 · 2 revisions

This is gonna be a lot so have fun reading through it, these are all a bunch of tiny helpers that make my life easier, documenting because there's a lot.

Note

This is incomplete

Basic Data Types

Int

BetterMod

A slightly more complex modulo function that deals well with negative values and wraps correctly

Clamp

A slight modification extension that lets you use variables instead of needing to use something like Mathf.Clamp

Example: int thing = 5; thing.Clamp(0, 3);

WrapClamp

Clamp a Value and wrap around to based on the difference

Double

Floor/Ciel/Round/Clamp

A slight modification extension that lets you use variables instead of needing to use something like Mathf.Clamp

Example: double thing = 5.0; thing.Clamp(0.0, 3.0);

InRange

Returns a bool that says if the value is in range

Remap

Takes a Value from one range and remaps it relative to a different range.

For example 0.5 in a 0 to 1 range, would map to 5 in a 0 to 10 range.

WrapClamp

Clamp a Value and wrap around to based on the difference

For example WrapClamp(15.55, 0, 5) would be 0.55 as it wraps 3 times

The wrap clamp maxValue is EXCLUSIVE so WrapClamp(0, 5, 4.99f) = 4.99f WrapClamp(0, 5, 5.00f) = 0.0f

InverseLerp

Does the inverse of a lerp, given some value and a min-max return

For example InverseLerp(Value = 5, min = 0, max = 10); will return 1, since 5 is halfway between 0 and 10

InverseLerpUnclamped

Returns the normalized Value of the Value between min and max unclamped

For example InverseLerpUnclamped(Value = 20, min = 0, max = 10); will return 2 since 20 is double the size of the range

BetterMod

A slightly more complex modulo function that deals well with negative values and wraps correctly

LinearToDecibel

Takes a normalized value from 0-1 and converts it to decibels.

DecibelToLinear

Takes a value in decibels and converts it to normalized 0-1

RoundTo

Rounds to the nearest snap target

For example a value of (1.35d).RoundTo(0.5d) would return 1.5 since it would round up to the nearest 0.5

Float

Floor/Ciel/Round/Clamp

A slight modification extension that lets you use variables instead of needing to use something like Mathf.Clamp

Example: float thing = 5.0f; thing.Clamp(0, 3);

InRange

Returns a bool that says if the value is in range

Remap

Takes a Value from one range and remaps it relative to a different range.

For example 0.5 in a 0 to 1 range, would map to 5 in a 0 to 10 range.

WrapClamp

Clamp a Value and wrap around to based on the difference

For example WrapClamp(15.55, 0, 5) would be 0.55 as it wraps 3 times

The wrap clamp maxValue is EXCLUSIVE so WrapClamp(0, 5, 4.99f) = 4.99f WrapClamp(0, 5, 5.00f) = 0.0f

BetterMod

A slightly more complex modulo function that deals well with negative values and wraps correctly

LinearToDecibel

Takes a normalized value from 0-1 and converts it to decibels.

DecibelToLinear

Takes a value in decibels and converts it to normalized 0-1

RoundTo

Rounds to the nearest snap target

For example a value of (1.35d).RoundTo(0.5d) would return 1.5 since it would round up to the nearest 0.5

CreateSpeedFromTime

Converts a time into a speed to multiply delta time by.

For example: If something should take 1.0 second then the speed is 1.0 If something should take 2.0 seconds then the speed is 0.5 if something should take 0.5 seconds then the speed is 2.0

This is the opposite of CreateTimeFromSpeed

This is because I'm dumb and always forget the math is just 1.0/time

CreateTimeFromSpeed

Converts a speed into a duration per second

Since speed is units "per second" the speed is the inverse of the time

for example if something moves at 1.0 speed, it would travel 1 unit in 1.0 seconds if something moves at 2.0 speed, it would travel 1 unit in 0.5 seconds if something moves at 0.5 speed, it would travel 1 unit in 2.0 seconds

This is the opposite of CreateSpeedFromTime

This is because I'm dumb and always forget the math is just 1.0/speed

Complex types

Vector2

VectorFromAngle

Given a angle in degrees, returns a direction vector pointing in that direction. This assumes that Vector2.Right is 0 radians

VectorFromAngleRads

Given a angle in radians, returns a direction vector pointing in that direction. This assumes that Vector2.Right is 0 radians

AngleFromVector

Given a direction vector get the angle in degrees. This assumes that Vector2.Right is 0 degrees

AngleFromVectorRads

Given a direction vector get the angle in radians. This assumes that Vector2.Right is 0 radians

PerpendicularClockwise

Gets a vector that's rotated 90 degrees clockwise.

PerpendicularCounterClockwise

Gets a vector that's rotated 90 degrees counter clockwise.

AngleTo

Given 2 angles, gets the difference in angle between them

For example if you give it (1, 1) and (1, 0) it would return 45 degrees.

FindScaleFactor

Given a size, and a targetSize, finds the scale factor needed to fit the size into the target size, Including fractional proportions when the size would need to be decreased to fit the size

For example if I have a size of (2, 2) and a target of (16, 9), it would return a scale factor of (4.5, 4.5) as (2 * 4.5, 2 * 4.5) would be (9, 9) and is the smallest that would fit inside (16, 9).

If the stretchToFit option is enabled, this will return a non uniform scale. For example finding the scale factor of (2, 2) to (16, 9) would in this case be (8, 4.5) as multiplying (2 * 8, 2 * 4.5) would give you the target scale of (16, 9).

Floor/Ciel/Round

There are several functions that do floor/ciel/round for both Vector2 and also converting to Vector2Int they do what it says on the tin

Clamp

Clamps the Vector2 from Vector2Int min to Vector2int max

For example (-4, 6) clamped to min (0,0) max (5, 5) would return (0, 5)

Vector2Int

Clamp

Clamps the Vector2 from Vector2Int min to Vector2int max

For example (-4, 6) clamped to min (0,0) max (5, 5) would return (0, 5)

GetCoordinateFromIndex

The inverse of GetIndexFromCoordinate, given a size, and an index, gets a coordinate inside a grid of that size.

Used to turn 1D arrays into 2D arrays.

For example in a 3x3 grid, index 5 belongs to (1, 1) 7,8,9 4,5,6 1,2,3

GetIndexFromCoordinate

The inverse of GetCoordinateFromIndex, given a size, and a position inside that size, gets a unique index.

Used to turn 2D arrays into 1D arrays

For example in a 3x3 grid, position (1, 0) is index 4 7,8,9 4,5,6 1,2,3

Vector3

Floor/Ciel/Round

Clamp

Collections

Array

Shuffle

Shuffles all elements of the array

Swap

Swaps 2 elements by their indexes

RemoveNullEntries

GetRandom

IndexInBounds int

Returns a bool, if true if the int index is in bounds

IndexInBounds Vector2

Returns a bool, if true the vector2Int index is inside the 2D array

IndexInBounds X/Y

Returns a bool, if true the X and Y indexes are both inside the 2D array

Enumerable

Shuffle

GetRandom

TakeLast

Gets the last count elements from the enumeration

List

ShuffleList

Shuffles all elements of the list and returns a new list as a copy

ShuffleSelfMutate

Shuffles all elements of the array and returns the same list but mutated

Swap

Swaps 2 elements by their indexes

RemoveNullEntries

GetRandom

GetRandomCount

Unity Specific

Color

ParseStringAsColor

Converts a hex string like "#FF00FF" into a color. Takes a fallback in case the parse fails.

GetColorFromRainbow

A quick tool for getting a random color from a rainbow gradient of colors.

Passing it a value from 0-1 returns a color

You can also pass it a second parameter rangeMax which will let you pick a color from a 0-rangeMax instead of 0-1

UnitySerializedDictionary

A specific type of serialized dictionary used for displaying key value pairs in editor

This is a bit of overhead internally as it converts them to lists to display and uses a custom editor interface but useful for viewing debug info, or letting you set data in a scriptable object

public class Test : MonoBehaviour
{
    [SerializeField] Dictionary<string, string> thisWontShow = new Dictionary<string, string>();
    [SerializeField] UnitySerializedDictionary<string, string> thisWorks = new UnitySerializedDictionary<string, string>();
}

Clone this wiki locally