-
-
Notifications
You must be signed in to change notification settings - Fork 1
Extensions
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
A slightly more complex modulo function that deals well with negative values and wraps correctly
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);
Clamp a Value and wrap around to based on the difference
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);
Returns a bool that says if the value is in range
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.
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
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
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
A slightly more complex modulo function that deals well with negative values and wraps correctly
Takes a normalized value from 0-1 and converts it to decibels.
Takes a value in decibels and converts it to normalized 0-1
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
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);
Returns a bool that says if the value is in range
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.
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
A slightly more complex modulo function that deals well with negative values and wraps correctly
Takes a normalized value from 0-1 and converts it to decibels.
Takes a value in decibels and converts it to normalized 0-1
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
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
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
Given a angle in degrees, returns a direction vector pointing in that direction. This assumes that Vector2.Right is 0 radians
Given a angle in radians, returns a direction vector pointing in that direction. This assumes that Vector2.Right is 0 radians
Given a direction vector get the angle in degrees. This assumes that Vector2.Right is 0 degrees
Given a direction vector get the angle in radians. This assumes that Vector2.Right is 0 radians
Gets a vector that's rotated 90 degrees clockwise.
Gets a vector that's rotated 90 degrees counter clockwise.
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.
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).
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
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)
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)
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
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
Shuffles all elements of the array
Swaps 2 elements by their indexes
Returns a bool, if true if the int index is in bounds
Returns a bool, if true the vector2Int index is inside the 2D array
Returns a bool, if true the X and Y indexes are both inside the 2D array
Gets the last count elements from the enumeration
Shuffles all elements of the list and returns a new list as a copy
Shuffles all elements of the array and returns the same list but mutated
Swaps 2 elements by their indexes
Converts a hex string like "#FF00FF" into a color. Takes a fallback in case the parse fails.
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
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>();
}