- (6/5/23) I'll be updating this repo soon to include a list of each extension along with its summary. This will make it easier to see what this repo can do.
Helpful auxiliary extensions for common types in C# Unity development. The code and documentation is currently a work in progress, but functions are named obviously enough. Please leave feedback and feel free to make critiques as I improve this repo!
- Extensions for a multitude of types
- Extensions for common usages of Unity's
Mathf
class in relation toint
andfloat
- Extensions to replace static functions for
Vector3
andQuaternion
- Seriously, there's so many extensions...
- Additional utility classes for common operations
- Almost entirely documented
string
char
float
int
Vector2
&Vector3
Quaternion
IList
IEnumerable
IEnumerator
IDictionary
- This API favors usage of
IList<T>
over arrays andIEnumerables
- Some
Linq
is used forIEnumerable
extensions. You can avoid it by usingIList<T>
- Documentation
- Complete list of intended extensions
- Function attributes for IDE and dev insight
- Support for arrays, not just
IList
andIEnumerable
(coming in version 1.1.x)
TODO (there's a lot)
- Install the package with Package Manager OR download the repository into your
Assets
folder (if downloading from Git) - Import the Auxtensions namespace into a C# file.
using Auxtensions;
- Access extensions or util classes as needed (see below examples)
// Find the closest power of 2 to an integer.
(100).ClosestPowerOfTwo();
// Round a float value to an integer.
(1.71248f).RoundToInt();
// Get a new Vector3 full of the absolute values of the current one
new Vector3(-1, -23, 400).Abs();
- ???
- Profit
Note that this isn't an exhaustive list, the API has dozens of extension methods not listed here.
// Remap or normalize values
var myCrazyFloat = 173.81245f;
var remapped = myCrazyFloat.Remap(0.0f, 200.0f, 0.0f, 1.0f);
// Check if this float is outside of a range
var anotherCrazyFloat = 1.618f;
if (anotherCrazyFloat.IsOutsideRange(0.0f, 1.0f))
{
Debug.Log("Wow, it's out of the range.");
}
// Clamp this float to a maximum value, ensuring it never goes over
var wowAnotherCrazyFloat = 12757.0812f;
var clamped = wowAnotherCrazyFloat.ClampToMax(100.0f);
// Quickly round a float
var howManyCrazyFloatsAreThere = 8125.812882882f;
var rounded = howManyCrazyFloatsAreThere.RoundToInt();
// Move this value towards another value
var velocity = 15.08f;
velocity = velocity.MoveTowards(20.0f, Time.deltaTime * 10.0f);
Extensions for int
typed variables exist, but the documentation is coming soon. It's basically the same (and a little more) as the float
extensions.
// Get a random item from a list
var list = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var randomInt = list.Random();
// Shuffle
var list = new List<int> { 5, 10, 15, 20 };
list.Shuffle();
// Dequeue the item in front, like a Queue<T>
var list = new List<int> { 9, 8, 7, 6, 5 };
var next = list.Dequeue();
// Remove items in the list that meet the criteria
var list = new List<string> { "Hello", "Test", "Yeup" };
list.RemoveWhere(item => item.Length > 4);
The following example would require configuration through the editor. It shows how to setup a weighted prefab random selector though.
using UnityEngine;
using Auxtensions;
using System.Collections.Generic;
public class MyBehaviour : MonoBehaviour
{
[SerializeField] private List<WeightedPrefab> _weightedPrefabs;
public GameObject GetRandomWeightedPrefab()
{
return _weightedPrefabs.RandomByWeight(item => item.weight);
}
}
[CreateAssetMenu(menuName = "Weighted Prefab")]
public class WeightedPrefab : ScriptableObject
{
public GameObject prefab;
public float weight;
}
// Easily convert from/to JSON with serializable objects
var json = "{ \"fieldName\": \"value\" }";
var myObj = json.FromJson<MySerializableClass>();
// Quickly check "is null or whitespace"
var emptyString = " ";
if (emptyString.IsNullOrWhiteSpace())
{
Debug.Log("String is null or whitespace!");
}
// Try to parse a string to an int
var numberString = "100";
if (numberString.TryParseToInt(out var result))
{
Debug.Log("Yep, it's a number");
}
// Get a prefab from a path
var myPrefabResourcePath = "Resources/MyPrefab";
var resource = myPrefabResourcePath.LoadResource();
// or asynchronously...
var resourceRequest = myPrefabResourcePath.LoadResourceAsync();
// Check is digit, special character, period, etc.
var c = 'A';
if (c.IsDigit() || c.IsPeriod() || c.IsWhiteSpace || c.IsSpecialCharacter())
{
Debug.Log("Everything is A-O-K.");
}
// Check if letter
var c = '1';
if (c.IsLetter())
{
Debug.Log("It's a letter.");
}
else
{
Debug.Log("It's not a letter.");
}
More Vector2
extensions will be added in time. A lot of the Vector3
extensions can be applied to Vector2
as well, it's just not prioritized work right now.
// Get a random float based in the range from X to Y
var vector2 = new Vector2(-1.0f, 1.0f);
var value = vector2.RandomFloatFromRange();
// Clamp the magnitude of a Vector3
var currentVelocity = new Vector3(-1.0f, 12.0f, 0.3f);
var clampedVelocity = currentVelocity.ClampMagnitude(5.0f);
// Convert a Vector3 with values in its X and Y fields to a Vector3 with values in its X and Z fields
var vector = new Vector3(1.0f, 12.0f);
var topDownVector = vector.ToVector3xzFromVector3xy();
// Check if all values on a Vector3 are within a float range
var velocity = new Vector3(12.0f, 0.0f, 1.0f);
if (velocity.IsMagnitudeInsideRange(0.0f, 1.0f))
{
Debug.Log("Player is walking.");
}
else
{
Debug.Log("Player isn't walking.");
}
Checkout Coroutine Utils because it has the same vibe.
private void Start()
{
StartCoroutine(MyCoroutine().Then(AnotherCoroutine());
}
private IEnumerator MyCoroutine()
{
Debug.Log("Hello World");
}
private IEnumerator AnotherCoroutine()
{
Debug.Log("Whoop");
}