Extension Methods
| Keywords | static class, this (first parameter) |
|---|---|
| Category | Methods |
| In Unity | DOTween's transform.DOMove(), LINQ's list.Where() |
Extension Methods
Extension methods let you add your own methods to types you didn't write, such as Transform, Vector3 or GameObject, and call them as if they were built in. DOTween's transform.DOMove(...) is an extension method.
Stick-on accessories for a phone. You can't open the phone and rebuild it (you don't own Unity's source code), but you can clip on a camera lens that looks and works like part of the phone. Underneath, it's still a separate piece.
How to write one
- Create a
static class(any name, often ending in "Extensions"). - Write a
staticmethod. - Put
thisbefore the first parameter. Its type is the type you're extending.
using UnityEngine;
public static class VectorExtensions
{
// Returns a copy with a different y
public static Vector3 WithY(this Vector3 v, float y)
{
return new Vector3(v.x, y, v.z);
}
// Ignore height: useful for top-down distances and directions
public static Vector3 Flat(this Vector3 v) => new Vector3(v.x, 0f, v.z);
}
// Usage: looks like a Vector3 method
transform.position = transform.position.WithY(0f);
Vector3 dir = (target.position - transform.position).Flat().normalized;
Compare with the long version: Vector3 p = transform.position; p.y = 0f; transform.position = p;
More useful examples
using UnityEngine;
public static class UnityExtensions
{
// Destroy every child
public static void DestroyChildren(this Transform t)
{
for (int i = t.childCount - 1; i >= 0; i--)
Object.Destroy(t.GetChild(i).gameObject);
}
// Is this layer inside a LayerMask?
public static bool Contains(this LayerMask mask, int layer)
{
return (mask.value & (1 << layer)) != 0;
}
// Get a component, or add it if it's missing
public static T GetOrAdd<T>(this GameObject go) where T : Component
{
return go.TryGetComponent(out T c) ? c : go.AddComponent<T>();
}
// Pick a random element
public static T RandomItem<T>(this T[] array)
{
return array[Random.Range(0, array.Length)];
}
}
// Usage
inventoryPanel.DestroyChildren();
if (groundMask.Contains(other.gameObject.layer)) { /* landed */ }
var source = gameObject.GetOrAdd<AudioSource>();
AudioClip clip = footsteps.RandomItem();
What happens at runtime
- Extension methods are only a nicer way to write the call. The compiler turns
pos.WithY(0f)intoVectorExtensions.WithY(pos, 0f). - They cost exactly the same as calling a normal static method.
- They can only use the public members of the type. They can't reach private fields or add new fields.
Common Mistakes
- Method not found. The class must be
static, not nested inside another class, and its namespace must be imported withusing. - Expecting it to change a struct.
transform.position.WithY(0)returns a new Vector3; assign it back. See Value & Reference Types. - Calling on a null object. Extension methods can be called on null (no instant error, since it's a static call), so check inside if it matters.
- Name clashes. If the type already has a method with the same name and parameters, the built-in one wins and yours is ignored.
See also
This page is a simplified, AI-assisted interpretation. For the complete and authoritative reference, see the official Microsoft C# documentation.