background top icon
background center wave icon
background filled rhombus icon
background two lines icon
background stroke rhombus icon

Download "SIMPLE Tip For Better Unity Game Architecture"

input logo icon
Video tags
|

Video tags

unity tutorial
unity arcade game
unity game tutorial
unity
game development
game dev
game development unity
programming
c#
software development
learn to code
learn programming
unity tutorials
unity devlog
indiedev
gamedev
unity3d
unity architecture
unity best practices
untiy game architecture
Subtitles
|

Subtitles

subtitles menu arrow
  • ruRussian
Download
00:00:00
something really interesting happened to
00:00:01
me last week I had four different people
00:00:03
reach out to me for advice on their game
00:00:05
projects all four of them were Building
00:00:07
Systems and they felt like things were
00:00:08
starting to go sideways with the
00:00:10
architecture and the symptoms with all
00:00:12
of these projects was the same The
00:00:14
Inheritance hierarchy was starting to
00:00:16
get out of control with some of their
00:00:17
class structures so today I want to talk
00:00:19
about one of the foundational principles
00:00:21
of software architecture which is to
00:00:23
favor composition over inheritance and
00:00:26
we're going to take a look at what that
00:00:28
looks like and we'll do a little bit of
00:00:30
factoring things will get progressively
00:00:31
more advanced as we go along and I've
00:00:33
got some good tips for you at the
00:00:38
end everybody is familiar with this sort
00:00:41
of thing where we have a base abstract
00:00:43
class say a vehicle and we start
00:00:46
extending that with types that make
00:00:47
sense a car a truck of course those are
00:00:50
both vehicles but what happens when we
00:00:52
start having things like a black car
00:00:54
does our hierarchy continue to make
00:00:56
sense or is something starting to go
00:00:58
sideways here one method that can help
00:01:00
you build good hierarchies of
00:01:02
inheritance is to use Isa and haza a car
00:01:06
is a vehicle but a car has a color in
00:01:11
fact all vehicles have colors this
00:01:13
simple little trick can really help when
00:01:15
you're designing systems why don't we
00:01:17
add one more thing that belongs to a
00:01:18
vehicle how about an engine so all
00:01:21
vehicles have engines but the engine
00:01:24
itself has its own properties so I would
00:01:27
represent that as its own class and here
00:01:29
copile is already filled in that it has
00:01:31
a horsepower that makes sense so your
00:01:34
class names Define what this sort of
00:01:36
thing is whereas the members of your
00:01:39
class Define Its Behavior and its
00:01:41
properties remember that any sort of
00:01:43
class can also have a type of Behavior
00:01:46
now I want to look at a slightly more
00:01:47
complex example that's based a little
00:01:50
bit on what one subscriber asked me
00:01:52
about and something that I've been
00:01:53
working on for myself so a very simple
00:01:56
version of a resource Gathering system
00:01:58
might start with an enum of a a whole
00:02:00
bunch of different types of resources we
00:02:02
might Define a base node that's just
00:02:04
going to have an abstract method that
00:02:06
will return the resource type that this
00:02:09
node represents so we might start
00:02:11
building up an inheritance hierarchy
00:02:13
with a stone node just return that type
00:02:16
we could go a little further and make
00:02:17
one for a tree maybe an oak
00:02:19
node now one of the questions that was
00:02:22
posed to me is what if we want this type
00:02:25
to actually represent more than one type
00:02:27
of element this might work some
00:02:29
something like when we harvest the base
00:02:31
elements we have a chance to get a
00:02:33
special item on some nodes so for that
00:02:35
we might extend our BAS node class with
00:02:37
a special node abstract class and then
00:02:40
we start making a little bit more
00:02:42
complex nodes out of that so for example
00:02:44
maybe we have an iron node here and our
00:02:47
special gather me method could return
00:02:49
the iron but we also need the iron node
00:02:51
to return a base type so we also need to
00:02:55
start overriding our regular gather
00:02:56
method here and maybe it just returns
00:02:58
regular stones
00:03:00
perhaps so you can probably see how this
00:03:03
sort of structure The Inheritance
00:03:05
hierarchy is going to get out of control
00:03:07
very quickly so at this point you can
00:03:10
think what do all of these things have
00:03:12
in common what are they what is the ISA
00:03:15
in this case well they're all nodes the
00:03:18
only difference between them is the type
00:03:21
so let's wipe this out and start over
00:03:24
now a node has a type let's define that
00:03:27
as a member now at this point we might
00:03:29
say okay it's probably a good idea to
00:03:31
also make one for the special node that
00:03:33
overrides the base node but if we look
00:03:35
at these what's really different here if
00:03:38
I were to leave this in this state the
00:03:40
consumer of these nodes is going to have
00:03:43
to do some casting to figure out whether
00:03:45
or not the node is special or not what
00:03:47
if we just combined these two into one
00:03:49
and then we could say optionally we
00:03:51
could have a special resource type but
00:03:53
it could just be empty as the default
00:03:55
right so I can just remove this bit and
00:03:59
just switch switch these around a little
00:04:00
bit so they're a little bit more
00:04:01
readable but this could really be our
00:04:05
base class but let's think about this a
00:04:07
little bit more does the bass node have
00:04:09
a resource type or does it actually have
00:04:11
a resource it really does have a
00:04:13
resource that we want to pass around and
00:04:14
we're probably going to want to know how
00:04:16
much of it we're passing around we might
00:04:18
want to know a few other things too but
00:04:20
we could start with this and so with
00:04:23
that in mind we can actually just change
00:04:25
all of these up here to be resources
00:04:28
I'll just give them some slightly better
00:04:29
names here now ideally the resource
00:04:33
itself is going to represent a resource
00:04:35
in our game but there's static data
00:04:37
associated with that that we might want
00:04:38
to configure with a scriptable object so
00:04:41
why don't we make another class resource
00:04:43
config this way we'll be able to set up
00:04:45
any fields we want to be configurable
00:04:47
inside of unity and then we could have a
00:04:49
public method here that will just create
00:04:51
our runtime resource for us I'll just
00:04:53
call it generate and I'll let it create
00:04:55
a new resource for us for the time being
00:04:57
I'll just set these resource fields to
00:04:59
be public in ryer if you press control
00:05:01
twice and hold it down on the second
00:05:03
press you can press up and down and
00:05:05
it'll give you multic cursor you can do
00:05:07
the same kind of thing in VSS code with
00:05:09
alt click okay moving on we can come
00:05:12
back up here to our bass node and just
00:05:14
make some serialized fields for these so
00:05:16
we can call them resource config and
00:05:18
special resource config then in our
00:05:19
awake method we can just use the public
00:05:21
method that we just created to assign
00:05:23
references to these so now at this point
00:05:26
do we even really need an abstract class
00:05:28
probably not
00:05:30
this one class is going to handle
00:05:32
exactly what we needed to get out of all
00:05:33
those other classes we started with all
00:05:36
we really have to do is have an
00:05:37
implementation for both of the methods
00:05:39
and that's it that's a very simple
00:05:41
system it should do what we need I'm
00:05:43
going to move the scriptable object into
00:05:44
its own file and just add an annotation
00:05:47
here so we can create them in unity now
00:05:49
just because I removed all of the
00:05:51
inheritance here it doesn't mean that
00:05:52
that's necessarily the right thing to do
00:05:54
all the time there are no absolutes in
00:05:57
this you know you got to figure out
00:05:59
what's going to work best for your
00:06:00
situation sometimes you can get it down
00:06:02
to really needing no inheritance
00:06:05
generally speaking any system you build
00:06:06
in C is going to use both inheritance
00:06:09
and composition you just need to figure
00:06:11
out which parts are the ISA and which
00:06:14
parts are the
00:06:15
haa so I created a few object
00:06:18
definitions I'm just going to go over to
00:06:19
this rock in my scene here and why don't
00:06:21
we just add the Bas node onto here and
00:06:25
we can give it some details and see what
00:06:27
happens just make sure it's all working
00:06:29
before we jump into the next part here
00:06:32
so for this node let's put Stone in as
00:06:34
the base resource and the special
00:06:36
resource can be gold now I might have to
00:06:39
add a little bit of code to my hero so I
00:06:40
can actually call The Gather methods on
00:06:43
this object uh but first let's put a
00:06:46
capsule collider on here just so that we
00:06:47
can actually interact with it in a very
00:06:49
simple way um I'll flip that on there
00:06:53
and just uh adjust it a little bit
00:06:58
here okay just make it a little bit
00:07:00
narrow it doesn't really matter for this
00:07:01
I suppose and then let's just uh jump
00:07:03
back into code and we can add a little
00:07:05
bit to the hero
00:07:07
class so I can probably just let
00:07:09
co-pilot figure this one out for the
00:07:11
most part but basically we can have a
00:07:13
harvest method and we can just check for
00:07:16
anything around us we can use a overlap
00:07:18
Sphere for this maybe a radius of about
00:07:20
five so we can walk up to it and press a
00:07:23
key and see what happens so for every
00:07:26
Collision let's check for a base node no
00:07:29
base node we just continue otherwise
00:07:31
we're going to gather the First Resource
00:07:34
if the First Resource is type empty we
00:07:36
continue and if it wasn't empty let's
00:07:38
also get the special
00:07:40
resource now in our update method we
00:07:42
could just say if we've pressed let's
00:07:44
say the F
00:07:45
key then we'll just try to
00:07:48
harvest yeah that's simple enough
00:07:50
that'll work let's jump back into unity
00:07:53
recompile and click
00:07:56
play all right so I'm just going to
00:07:58
scroll over over to This Little Rock
00:08:00
here press F and let's see what
00:08:05
happens boom got the stone got the gold
00:08:09
perfect it's working just the way we'
00:08:11
expect now is there any way we can make
00:08:14
this even simpler yes there is let's go
00:08:17
look at
00:08:20
it so the thing that's bothering me
00:08:23
about this still is that we have two
00:08:25
public methods here and we have to call
00:08:27
them one right after the other what's
00:08:28
the point of this now if you were
00:08:30
building some kind of public API that
00:08:32
other people other programmers would be
00:08:34
consuming you'd probably want to be very
00:08:36
clear with your intent here and you
00:08:37
might want to make a struct and pass
00:08:40
both uh resources back together but this
00:08:43
is also a great opportunity to introduce
00:08:45
the concept of tles to those people who
00:08:48
have never seen this before you can pass
00:08:50
back more than one value as a return
00:08:52
type if you put it into parenthesis so
00:08:54
if I want to pass back both resources I
00:08:57
can do it like this and then and I just
00:08:59
pass them back again in parentheses one
00:09:01
after the other now this isn't very
00:09:03
clear how's the person calling this
00:09:05
method supposed to know what any of
00:09:07
these things are it's kind of tricky
00:09:09
right and this is precisely why people
00:09:11
don't like using tuples in a public API
00:09:14
as if someone else is going to be using
00:09:16
your code well let's see what happens
00:09:18
I'll just wipe all of this out and let's
00:09:20
call The node. Gather method and store
00:09:22
that in a new variable so just wipe this
00:09:24
out I'll say now we'll call it all
00:09:26
resources equals no. gather and now you
00:09:29
can see in the hint here it just says
00:09:31
resource resource that is not very
00:09:33
helpful right but there's more about
00:09:35
tuples we can do why don't we go back
00:09:37
over to our node you can actually give
00:09:40
these a name
00:09:41
here so the name can be anything you
00:09:44
want I'm just going to use the same name
00:09:45
as the member names so let's go back to
00:09:48
the hero and see what that looks like in
00:09:50
the hint now so you can see I've got
00:09:52
both the names there so both of our
00:09:55
resources have been destructured into
00:09:57
this new struct all resource is as
00:09:59
children of that so we can reference
00:10:01
them just with DOT notation now put them
00:10:03
into their own variables or do whatever
00:10:05
we want so that's a little bit more
00:10:07
useful to us why don't we explore this
00:10:10
just a little bit further just to have a
00:10:12
really good understanding of tles as a
00:10:14
return type so if you don't want them to
00:10:17
actually go into a struct and then
00:10:19
reference them by dot notation you could
00:10:21
actually do something like this you can
00:10:23
mimic the return type from the function
00:10:25
and destructure them right into
00:10:27
variables that you want and then you can
00:10:29
go on using them after this now there is
00:10:32
shorthand for this too so we could say
00:10:35
instead of resource here we can just say
00:10:37
VAR um we could say VAR on the first one
00:10:40
and if they're all going to be the same
00:10:43
type you can say VAR at the front and
00:10:45
that would make it even shorter now when
00:10:48
destructuring like this I don't have to
00:10:49
use these specific names there's no
00:10:51
compiler enforcement on this at all I
00:10:53
could say VAR one comma 2 and that would
00:10:56
work perfectly fine but you know for me
00:10:58
person I think I like this way the best
00:11:00
you've got a little struct that you can
00:11:02
use do notation on and do whatever you
00:11:04
want so you know just as a test why
00:11:06
don't we make sure it's working I'll
00:11:08
just add some debug statements here or
00:11:10
rather I'll let co-pilot do it now the
00:11:12
next time we press play we should get
00:11:13
two messages same as before maybe one
00:11:16
more thing before we go I'm just going
00:11:17
to hover over the type of the struct so
00:11:19
you can see what it is here it's
00:11:21
actually a system. value tle of type
00:11:24
resource comma resource so let's leave
00:11:27
tles for now I want to come back just
00:11:29
briefly to The Inheritance versus
00:11:32
composition now the principle says to
00:11:35
favor composition over inheritance but
00:11:38
of course you're going to use both
00:11:40
almost all the time just trying to keep
00:11:42
these tips in mind use inheritance when
00:11:45
there's a clear is a relationship and
00:11:47
the subass is a more specific form of
00:11:50
the super class use composition when
00:11:52
there's a clear ha a relationship then
00:11:55
you can combine your properties and
00:11:56
behaviors into whatever part of your
00:11:59
inheritance hierarchy makes sense don't
00:12:02
be afraid to refactor your code at any
00:12:04
point when you start to sense that
00:12:06
things are going sideways before we wrap
00:12:08
it up for today I want to show you one
00:12:10
more thing that I like to do sometimes
00:12:12
and it might help you out I know a lot
00:12:14
of you are building bigger systems what
00:12:16
I'll sometimes do is come over to chat
00:12:18
GPT and just ask it to build me a plant
00:12:21
uml diagram for all the code that I
00:12:23
paste in and this works great too if
00:12:25
you're just trying to understand someone
00:12:26
else's system now the output is going to
00:12:29
be something that I can copy and paste
00:12:30
into the plantuml website on the
00:12:32
homepage of plant ml.com now you can
00:12:35
just paste it into the box at the bottom
00:12:37
of the page and it's going to Output you
00:12:39
this nice uml diagram so for those of
00:12:41
you that are a little bit more visually
00:12:43
oriented this can be an extremely
00:12:46
helpful tool well that's all I've got
00:12:48
for you today click on one of these
00:12:50
boxes on your screen if you're
00:12:51
interested in more intermediate Advanced
00:12:54
Unity
00:12:55
C

Description:

Architecture Principles apply to Unity Game Dev just like any other Software Development. Let's dive into practical application of one of the most foundational principles in Object Oriented Programming - Favoring Composition over Inheritance (of course, there will be a bit of both) and a few Game Dev tips along the way! The Advanced Dissolve shader you see in the video is at the top of the list of Assets below. In the video it is integrated with two Shader Graph shaders as it's own node for the leaves and the trunk of the trees which required a very easy integration. It uses a geometric shape to hide things between the player and the camera. It is well documented, and on sale right now. Check it out! A few things about Tuples! The word "Tuple" comes from the field of mathematics, particularly from the concept of a tuple in set theory. In mathematics, a tuple is an ordered list of elements. The term has been adopted in computer programming to refer to a data structure that contains multiple fields, usually of varied types, grouped together. "Tuple" is pronounced similar to the word "couple" rather than rhyming with "quadruple," even though the latter shares the same "-uple" suffix. 🔔 Subscribe for more Unity Tutorials https://www.youtube.com/@git-amend ▬ Contents of this video ▬▬▬▬▬▬▬▬▬▬ 0:00 Composition vs Inheritance 8:20 Tuples 12:10 Generating UML Assets Shown In This Video (Affiliate Links) Advanced Dissolve: https://assetstore.unity.com/packages/vfx/shaders/advanced-dissolve-111598?aid=1101lw3sv Aquarius Max Stylized Trees: https://assetstore.unity.com/packages/3d/vegetation/trees/stylized-trees-and-foliage-190601?aid=1101lw3sv Eole Foliage Shader: https://assetstore.unity.com/packages/vfx/shaders/eole-stylized-foliage-shaders-265842?aid=1101lw3sv Dungeon Mason Tiny Hero Duo: (FREE): https://assetstore.unity.com/packages/3d/characters/humanoids/rpg-tiny-hero-duo-pbr-polyart-225148?aid=1101lw3sv Dmitriy Dryzhak Models: https://assetstore.unity.com/publishers/10181?aid=1101lw3sv Chromisu: Handpainted Forest MEGA Pack https://assetstore.unity.com/packages/3d/vegetation/handpainted-forest-mega-pack-248421?aid=1101lw3sv VFX Trees: https://assetstore.unity.com/packages/vfx/particles/environment/stylized-vfx-trees-gpu-based-effect-238647?aid=1101lw3sv Kronnect Beautify: https://assetstore.unity.com/packages/vfx/shaders/fullscreen-camera-effects/beautify-3-advanced-post-processing-233073?aid=1101lw3sv *Follow me!* https://linktr.ee/gitamend

Preparing download options

popular icon
Popular
hd icon
HD video
audio icon
Only sound
total icon
All
* — If the video is playing in a new tab, go to it, then right-click on the video and select "Save video as..."
** — Link intended for online playback in specialized players

Questions about downloading video

mobile menu iconHow can I download "SIMPLE Tip For Better Unity Game Architecture" video?mobile menu icon

  • http://unidownloader.com/ website is the best way to download a video or a separate audio track if you want to do without installing programs and extensions.

  • The UDL Helper extension is a convenient button that is seamlessly integrated into YouTube, Instagram and OK.ru sites for fast content download.

  • UDL Client program (for Windows) is the most powerful solution that supports more than 900 websites, social networks and video hosting sites, as well as any video quality that is available in the source.

  • UDL Lite is a really convenient way to access a website from your mobile device. With its help, you can easily download videos directly to your smartphone.

mobile menu iconWhich format of "SIMPLE Tip For Better Unity Game Architecture" video should I choose?mobile menu icon

  • The best quality formats are FullHD (1080p), 2K (1440p), 4K (2160p) and 8K (4320p). The higher the resolution of your screen, the higher the video quality should be. However, there are other factors to consider: download speed, amount of free space, and device performance during playback.

mobile menu iconWhy does my computer freeze when loading a "SIMPLE Tip For Better Unity Game Architecture" video?mobile menu icon

  • The browser/computer should not freeze completely! If this happens, please report it with a link to the video. Sometimes videos cannot be downloaded directly in a suitable format, so we have added the ability to convert the file to the desired format. In some cases, this process may actively use computer resources.

mobile menu iconHow can I download "SIMPLE Tip For Better Unity Game Architecture" video to my phone?mobile menu icon

  • You can download a video to your smartphone using the website or the PWA application UDL Lite. It is also possible to send a download link via QR code using the UDL Helper extension.

mobile menu iconHow can I download an audio track (music) to MP3 "SIMPLE Tip For Better Unity Game Architecture"?mobile menu icon

  • The most convenient way is to use the UDL Client program, which supports converting video to MP3 format. In some cases, MP3 can also be downloaded through the UDL Helper extension.

mobile menu iconHow can I save a frame from a video "SIMPLE Tip For Better Unity Game Architecture"?mobile menu icon

  • This feature is available in the UDL Helper extension. Make sure that "Show the video snapshot button" is checked in the settings. A camera icon should appear in the lower right corner of the player to the left of the "Settings" icon. When you click on it, the current frame from the video will be saved to your computer in JPEG format.

mobile menu iconWhat's the price of all this stuff?mobile menu icon

  • It costs nothing. Our services are absolutely free for all users. There are no PRO subscriptions, no restrictions on the number or maximum length of downloaded videos.