Handy extensions for general Unity development.
This is a collection of extensions that I've accumulated over many years of Unity development. The goal is to reduce boilerplate code and simplify development without adding too much clutter, while being consistent with Unity's API style.
Nearly all extensions have XML documentation, except for where the method name alone is clear enough.
The extensions were written with performance in mind, and the few unavoidably slow methods that use reflection have explicit warnings in their XML documentation.
- MathExtensions
- Focuses mainly on vectors, adding shorthand functions such as:
vector.WithY(0f)fornew Vector3(vector.x, 0f, vector.z).vector.OnlyY()fornew Vector3(0f, vector.y, 0f).vector.XZ()fornew Vector2(vector.x, vector.z).
- Focuses mainly on vectors, adding shorthand functions such as:
- EnumerableExtensions
list.Random()returns a random object from the list (list[UnityEngine.Random.Range(0, list.Count)]).list.Average()like LINQ's Average() function (get the average of all values in a list) but for Vector3 and Quaternion.list.RandomizedOrder()to enumerate a list in a randomized order (shorthand forlist.OrderBy(item => UnityEngine.Random.value)).- Simple enumeration shorthands:
list.AddAll(otherList),list.RemoveAll(otherList),list.ContainsAll(otherList).
- BitwiseExtensions
enum.CountFlags()used on layer masks or other bit field (FlagsAttribute) enums to check how many flags they contain (i.e., how many bits equal 1).
- LayerExtensions
layerMask.HasLayer("Water")forlayerMask == (layerMask | (1 << LayerMask.NameToLayer("Water"))).gameObject.SetLayer("Water", includeChildren: true)for recursively setting gameObject and its children's layers.
- GameObjectExtensions
- GetComponent overload with an index parameter, e.g.
gameObject.GetComponent<Collider>(2)to get the third Collider component.
- GetComponent overload with an index parameter, e.g.
- TransformExtensions
transform.GetPath()gets the hierarchical path of the transform, e.g. "Player/Armature/Spine/LeftArm/LeftHand".transform.SetPath("Player/Armature/Spine/LeftArm/LeftHand")sets the hierarchical path to where the transform should be parented.
- ConstraintExtensions
constraint.AddSource(transform)forconstraint.AddSource(new ConstraintSource {sourceTransform = transform, weight = 1f)).constraint.AddSourceWithCurrentOffsets(transform)for adding source while doing all the transform translation math to keep the source transform in its current position and rotation.
- ColorExtensions
color.WithAlpha(0.5f)fornew Color(color.r, color.g, color.b, 0.5f).color.WithAlphaRelative(0.5f)fornew Color(color.r, color.g, color.b, color.a*0.5f).
- StringExtensions
- Rich text color with
string.WithColor("#ff0000")andstring.WithColor(Color.red). string.SplitPascalCase()turns pascal case strings like "AllYourBase" into "All Your Base".string.UppercaseFirstLetter()
- Rich text color with
- StringBuilderExtensions
- Rich text color with
stringBuilder.Append("Foo", "#ff0000")andstringBuilder.Append("Foo", Color.red). stringBuilder.TrimEnd()with respective overloads identical tostring.TrimEnd().
- Rich text color with
- UIToolkitExtensions
element.SetActive(false)toggles the element on and off much likeGameObject.SetActive()does, by setting the 'display' style to Flex or None, and also sends a ElementActiveEvent for you to register callbacks for.element.GetRoot()recursively traverseselement.parentand returns the root parent.
- In Unity's Package Manager, press the "+" button.
- Select "Add package from git URL...".
- Put in: https://github.com/Jobus0/Unity-Extensions.git