Learn how you can use C# extension methods to add your own methods to existing Unity classes. SUBSCRIBE Subscribe for more game dev tips and tutorials: bit.ly/3edJUA5
I've been struggling with c# so much. I know what everything means but when it comes to creating a game. I don't remember what and how to use code. Ur really good at teaching and tutorials. Would you ever consider making c# tutorial series specifically for making games on unity?
@@JamesMakesGamesYT I was thinking fundamentals? I've watched alot of series about them but it never seems to click. I understand alot from your tutorials and hopefully will learn the fundamentals better here. But it's up to you.. whichever you think would be more helpful for me (beginner-ish) will better.
A lot of what I use extension methods for is usually Vector3 calculations, for example, sometimes I need the average of several vectors, so I do vector.Average(// rest of the vectors)
What if I need 2 or more extension methods to extend SpriteRenderer, Transform and TextMeshPro, can I put them in a static class together or need to make 3 separate static classes?
That seems super useful. I'm often creating exterior utilities, but this seem a lot more seamless to use in the long run!
Extension methods can really simplify things. Glad the video helped!
I've been struggling with c# so much. I know what everything means but when it comes to creating a game. I don't remember what and how to use code. Ur really good at teaching and tutorials. Would you ever consider making c# tutorial series specifically for making games on unity?
Thanks for the feedback! Do you mean a series specifically on C# fundamentals or do you mean a series where I make a game from start to finish?
@@JamesMakesGamesYT I was thinking fundamentals? I've watched alot of series about them but it never seems to click. I understand alot from your tutorials and hopefully will learn the fundamentals better here. But it's up to you.. whichever you think would be more helpful for me (beginner-ish) will better.
@@Definitely1V4NN if you can stand tons of reading, I suggest RB Whitaker C# Crash Course. He's a MonoGame developer, which uses C#.
A lot of what I use extension methods for is usually Vector3 calculations, for example, sometimes I need the average of several vectors, so I do vector.Average(// rest of the vectors)
Great tip! The Vector3 class is definitely one of the best candidates for a few extension methods!
What if I need 2 or more extension methods to extend SpriteRenderer, Transform and TextMeshPro, can I put them in a static class together or need to make 3 separate static classes?
Great question! You can put all extension methods in one static class and it will work, even if the extension methods are for different classes.
@@JamesMakesGamesYT Noted, thanks for the reply! Could you please make a video on Generics, if you have the time, thanks in advance
Vector3.SetX(value) /.SetY /SetZ are pretty useful.
Something I use a lot to automatically get required components:
public static void ValidateComponent(this Component obj, ref T component) where T : Component
{
if (component != null) { return; }
component = obj.gameObject.GetComponent();
}
[SerializeField] private SpriteRenderer renderer;
[SerializeField] private AudioSource audio;
[SerializeField] private CharacterMovement movement;
private void OnValidate()
{
this.ValidateComponent(ref renderer);
this.ValidateComponent(ref audio);
this.ValidateComponent(ref movement);
}
Those are great suggestions! I'll definitely try out the ValidateComponent one