So now when I need to get the direction from the player to an enemy in my player script, i can simply type:

Note that although the method is static it will be applied on a per-instance basis, thanks to the this keyword in front of the first Vector3 parameter in the function definition. This results in the ability to call the method from any instance of a Vector3 , for instance the transform.position of a script on the player object!

The extension method in practice Vector3 playerToEnemyDir = transform.position.DirectionTo(enemy.transform.position); 1 Vector3 playerToEnemyDir = transform . position . DirectionTo ( enemy . transform . position ) ;

MORE EXAMPLES

Thanks to this handy little method, I will never have to worry about which vector to subtract from which again! But I will still only have a 50% chance of answering correctly if anyone asks me

I’ve done a lot of work on editor extensions for the Unity editor, and one thing I often want to do is highlight something by drawing a semi-transparent white rect on top of it. However, there’s no easy way to quickly write “I want the color white, but with an alpha value of 0.5!” in the code! Again, extension methods to the rescue:

An extension method for colors // The slow and annoying way EditorGUI.DrawRect (myRect, new Color(255, 255, 255, 0.5f)); // The cool and extension-methody way EditorGUI.DrawRect(myRect, Color.white.WithAlpha(0.5f)); //The extension method public static class Util{ public static Color WithAlpha(this Color color, float alpha){ color.a = alpha; return color; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 // The slow and annoying way EditorGUI . DrawRect ( myRect , new Color ( 255 , 255 , 255 , 0.5f ) ) ; // The cool and extension-methody way EditorGUI . DrawRect ( myRect , Color . white . WithAlpha ( 0.5f ) ) ; //The extension method public static class Util { public static Color WithAlpha ( this Color color , float alpha ) { color . a = alpha ; return color ; } }

I’ll admit that at first it doesn’t look like it’s that much faster and/or more easy. However, imagine having some color property that you, or even the player, has picked. If this color is not exactly white or black, it becomes harder to read – not to mention that drawing that defined color, but with decreased alpha, becomes a mess!

Take a look:

Wow, so much better! // This property could be edited with eg. a color picker! public Color verySpecificColor; // Draw the rect with the verySpecificColor, but half-transparent EditorGUI.DrawRect(myRect, new Color(verySpecificColor.r, verySpecificColor.g, verySpecificColor.b, 0.5f)); // Now draw it, but using the handy extension method (wow)! EditorGUI.DrawRect(myRect, verySpecificColor.WithAlpha(0.5f)); 1 2 3 4 5 6 7 8 // This property could be edited with eg. a color picker! public Color verySpecificColor ; // Draw the rect with the verySpecificColor, but half-transparent EditorGUI . DrawRect ( myRect , new Color ( verySpecificColor . r , verySpecificColor . g , verySpecificColor . b , 0.5f ) ) ; // Now draw it, but using the handy extension method (wow)! EditorGUI . DrawRect ( myRect , verySpecificColor . WithAlpha ( 0.5f ) ) ;

CONCLUSION

So there you have it! Extension methods are extremely useful, and can end up saving you a lot of time, with the added bonus of making your code look much better!

I’ve only shown a couple of examples with vectors and colors so far, but keep in mind – extension methods can be added to any type you want! Get creative with it, and if you don’t mind, share your favorite extension methods with other people in the comments!

I’ll leave you with a few more examples to get the inspiration flowing!