Unity Architecture - Composition or Inheritance?

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

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

  • @AlexanderZotov
    @AlexanderZotov 6 лет назад +150

    You are really one of the best youtubers on Unity and programming tutorials. Thank you veru much!

    • @Unity3dCollege
      @Unity3dCollege  6 лет назад +9

      thanks :)

    • @McNottagoose
      @McNottagoose 6 лет назад +5

      This is divine truth.

    • @ЯрославВальчишен
      @ЯрославВальчишен 5 лет назад

      facts

    • @--8251
      @--8251 4 года назад

      @@McNottagoose No, it's not. For true divine intellect go watch Terry Davis instead

    • @McNottagoose
      @McNottagoose 4 года назад

      @@--8251 ha damn thats a grave dig. But I agree, I have since found the truth that is terry davis may his genius soul rest in peace

  • @melancholerik
    @melancholerik 6 лет назад +137

    This is probably the single most helpful Unity-related channel on RUclips I've found so far. A real hidden gem.
    I feel like 99% of tutorials encourage just copying the code, without really explaining the thought process. And while the solutions they offer are fine, they're also out of context and not really applicable to even slightly bigger projects - that's how you get those monster classes. When you're trying to get into these things by yourself, those kinds of tutorials do more harm than good in a long run and encourage bad practices. Or maybe it's just a natural thing, doing stuff the wrong way to get started and then learn how to do it properly, I'm not sure.
    Anyway, I'm really thankful for videos/articles about architecture and programming patterns, you're one of the few people who can explain how these things work and what the benefits are, in a way that I almost instantly get it. Can't thank you enough Jason, your videos have been invaluable and I've learned more in a couple of months than I've learned previously in almost two years. You deserve a lot more views/subs.
    PS: I have an idea for a video if you don't mind: how about some Unity workflow tips, shortcuts or other fun stuff that you often use when working in the editor? That could be super handy to a lot of people.

    • @Unity3dCollege
      @Unity3dCollege  6 лет назад +19

      Thanks! That's a great video idea too, will have to figure how to structure it, but I think it'd be worth doing for sure :)

    • @HippieJavelin99
      @HippieJavelin99 6 лет назад +3

      I know that your comment is 10 months old but I feel the same way! It was so hard to find these videos, although I was looking for it. I just didn't know what I'm exactly want to find. It's shame that everyone knows Brackeys content when it's not that helpful (sometimes harming as you said). Anyway, I'm really thankful for your videos Jason!

    • @punixerz
      @punixerz 5 лет назад

      Yep, totally right. I found this channel via facebook on unity college by luck.

  • @WhyDoINeedAHandle__
    @WhyDoINeedAHandle__ 6 лет назад +47

    This video and the SOLID principles video should be mandatory for everyone ... not just Unity, but for life in general.

  • @oatcube3961
    @oatcube3961 4 года назад +8

    These videos are awesome. The more flashy Unity RUclipsrs like Brackeys and CodeMonkey have very good tutorials also. But the great thing about your tutorials is that you don't just give us a specific way to do one thing, but teach us the principles not only for Unity for coding all together. I believe lot of your viewers are self taught people and the basic principles are the things that the coder easy skips when starting. I have a CharacterAI script in my game that I have been splitting apart a bit to achieve this, but it's still about 600 lines long.

  • @melodysprout23456
    @melodysprout23456 3 года назад

    the [RequiredComponent(typeof(T))] was EXTREMELY helpful. Thank you!

  • @AussieRaver1996
    @AussieRaver1996 3 года назад

    I love it when you mention something not specific to the video, but you go ahead and explain it.
    I learnt heaps, thanks!

  • @imheretosleep
    @imheretosleep 3 года назад

    I'm really bad at coding and I'm outta here speechless how lucky I am to find this channel explaining things that saved my motivation in becoming a game developer. Thank you so much good sir! I will do my best to improve and to make a game that everyone will love to play. Once again, thank you!

  • @davidferguson9875
    @davidferguson9875 3 года назад

    This clearly answered my questions for porting an existing Windows application with heavy class structure to Unity. Brilliant. Thanks Jason. You are doing some good work here.

  • @dknighter2
    @dknighter2 6 лет назад +21

    I'll admit I make monster classes all the time. Thank you so much for this, I'll definitely be using composition in the future!

    • @Unity3dCollege
      @Unity3dCollege  6 лет назад +12

      We've all done it :) I spent years making them before I found the light ;)

    • @thedude4039
      @thedude4039 4 года назад

      I think I just make way too many little classes.

  • @Landon_Hughes
    @Landon_Hughes 3 года назад +1

    You've EASILY become my favorite source to learn more about Unity 😁 Thanks for all the great content! ✊

  • @DavidBarile
    @DavidBarile 5 лет назад

    Oh man! You are the programming guru I've always wanted... a true master! Deep understanding of the principles coming from real-world professional experience. You're clear, charismatic as necessary, and cover countless principles that I've only vaguely understood or are completely new to me -- and useful! Thank you so much!

  • @bronzekoala9141
    @bronzekoala9141 5 лет назад

    Probably the single most helpful video I've ever seen when it comes to code design in Unity.

  • @maddehaan
    @maddehaan 5 лет назад

    This is supporting what I do for some time now, making the object an organic 'disciplines' classes. A. Stats, B. Autonomous, C. Actions, D. Responses, E. Extra (or whatever A,B,C,D,E are needed).

  • @greglyons288
    @greglyons288 6 лет назад +5

    Thank you for covering this. I am a fan of the composition structure myself, but maybe that's because it's easier for my head to wrap around it.

    • @Unity3dCollege
      @Unity3dCollege  6 лет назад +2

      Agreed, it makes it much easier to grasp the project

  • @jerrywalz6308
    @jerrywalz6308 6 лет назад +1

    Four minutes into your tutorial, I found what I needed. I just wanted to know how to add inheritance from a Non-Unity C# class into derived classes of Unity so that I could use Inheritance like a normal programmer, else all it is, is a scripting language and that would be a waste. Much appreciation, it is hard to find people who are intuitively insightful to answer our "newbie" questions.
    public class Humanoids : Monobehavior { }
    public class EventManager : Humanoids { }
    So easy but hard to make that bridge with out knowing this.

    • @Unity3dCollege
      @Unity3dCollege  6 лет назад

      Awesome! Glad to hear it was helpful :) Inheritance is an amazing tool to have available.

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

    Thanks Jason, instead of just the classic, animal, dog/cat example, you used functions and also showed how protected virtual and override is used. Makes so much sense why this is useful or at least time saving! Keen to play around with it and keep improving my code!

  • @kayronmercieca8226
    @kayronmercieca8226 5 лет назад

    I understand both Inheritance and Composition, but honestly it feels a lot more "natural" to use Composition in the context of programming a game. This is a great video, thank you.

  • @Zethneralith
    @Zethneralith 5 лет назад +4

    It seems to me that the component structure makes more sense if you find it first. I started programming around the time OOP and inheritance were the natural law. I think I'm liking composition for Unity development, but it's taking me a surprising amount of effort to think about it as clearly as I do inheritance.

  • @Ninja-Dev
    @Ninja-Dev 2 года назад

    @Jason Weimann
    Inheritance 0:43
    Composition 9:50

  • @davidbingham8062
    @davidbingham8062 6 лет назад +1

    This is the video I've always wished someone would make. Thank you!

    • @Unity3dCollege
      @Unity3dCollege  6 лет назад

      you're welcome, and thx for the comment :)

  • @Alluvian567
    @Alluvian567 5 лет назад

    Thanks so much for this. Just getting started in Unity (last time I did any REAL programming was in actionscript 3, lol) and this really helped clear my head regarding how to re-use and organize code intelligently. Unity is in many ways pretty similar to the flash IDE actually. Learning to move AWAY from such heavy inheritance that I was being taught back in the day is refreshing.

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

    As always - perfect video for learning Unity and coding. Thank You for Your hard work!

  • @JerryIsdale
    @JerryIsdale 6 лет назад

    Really appreciating your Clean Software Design topics! Composition is a very powerful feature of the Unity Engine. It is a style of ObjectOrientedProgramming that was passed over by most of the mainstream systems. I recall touting it back in 80s for modeling/animation software but not finding good support in languages, etc.

  • @guilhermejunqueira6598
    @guilhermejunqueira6598 6 лет назад

    Great talk. The proper way to use ECS archtecture is definately via Composition. You explained the concept really really well, i would love to see you talk more about SwArchtecture in Unity, subscribed.

    • @Unity3dCollege
      @Unity3dCollege  6 лет назад

      It's been a while since I wrote this, but this method worked last time I tried: unity3d.college/2017/04/24/360-video-in-unity-5-6/

  • @filipathtc
    @filipathtc 4 года назад

    One of your most helpful videos for me. Thank you very much!

  • @TheBcoolGuy
    @TheBcoolGuy 4 года назад +14

    What about interfaces vs composition in the context of Unity?

  • @ruslan_yefimov
    @ruslan_yefimov 3 года назад

    I have a fobia of writing spagetti code, so your tutorial really helped) Thanks a lot

  • @kpickett9128
    @kpickett9128 4 года назад

    This is awesome!!! I'm a C# newbie and found this to be a very valuable high-level explanation of how to think about setting up game code. Thank you for making this!
    Time to go make a bunch of "behavior legos" and build cool things out of them. : D

  • @wadu7205
    @wadu7205 5 лет назад

    This vid just changed my life. Ty you are doing gods work.

  • @SinusQuell_
    @SinusQuell_ 5 лет назад

    Your videos are amazing. Your professional knowledge is so rare on RUclips, and super helpful! Keep it up

  • @JashJhaveri
    @JashJhaveri 5 лет назад

    just found your channel and i really like the way you approach teaching. Good to find videos more on the design philosophy!

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

    on 11:30 it should be a RequireComponent inside the CharacterMovement class, because its the one that needs PlayerInput, the latter does not need CharacterMovement to work.

  • @Gary1q2
    @Gary1q2 4 года назад

    yay good video on composition and inheritance with some solid examples!! thank u :D

  • @0tiii
    @0tiii 4 года назад +1

    You can make inheritance follow the solid principles easily if you segregate well. Eg NPC should never have a 'combat' functionality by default since it is supposed to describe a plain living entity, which doesn't necessarily have to be able to fight. If you use OOP and inheritance correctly you will only project the functionality on the classes, that reflect their most basic 'actual form'

  • @НейтральныйМаппер-з2м

    As an Unreal Engine programmer it was interesting video, i've received some decent ideas about architecture in general bc im a noob :D. It was really interesting and helpful, thanks

  • @MohamedAdelQuickDeveloper
    @MohamedAdelQuickDeveloper 6 лет назад +1

    I really liked this video, specially at this time, I have been stuck in a project which is a mess. And we need to add a multiplayer mode in it. I think some refactoring would help. Thanks for these videos It helps alot.

  • @lando6583
    @lando6583 4 года назад

    great video! gonna use composition to start cleaning up my 1000 line classes!

  • @yangez93
    @yangez93 4 года назад

    Your tutorials are really helpful, thank you very much Jason

  • @YookaKim
    @YookaKim 5 лет назад

    holy shit.... this is exactly what i was looking for!!! makes some things so much clearer

  • @АлексейНикитин-й1з
    @АлексейНикитин-й1з 4 года назад

    Превосходный контент! Обожаю смотреть ваши материалы!

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

    Thanks for the video sir. 😀👍🏻

  • @amirmreaee
    @amirmreaee 3 года назад

    It helps me alot. Thank you, Jason

  • @harmoniac.design
    @harmoniac.design 5 лет назад +1

    So incredibly helpfull and important information, thank you!
    I was always stuggling how to put things together and I was searching ALOT for this kind of info. This should go into the Unity Coding series!! You learn all sorts of things about what is what, syntax and so on, but a more distant overview about projects, coding in general was missing badly.
    One thing I'm still missing is the actual input when coding, like shortcuts, comment things out quick, how did you changed the first letter so quick, how to implement a method part when you wrot just the methods call in update and much more. Would mucho appreciate a tutorial or just tell your favorite shortcuts/techniques.
    Anyway, keep it up! Good stuff! Greetings from Germany

  • @popuzin
    @popuzin 6 лет назад +3

    I like your videos! Will you continue this playlist by teaching us Design Patterns applied to Unity game programming? I mean not only classical Singleton, Object Pooling and State Machine, but also others like Factory, Adapter ect.?

  • @davidvarga2916
    @davidvarga2916 9 месяцев назад

    Guilty as charged. I've worked on my game for a year(on and off). Nearly finished the base game, but realized that the gameplay design was wrong and the complexity of large pieces of codes made me want to push my fingers into my eyes. So I've stopped. Now I've finally redesigned the gameplay after months and all I needed was this. Thanks. I'll start cutting up my code into little pieces.

  • @LeviAckerman-xt7dx
    @LeviAckerman-xt7dx 3 года назад +1

    its beautiful.

  • @williamedwards3911
    @williamedwards3911 5 лет назад

    Thanks for this tutorial - really helpful

  • @DoomCatcher
    @DoomCatcher 3 года назад +1

    Really useful thank you, how do you decide when you should use a component for health or use an interface that "forces" a class to have hp if that makes sense?

  • @rafa_guitar
    @rafa_guitar 6 лет назад

    Very useful! You help a lot, thanks!

  • @Blackcendre
    @Blackcendre 6 лет назад +1

    Thank you so much for this video ! :D

  • @bobsignori9024
    @bobsignori9024 5 лет назад

    By the 12:50 you talk about events...
    It can be used that idea in a multiplayer project?
    Like, classes inheritance and events?
    If so, do you have something like that more specific?

  • @peskitsnet
    @peskitsnet 6 лет назад

    hi would it be possible for you to show us how to create a boxing 12 round system with cut scenes between rounds please .

  • @baroquedub
    @baroquedub 6 лет назад

    Thanks for another brilliant video. I wish I had the money to do your courses, they're sadly out of my price range, but I'm very grateful that you give so much of your expertise for free on RUclips :)
    Can I ask... with composition is there a way to know or enforce the order in which the various components are acted on? Does the order in the Inspector make any difference? I know about Script Execution Order Settings but I can see this getting very convoluted on a large project with multiple scenes. I guess there's also the option of choosing carefully between initialising things in Awake() or Start(). Just wondered if you had any tricks or recommendations for this?

  • @lvx969
    @lvx969 6 лет назад

    I tend to use a combo of both.

  • @f123-i4d
    @f123-i4d 6 лет назад

    could you make a video talking on how to manage packets? great videos btw :)

  • @lizardkamal
    @lizardkamal 6 лет назад +8

    I wish I can see you work on a like full project maybe something small with solid principles.

    • @Unity3dCollege
      @Unity3dCollege  6 лет назад +6

      I'll definitely have to do that sometime soon

  • @RossYlitalo
    @RossYlitalo 5 лет назад

    Very interesting--Thank You!

  • @0tiii
    @0tiii 4 года назад

    I feel one of the mein backdraws of being able to use inheritance well in Unity is their totally sucky default serializer.

  • @SealDev
    @SealDev 4 года назад

    Thanks for this :) Very useful!

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

    Awesome tips thanks

  • @slam2pan
    @slam2pan 3 года назад

    What keyboard are you using?

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

    .......Damn.......this.......is.......A.......Good.......Video.......

  • @haakon023
    @haakon023 4 года назад

    Hey, I am curious, would you prefer a public static event OnDied in the health class or having the pooling system actually have a reference to health Component?

  • @krissloo143
    @krissloo143 4 года назад +3

    that moment when you realize you've been using composition the whole time xD

  • @xatov2138
    @xatov2138 3 года назад

    You are the B E S T ! With dark mode it might be even better 🤣

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

    Would I be correct in saying that the proper syntax today for on died would be OnDied?.Invoke();

  • @sconosciutosconosciuto2196
    @sconosciutosconosciuto2196 6 лет назад +1

    I've a doubt.. If I have a script that manages weapon firing... If I have a weapon that fire 1 bullet once and another that fire 3 with different angulation.. Is better to do a inheritance (do another script that change the base class), or composition, so i do one script for firing like the first case, another for the second?

    • @sconosciutosconosciuto2196
      @sconosciutosconosciuto2196 6 лет назад

      I also want to do a system where there are some weapons that need charging before firing the first bullet (like a minigun) and others not.. In your opinion is better composition or inheritance? (Sorry for stupids questions)

    • @sconosciutosconosciuto2196
      @sconosciutosconosciuto2196 6 лет назад

      (For the last questiob)
      I want to do composition but I don't know how to do that if there is the component, before firing, the weapon need to wait.. Should I use event (I can't use them)?

    • @Rhino1004
      @Rhino1004 5 лет назад +1

      @@sconosciutosconosciuto2196 Ever seen that video called, "Ability System with ScriptableObjects"? You should consider adding methods to a 'Bullet' ScriptableObject that your weapon can use. Then, make an SO that inherits from Bullet, call it something like 'SplitBullet', give it a different Fire() method, and you're good.

  • @DustVoltrage
    @DustVoltrage 5 лет назад

    I've always felt like the unity component system was screaming for composition and make it extremely easy to manage !
    The only time I dealt a lot with inheritance was for my rpg equipment/inventory system and it was much more complicated...

  • @sunflowerchocolates
    @sunflowerchocolates 6 лет назад

    Nice, thanks for all these tutorials! Can you create a tutorial on a bubble burst games? Like when you click on the bubble, it pops. I'm trying to do this, but can't figure out how to switch back from a bubble image --> popped image with the prefabs and animations I've created.

  • @abaddondestroyer1124
    @abaddondestroyer1124 5 лет назад +2

    Can you maybe start a series of making a RPG game - you know because of the complexity of how most classical RPG's work (but not as complex as an MMO) and also make it 2D for simplicity sake - The point being is - you are full with knowledge and if we would have such a series, we would learn first hand from you design patterns, best practices, real life solutions in specific scenarios.. damn make it on udemy if you like - shut up and just take my money (I am kidding - don't shut up keep it comming) :)!

  • @dango_3016
    @dango_3016 6 лет назад

    I'm composing a lot of scripts in my current project by having a Manager class that decides when to use which script: for example disabling and enabling different camera or movement scripts.
    Do you think this is a good method or inefficient somehow?

  • @northstar7978
    @northstar7978 6 лет назад

    one step further is using scriptable objects for adding functionality to gameobjects. maybe you should talk about it sometime?

  • @juts89
    @juts89 6 лет назад +1

    Registration is showing closed on the linked course, FYI

    • @Unity3dCollege
      @Unity3dCollege  6 лет назад

      Thanks for letting me know, just fixed the link :)

  • @Killer_Nads
    @Killer_Nads 6 лет назад

    Thats great thanks! Ill go with composition from now on, it seems much easier to write and also to maintain!! Just a question after using the composition Health class, how do you access its TakeDamage() method from another class? Would it be the same thing as inheritance? ie. you have to first find the object by tag and then use "npc = GetComponent" and then use "npc.takeDamage" ?? or is there another better way to do it? thanks

    • @beaverjoe9171
      @beaverjoe9171 5 лет назад

      FindTypeOfObject()???? He offered some reference to catch GameObject

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

      You can use an interface in the Health class and receive hits from enemies by accessing the class, regardless of who it is, the Observer pattern will be executed

  • @utilityy
    @utilityy 4 года назад +5

    This feels like a personal attack tbh

  • @kiyasuihito
    @kiyasuihito 4 года назад

    Very nice tutorial. It just seems weird to inherit from monobehaviour when a class doesn't need its functionality, like in your health class. Maybe I'm thinking about it wrong...

  • @MisterMashu
    @MisterMashu 6 лет назад +1

    happy times

  • @bigjamar
    @bigjamar 5 лет назад

    Muchas gracias..!!..excelente..!!

  • @latbat58
    @latbat58 5 лет назад

    isnt this really similar to single responsibility principle or am i missing something? sry i suck at codin stuff

  • @Danyu91
    @Danyu91 5 лет назад

    Why did you have to call base. Update()? I thought the override keyword was supposed to let you have different "Update()"s from the base class.

    • @tomwhitcombe7621
      @tomwhitcombe7621 5 лет назад

      it does, but there may be common stuff in the base class you want to call as well. One of the problems is knowing when this is a must to not break stuff or not.

    • @Danyu91
      @Danyu91 5 лет назад

      so, if you call base.Update(), it will call whatever is in the base class Update, but if you just call the override Update(), with custom functions, it will do the custom functions?

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

    You da man!

  • @soyfandev4925
    @soyfandev4925 5 лет назад

    thank u sir

  • @thedude4039
    @thedude4039 4 года назад

    9:00 In fact, let's umm, watch all the movies you want for one low price.

  • @CabinIcarus
    @CabinIcarus 6 лет назад

    This is like the ECS.

  • @MoonLiteNite
    @MoonLiteNite 4 года назад

    @7:55 this is me...

  • @Aladato
    @Aladato 6 лет назад +7

    I never really liked Inheritance, while on the other hand, Composition immediately clicked with me!

    • @Ziplock9000
      @Ziplock9000 5 лет назад

      more messy and less performance by far for cross calls

    • @Scottx125Productions
      @Scottx125Productions 5 лет назад +4

      I'm the opposite, I prefer inheritance. Clear line of hierarchy with classes. You don't end up mixing and matching making it much easier to follow the code.

    • @Zethneralith
      @Zethneralith 5 лет назад

      ​@@Scottx125Productions I'm in the same boat as you. Inheritance just makes more sense to me. But I'm finding that it has problems when we don't know the full scope of our application from the start, which can lead to a great deal of refactoring. It seems easier to follow single-responsibility when using components, so I'm starting to like components more. It does seem that Unity prefers this approach, as well, but it's difficult for me to switch my brain over from inheritance to composition.

  • @idle.observer
    @idle.observer Год назад

    But you can use Composition and Inheritance together.

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

    I think what annoys me about the whole component system is that in your example and many others, these components still depend on one another. CharacterMovement still needs the NavMeshAgent and is useless without it. This is not a criticism of your design, but rather this idea that "you can mix and match components and it just works" but you really cannot in many cases because they usually need to communicate in some way.

  • @keyboard1819
    @keyboard1819 4 года назад

    I understood a thing after this.....
    My code really sucks

  • @XtroTheArctic
    @XtroTheArctic 5 лет назад

    I love your videos but the OnDied event name is horrible :) Event names should be Verb-ed only, like Died, Closed, Shown. OnXXX format is used for override(virtual) methods only :)

  • @Pspet
    @Pspet 5 лет назад +2

    - starts showing an inheritance design
    - continues to mess up the inheritance design
    - points out that inheritance doesn't work well
    Lol, if you want to have a Merchant in your design it should not inherit directly from your NPC class. Inside the NPC class you have some combat logic, not all NPC's do combat. Instead you should have a CombatNPC class that inherits from the NPC and put the combat logic there. This way, the Merchant could inherit from the NPC correctly with no problem at all.

    • @TheBendixSA
      @TheBendixSA 4 года назад

      Until you want to add a special merchant that can do combat. Then you're fucked

  • @sconosciutosconosciuto2196
    @sconosciutosconosciuto2196 6 лет назад +1

    So inheritance is useless

  • @user-lv6qm3fj2z
    @user-lv6qm3fj2z 5 лет назад

    The composition approach looks too heavy - 3 components on each character instead of 1 is a fuck up.

    • @markcooke4866
      @markcooke4866 3 года назад

      Using unity components is only one approach to composition. You could also just use class composition instead to separate concerns.