Generics in C# and Unity - Do More with Less Code

Поділитися
Вставка
  • Опубліковано 31 лип 2024
  • Generics in C# and Unity help reduce code duplication by reusing the same code for different types. This means less code to write, less code to debug and your project can get finished sooner. We'll look at generic functions, generic constraints, and generic classes within C sharp.
    Written Blog Post on Generics: onewheelstudio.com/blog/2020/...
    Tutorial Code: github.com/onewheelstudio/Adv...
    C# Constraints: docs.microsoft.com/en-us/dotn...
    Thank You!!!
    ►► Владимир Перепелицын for subscribing! ||||||||||||||| 121% ||||||||||||||| 24.3K/20K
    ►► Michael Linzenmeyer - Spoke in the Wheel for being the newest channel member!
    ►► Jeremy Wolf - Spoke in the Wheel for being the newest patron!
    Want to support the Channel?
    *******************************
    Shop the Unity Asset Store (affiliate): assetstore.unity.com/top-asse...
    Become A Channel Member: / @onewheelstudio
    Or a Patron: / onewheelstudio
    Grab a T-Shirt: onewheelstudio.com/merch/
    My video and streaming kit: kit.co/onewheelstudio/tutoria...
    Buy a Game Design Books: kit.co/onewheelstudio/game-de...
    Other Links
    ************
    Discord: discord.onewheelstudio.com
    Twitter: / onewheelstudio
    Devlog: onewheelstudio.com/
    Twitch: / onewheelstudio
    Timestamps
    *************
    0:00 Generics Intro
    2:09 Custom Generic Function
    5:26 Calling Generic Functions
    5:53 Generic Constraints
    6:46 Destroy Objects of Type
    7:32 Object Type Under Mouse
    8:26 Multiple Arguments
    9:18 Multiple Constraints
    9:39 Generic Classes
    10:46 Generic Object Pooling
    11:42 Generics vs. Inheritance
    #CSharp #BetterCode #GameDevelopment #Unity #generics #unitygenerics

КОМЕНТАРІ • 33

  • @OneWheelStudio
    @OneWheelStudio  3 роки тому +3

    Blog Post on Generics: onewheelstudio.com/blog/2020/12/27/c-generics-and-unity

  • @m1g4s
    @m1g4s 3 роки тому +15

    Wow. Just wow. Probably the best and simplest explanation of generic classes i've seen around. Well done, man!

  • @1DrowsyBoi
    @1DrowsyBoi 3 роки тому +7

    After watching this video, I went down a rabbit hole. My couple ongoing projects have me CONSTANTLY using the same couple lines of code to pull a random element from a list/array, so after this video I realized I can just make a static class and utilize the new knowledge about generics. Talked about it with a few people in a Discord server and one person hinted that I should go one step further and make it an extension method.
    Had no idea what they were, so I looked up them up, and when I figured that out, I had a glorious script that I can keep for future projects too. Even made some overloads for things like returning a list of multiple selections - with the option of also making sure each element is distinct, being able to also input a blacklist, etc. So now I can just use [List Name].SelectRandom().
    Extension methods could be a neat video idea too. I'm definitely gonna be exploring them more, but either way, thanks for the video, and leading me to learning several new things!

    • @OneWheelStudio
      @OneWheelStudio  3 роки тому

      Glad the video helped! Extension methods are defined on my to do list.

  • @EpicGamer63637
    @EpicGamer63637 2 роки тому +2

    This is the most amazing video I have ever watched on this topic.....
    Thanks!

  • @JackFoz454
    @JackFoz454 3 роки тому +3

    These videos are amazing. So far I've watched exactly two, once each. Both times, I've grasped a concept that had eluded me for months of trying with a ton of other sources.
    Can't overstate how good it feels to just... understand. You are a wonderful teacher. Thank you.

    • @OneWheelStudio
      @OneWheelStudio  3 роки тому +1

      Thanks for the kind words. I'm glad the videos are useful!
      If there's other topics you'd like to see explored I'd love to hear what they might be. Digging deeper into these intermediate topics has been really fun.

  • @TheKr0ckeR
    @TheKr0ckeR 2 роки тому +1

    That's the greatest generics explanation so far! Thank you

  • @josephramos6939
    @josephramos6939 Рік тому

    The way you explain and teach is so helpful to me. Sometimes I watch other content and it takes multiple videos for new programming concepts to sink in, but I understand what you're explaining on the first go-around. Thank you!!

  • @forginbits5457
    @forginbits5457 2 роки тому +2

    Thank you so much for making these tutorials, incredibly helpful and cristal clear explanations. I just discovered your channel and I guess I'm going to be watching a LOT of your videos. Cheers !

  • @theLegendPiyush
    @theLegendPiyush 11 місяців тому

    Underratedd!!!

  • @restushlogic5794
    @restushlogic5794 3 роки тому +2

    Awesome, thank you!

  • @vladisslavss
    @vladisslavss 2 роки тому +1

    Coool

  • @djdavidj
    @djdavidj 3 роки тому +2

    Thank you!

  • @alejmc
    @alejmc 3 роки тому +2

    I still find it very complicated when the right constraints and interfaces have to be implemented for equality/inequality compares, mathematical functions, etc to work. In my experience generics start very well as an idea but then sometimes I’m met with knowledge needed that I just don’t have yet.
    Great example on generics nevertheless, I do like the Pool system and a way to get all of the types from a shape. Can be used for many things, ability systems, collectible items types, etc etc... maybe mix-able with scriptable objects of sorts, to think about.

    • @THEspindoctor84
      @THEspindoctor84 2 роки тому +1

      yeah I hear what you're saying. I think the use of generics overall will never be strictly necessary, and could always just be worked around with more code. I think they are more useful when you constrain what the generic type will be, such as monobehavior, because at that point you can reliably know certain functionality of whatever type is passed in at run time. Or, if you're project has multiple levels of custom class inheritance, it might be useful to constrain the type to the parent class of you're project.

    • @THEspindoctor84
      @THEspindoctor84 2 роки тому

      Basically, I think there is a baseline level of complexity you're project must have to really use generics that much.

  • @unityvictor787
    @unityvictor787 3 роки тому +1

    Thanks for Tutorial!!
    Can you make a Tutorial of AssetBundles Build and Load please?

    • @OneWheelStudio
      @OneWheelStudio  3 роки тому

      Addressables are on my list for a potential topic - my understanding is that they have replaced AssetBundle functionality. No promises on when that will happen.

  • @Director414
    @Director414 9 місяців тому

    Great video, perfect pacing and content. Awesome!! :)
    In my own project, I think I might have made it too complicated for myself. I have problem reaching my script via code.
    I have my base-weapon-script like this:
    public abstract class WeaponBase : MonoBehaviour, IWeapon where T : WeaponDataBase
    then my different weapons inherit like this,
    public class WeaponStanardProjectile : WeaponBase (WeaponStandardData being a Scriptable Object that is of different types)..
    The problem I'm facing is trying to reach this weapon-script via code, turning the scrip on (when a weapon is picked up, and off then its dropped) . Seem the GetComponent() don't work for generic scripts?

  • @watercat1248
    @watercat1248 3 роки тому

    From what i understand the list is samthin very useful but i still don't get it how it works the only thing i understand is i add same type off faction for example cameras but i don't get how the understand which camera or what ever type off faction i want to add?
    For example if have 5 camera on my sean how that list knows to which off this cameras is the correct camera

    • @OneWheelStudio
      @OneWheelStudio  3 роки тому

      Selecting items from a list requires other logic. You could apply tags to an objects or check values on a class. A foreach loop can be very useful for that. A dictionary can also be very useful for storing and selecting information since the information is stored with a "key" so that could be "camera one" or "camera two."

    • @watercat1248
      @watercat1248 3 роки тому

      @@OneWheelStudio ok

  • @watercat1248
    @watercat1248 3 роки тому +1

    Can i ask you i questions do you work with team or you area solo developer like myself

  • @unclerandy398
    @unclerandy398 3 роки тому

    Did you create odin?

  • @sugarsores6521
    @sugarsores6521 3 роки тому +4

    I'm more smart now.