SOLID Principles in Unity
Вставка
- Опубліковано 21 лип 2024
- Sign up for the Level 2 Game Dev Newsletter: eepurl.com/gGb8eP
You don't have to be an amazing programmer to finish a game! This Unity tutorial will show you how to use the SOLID principles to write code that'll stand the test of time.
#UnitySOLID #UnityTutorial #GameDevelopment
📦 Download the project: / 26159833
This tutorial was created with the help of @JasonStorey
💬 Join Our Community
Discord: / discord
Patreon: / infalliblecode
Newsletter: eepurl.com/gGb8eP
❤️ My Favorite Unity Assets *
Odin Inspector and Serializer: assetstore.unity.com/packages...
Editor Console Pro: assetstore.unity.com/packages...
Rainbow Folders 2: assetstore.unity.com/packages...
Peek: assetstore.unity.com/packages...
Project Search & Replace: assetstore.unity.com/packages...
⚡ Unity 3D Plus: prf.hn/click/camref:1100l3e8M/...
⚡ Unity 3D Pro: prf.hn/click/camref:1100l3e8M/...
👋 Contact me directly at charles@infalliblecode.com
* Disclosure: These are affiliate links, which means I'll receive a commission if you use them to make a purchase.
Sign up for the Level 2 Game Dev Newsletter: eepurl.com/gGb8eP
Hey, great tutorial, any chance you could also link some reading materials for the SOLID principles you mentioned about?
@@notared5948 connect.unity.com/p/solid-principles-in-unity-part-1-solid-overview
Thank you for this tutorial.
In a way, we can replace interfaces with scriptableObjects. What is the best method in your opinion?
As a self-taught programmer and game dev enthusiast I find that nearly all Unity tutorials completely gloss over any semblance of proper design patterns. They cock up quick scripts that do what their title says in sub-optimal ways and are impossible to expand and build upon. For years, such tutorials have been teaching me to haphazardly connect various Unity components and just hope for the best, and as a result I continue to rewrite my code over and over while struggling to make anything more complicated than the simplest Space Invaders knock off.
Nowadays, I find these kinds of tutorials that actually talk about how code is written in in general much more useful than anything else. Being set in Unity helps a lot too, cause I'm not familiar with many frameworks outside of that. Thank you.
That's comparing apples to oranges. Most of the Unity tutorials on youtube are teaching you how to implement a certain game logic. And yes, they should be quick and concise, telling you exactly how to implement that logic and be done with it. On the other hand, code design patterns, principles and practices is a separate domain, and it's the responsibility of the user to learn them as well as the game logic on their own.
@@HATL201 where do I start from? I am a beginner
@@buriedomasta3375 how did it go
For those struggling to use the selection manager outside of this or any project with it already working, here's a hard-earned tip: make sure your camera has a Physics Raycaster component on it.
Thanks for sharing this! I definitely glossed over this very important detail lol
Go up and save the day you foo! LIKES LIKES LIEKS!!!
and tag said camera as Main Camera. I almost had an aneurysm trying to figure out why it wouldn't work for me.
I don't have that on my camera and it still works fine
Oh man, we need more tutorials like this.
More content is in the works! Stay tuned! :D
I much preferred this teaching style of your videos over the mock conversations of the current videos. its faster to the content without the bloat of expression, "story building" filler moments like showing off gameplay, and the greeting conversation. time is precious and videos like this feel much more efficient and attention holding.
Dude this is a great tutorial! Finally, a channel that goes in-depth into core coding principles that every game programmer should be following! Coming from a coding background this level of detail is perfect!
As a student, I can't explain how useful your videos are at improving my code. Love your channel!
I have not even watched the video in full and I'm fucking mindblown.
Love code quality tutorials, it's an area I'm always looking to improve in
I didn't really get it, until I saw you swap out one script for the other at the end. Then it clicked...mostly.
such a great video, and the production value is off the charts and very much appreciated
I love your tutorials. I like that you take on slightly advanced programming topics, show them in a real-world scenarios/use cases, and explain why the topic makes sense - eg. this video on the SOLID ideas that a lot of teams use.
So glad you found it helpful! I really like tackling these advanced topics because their so important and aren't covered very often.
Was not expecting to become a better coder while looking at Unity tutorials today. Nice work.
Came across your channel a couple of days ago and it’s one of my favourite gamedev channels. Your tutorial easy to understand and you explain everything properly like the real problem, options, solutions.
Great work man. Keep helping people like me.. thanks
Excellent tutorial. It's great to see videos like this that emphasize clean code principles!
This is exactly what I've been looking for. Thank you so much for the clear and concise example!
this explanation of how scripts should interact with each has been just what I needed, thank you
That was one of the most enlightening tutorials I have ever seen. Thank you very much for this!
In this case, instead of interfaces, I'd use abstract classes that inherit from MonoBehaviour. That way you won't have to assume something IS A MonoBehaviour that IMPLEMENTS an interface. Also, some assertions to lock these invariants. Great video, nice editing.
Wonderful. Simply wonderful. Good explanation for those of who don't know SOLID principles.
honestly this is the most helpful tutorial I have ever watched
Wow I really appreciate that!
@@InfallibleCode wow that was quick, I appreciate you making these tutorials so much! I am going to link your video on namespaces in my next devlog, it helped me organize my project really well. With Brackeys gone, I can see you blowing up as much as he did :)
@@n8dev I'm just glad that my videos are helpful, that's the most important thing :D
amazing tutorial, i like how this is just at the level that i am at programming so i can understand everything , thanks!
What a relief! I'm an absolute code beginner, so it's easier for me to separate everything for debugging so i naturally code using something very close to this principle. Though i was dumb for making extra steps, but it's very cool to see this is a good practice. Nice!
Brilliant!
I have been getting back to this video for quite some time, but I just wasn't ready to comprehend the ideas due to lack of knowledge.
After reading about SOLID principles on my own and looking through some examples, I was able to familiarize myself with the core ideas.
Afterwards, it was far easier to follow the video and I was able to learn much more from watching it.
Quick, cool, informative - as always Charles! Cheers, much obliged.
Brilliant and clear, thank you, looking forward to more in the series.
great tutorial. the community needs more videos like this.
All the SOLID principles explained and used in 15 minutes :) Good job.
Thanks for concise tutorial. Keep it up man!
Fantastic tutorial. When working in the corporate world, I always try and teach the SOLID principles to developers from day one. There's nothing more demoralizing then coming across legacy code with a 12,000 line method in a class that does 37 things and nobody knows how it works. ;)
This REALLY helps a lot as I'm just getting in designing my own game structure but not able to find a tutorial like this. KEEP IT UP BRO!!!!!!
Great video, looking forward to more architecture related videos. You should do a series on Event architecture(delgates, events, actions, func) to decouple code.
Awesome video! I would love to see this turned into Pure ECS!!
Pretty cool and useful tutorial. Keep it up!❤️
this might bring a little bit more organization in my coding. thank you very much!
Thanks for this. I will be using this in my game. I have a class that is equivalent to your selection manager that does way more than what your class did at the beginning of the video. I have a lot of refactoring to do :)
I like how you genuinely want to inform us about green being your favorite color, in a subtle side note 14:04 :D. Thanks for the great content!
Thanks for the video! This helped me make a WeaponManager class allowing for different weapon behaviors :D
Interfaces are nice. A dev partner and I have been working on a simple 2D shmup for about 1.5 years now (we are taking our time). It came in handy for a lot of stuff. We have interfaces like IProjectile that make it so that all our different types of projectiles HAVE to have a damage value, speed, lifetime, etc etc etc. It also comes in handy when using Zenject, which we do ;-)
Good stuff! I always love to hear about practical examples of OOP concepts being applied in game development.
amazing tutorial!
Thanks for your content, you are my go-to unity related programming Feed!!
Im a beginner and I have no idea what is going on. Maybe I will in a few years. Saving this vid :D
Very good and easy to understand example, good job and thanks
good video, I often have crowded classes that are difficult to expand, fater this video i will try to do everything clearly
Perfect explanation. Thank you for sharing your knowledge with the community!
You have taught me more than my 4 years in University
Wow! That's amazing to hear :D
Honestly don't know another channel that explains everything so clearly and understandable, great video man
Great job! Quick question though, why don't you have a Rider warning for the serialized + private fields that don't begin with an underscore?
So useful! Thanks a lot!
Glad you found it useful!
Hey new subscriber here. Thank you so much. Please keep doing these videos.
Great video! So many tutorials ignore good programming principles. I have a few bad habits to break.
Great tutorial! I have a question though, what would you do if you'd want to combine the two behaviors of the selection responses together, so everytime you hover over a GameObject you'd want to highlight /and/ outline it? Thanks!
Your pop up literly was in the way of me seeing what you were doing.... thanks
Wow. This is an excellent tutorial on the SOLID principles. The example you used was so relevant not just to game programming but programming in general. Well OOP anyway.
I draw on years of experience building enterprise applications when I work on these tutorials. Some people think that those two worlds wouldn't mix but there's plenty of overlap between business and game development. Software is software at the end of the day.
Hello, could you post the link of the outline assets you are using? I got 2 errors related to outline script.
Really great , thank you :)
This feels a lot like a Ryder ad :P
Really good tutorial though. Certainly got me thinking
It's so beautyful :)
Excelent, thanks!
Great video, but I have a question: it appears how in the update method of the SelectionManager you are first deselecting the object and then, if the raycast hit the object, put the selection back in the same frame. Wouldn't be better to put the select and deleselct methods in a if else statement, checking before the value of the raycast, so that you don't run both and save performance? also since those methods do the same thing, they could be unified by creating a single method which change the material or a value in the case of the outline, as taking it as input paramenter.
Quality content!
probably a bit late to this but that syntax highlighting theme you have is amazing, what setup do you have?
Quickie: we could use a List, right? To get outline AND highlight?
Great tutorial! Btw, captions are broken
You got my subscription, man!
Awesome, thank you!
@@InfallibleCode Got to complete test assignment for Unity programmer job. your channel really helps.
awesome!
Not that it is important to the lessons shown in this video but you could derive from Unitys IPointerEnterHandler and IPointerExitHandler interfaces (in the UnityEngine.EventSystems) instead of implementing your own raycaster. (Note that you do need to add a Physics Raycaster component to your Camera object for it to work, which I saw another comment mentioning but is actually not needed since the code in the video is doing its own raycast.)
I wrote my own "selection manager" raycast code a long time ago, I didn't knew there was an already made one. So I never had those limitations...
Your cats tail at the start made me think I had a spider on my screen and i proceeded to spend the next 3 minutes trying to find the spider in my phone
quality content!
Is there any way we can use both Highlight and Outline Selection Responses simultaneously? I've been playing around with it but as you've mentioned in the vid you have to remove one to place the other. I want to know if I could make a third Response class, say InteractiveSelectionResponse, which is responsible for handling mouse input when you press on a selected object, while still making use of the HighlightSelectionResponse component for visual feedback.
Hi! awesome tutorial you've created, but i have one question. What is the difference between the deselect and select code bodies? They look identical
nevermind, i didn't see that deselect was default mat and select was highlight mat. Thank you
Tip for clean code : event handlers should call a function, but not contain implementation. You want to have a quick look at it and understand what is going on, not read throguh the whole event.
how is the tag being referenced in the new classes you created from the selectionManager ?
why do you name some variables with an _ at the beginning?
Very helpful video. I've been trying to internalize the SOLID principles for learning how to write enterprise web applications too and have been struggling with how exactly to apply the open-close principle. I understand writing the functionality in a separate class but still haven't really figured out how to get the manager to automatically pick up the new functionality without adding code directly to it to instantiate the new functionality. It's interesting to see how you can automatically pull from the components with the matching interface here in Unity with how things are plugged together in the editor but I'm guessing I'm not going to be able to pull the same thing in my Node app xD.
I am very curious though, what would happen if you had both components extending the ISelectionResponse interface still attached to the same game object? Would it just return an error? How would it know which one to assign to _selectionResponse?
To answer your first concern, it's rarely about automation and more about maintenance. You have to give the program a class, it isn't magic, somewhere in the chain a dependency must be injected. In this case it's injected through GetComponent, and then assigned by the designer in the editor. If the ISelectionResponse wasn't a mono, you might assign the specific class in Start instead. Using a DI framework, you would specify which ISelectionResponse you want in the inector, and it would go through your code and swap all references for you. If this was an IWeapon, you might get the specific item from a random loot spawn...but of course you'd have to manually add it to the list of things that can spawn, so you still have to type it in or drag and drop it somewhere.
Imagine tomorrow Unity said "We arent using Nvidia PhysX, we are using some other thing" and you realize that the new thing doesn't have "AddForce" and instead uses something else that takes completely different parameters? Well now you have to go through every new error in the log and replace AddForce with something new, redoing all of your math. If this was a corporate app, you wouldn't have used Nvidias RigidBody, you would have used an IRigidbody with an AddForce function that interfaces with Nvidias Rigidbody. When your boss tells you they made a deal with some new physics company and you are using their backend now, all you'd have to do is write a different IRigidbody that interfaces with the new system with an AddForce function that does the conversion for you behind the scenes. Swap your dependencies in the relevant scripts and it all just works!
In Unity, a more realistic example would be a gun. What if you have multiple gun classes? Every time the player clicks do you "If Gun1 then Gun1.Shoot(), if Gun2 then Gun2.Shoot()...etc etc etc? Maybe we could use inheritance. All guns can inherit the abstract "Gun" class, and that class can have a "Shoot" method. Then the player class can just Gun.Shoot()...but what if we wanted to add a Final Fantasy Gun blade? Now we need a Stab...maybe we should have a Weapon base class with melee and shoot...but what if my gun is made of chocolate and my character can eat it? I can't derive from Powerup And Weapon...but you can have more than one interface. My can can be IItem, IRanged, IMelee, and IEdible, and my player script doesn't need 1,000 nested if statements.
Great video. Very solid info. I just wonder, what if you want to make the response conditional, f.ex say, if you looking north make them yellow, if not then use the green outline. Something like that. You would have to make a new class with both interfaces? or am i missing something obvious here? :) I will have to watch this a few times
If you wanted to response to be conditional then you could apply the Null Object pattern. But if you wanted to implement more sophisticated behavior then you'd need to implement a new class to handle it. Does that make sense?
@@InfallibleCode Makes total sense :) I think i understand it! I will try to make my own test. I like this pattern. The "kind of generic" approach hehe.
What about putting any ISelectionResponse as a scriptable Object. Since its only being used by the SelectionManager. You know how an scene object can have alot of components attached to it. Instead of adding another one script to the object, you can drag the selection behavior onto the script using it. Is this a good, or bad idea?
Very good :) as always. but what about DRY principle? I always wonder is this piece of my code, is small enough that can be repeated.
I always match DRY with the Rule Of 3. When I find myself writing it for the third time, I should really consider an interface or inheritance. The exception to this is third party plugins and frameworks. In this case, hes interfacing with an asset. What if he needs a new Unity version that isn't compatible with that asset? What if the asset has a bug, is closed source, and the creator ghosted the project? What if your boss tells you networking package A is out, and networking package B is in? Interfaces are like condoms for third party assets.
I have a few questions:
1. How does the concrete SelectionResponse seep into the code as it just initialises ISelectionResponse the interface?
2. What if I want both effect to manifest? Highlight and outline together?
yep. green is a great color.
This is a bit out of my scope of c#, i have an idea though it would be nice if you made a c# basics and intermediate series but to stand out from all the other youtube videos that do this you could make the video like you did in your "Unity Events in Action" and "C# Events in Unity" videos. I like how you incorporated problem solving while also teaching code in a in game project. Many many youtube c# series just teach you the basic syntax and read the definition from the documentation and don't show in game examples.
Please make more videos like THIS. Do it again with example 2, then example 3. You got alot of views on this. If you check out your spatialOS videos....not many views.... We appreciate your skills as a coder more then you dabbling into the "new".
Haha don’t worry more videos like THIS are definitely on the way. Only one more video left in the SpatialOS series so stay tuned to the channel.
@@InfallibleCode "Celebrates"
I am confused, is the downloadable project one before or after the refactoring is done?
The problem I have is I try to make my own physics, sort of.
I try to make everything come down to one vector3 mainMovement.
Then other methods such as SlopeDifference use transform.forward and input.
It becomes a cluster**** of variables they use that is shared and changed through the entire script.
thanks for the tutorial, it would have been great if you only used 2/4 of your objects to use the new green highlight methods while having the others still swap to yellow, if that is instead of it would have worked.
Hi Charles! Great video! Is making monobehaviour uicontroller an interface a good idea? I wanted to make an interface out of it so maybe pc has different UI than mobile game but if they use the same interface I could just find the interface in Game Manager class and use whatever UI type I get. But can't find a good way to find my UI interface beside var ui = FindObjectsOfType().OfType(). Do you think it's a good way to do it? Thanks!
Hi Charles! Sorry to bother you but after asking the question I came up with a solution. I get a public GameObject to the uicontroller monobehaviour and use on it GetComponent instead of looping through all the monobehaviour. Do you think it's a good workaround? Thanks!
Ding ding, subbed XD
thanks for the video, i have followed the instructions based on the video. but i have problem with this error 'The type or namespace "Outline" could not be found (are you missing a using directive or an assembly reference?)'. how to fix it? thanks
Did you make sure to actually download the Outline pack he said he got off the store? Outline isn’t a native component to Unity so unless you got the same asset off the store Unity has no idea what that is.
But actually how would be using the Liskov substitution principle? as you use other class with the same method names but actually the functionality changes, and the principle says functionality must not change
What's the "set by unity" thing?
you do run OnSelect() and OnDeselect() method at the sametime when mouse enters on "Selectable" object, what s the point of that?
What means liskov substitution principle in terms of Unity? Not so clear for me as a novice
i started unity 1 year ago and i tried this tutorial last year. I didnt understand anything :D maaan now i see how messy my code actually is. As a software engineering student in 2. semester i just learned how to code object oriented and with an mvc principle but i feel i cant use these methods in unity/c#
How to change _selectionResponse value at runtime?
Does raycast work in augmentation reality
Decent video, using so many IDE options instead of just copy-pasting was slightly infuriating tho :p
Yes make it more complicated looking at specific IDE functionality rather than copy paste
I think this is a double edged knife.
We use these patterns to make the code look more readable but at the same time we're over engineering the code with lot of classes especially for a small hyper casual game where at these patterns maybe never needed.
The only annoying thing here is that Unity can't serialize interfaces so you can't drag&drop them in the editor. That's why I usually create a abstract base class which also inherits from MonoBehaviour instead (obviously this only works if the class only implements a single "interface", because otherwise it couldn't inherit from multiple abstract classes).
It's kinda ugly, but I don't want to lose the drag&drop workflow in the editor.
What about using c# events to decouple the code?
Yes, I would have a script on each object with the functionality of changing its own material. Selection manager would only sent events on raycast.
its prob just a preference thing and im prob in the wrong, but seeing 2 if blocks just to call OnSelect vs OnDeselect really just jimmies my rustles. I would slap a ternary conditional on one line and be done