Unity Hacks: C# Extension Methods

Поделиться
HTML-код
  • Опубликовано: 20 сен 2024

Комментарии • 13

  • @emericcoude4068
    @emericcoude4068 2 года назад

    That seems super useful. I'm often creating exterior utilities, but this seem a lot more seamless to use in the long run!

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  2 года назад +1

      Extension methods can really simplify things. Glad the video helped!

  • @Definitely1V4NN
    @Definitely1V4NN 2 года назад +4

    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
      @JamesMakesGamesYT  2 года назад

      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?

    • @Definitely1V4NN
      @Definitely1V4NN 2 года назад

      @@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.

    • @twqzjsidIsndusiakdixisqjeksixi
      @twqzjsidIsndusiakdixisqjeksixi 2 года назад

      @@Definitely1V4NN if you can stand tons of reading, I suggest RB Whitaker C# Crash Course. He's a MonoGame developer, which uses C#.

  • @ZiplawDev
    @ZiplawDev 2 года назад +1

    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)

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  2 года назад

      Great tip! The Vector3 class is definitely one of the best candidates for a few extension methods!

  • @WeirdGoat
    @WeirdGoat Год назад

    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?

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  Год назад

      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.

    • @WeirdGoat
      @WeirdGoat Год назад

      ​@@JamesMakesGamesYT Noted, thanks for the reply! Could you please make a video on Generics, if you have the time, thanks in advance

  • @haxax
    @haxax 2 года назад

    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);
    }

    • @JamesMakesGamesYT
      @JamesMakesGamesYT  2 года назад

      Those are great suggestions! I'll definitely try out the ValidateComponent one