EVERYTHING You Need To Know About UNITY PREFABS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so let's talk about prefabs which is maybe one of the most important unity fundamentals and concepts that there is so you better listen to this video it's very important but no worries it's not too complicated so what are prefabs prefabs help you to reuse pre-configured game objects so what does that mean let me show you instead of just talking about it let's assume here now we have our little stage over here and let me just get rid of these objects over here and now we have let me just take those and call them stage for a second so we can differentiate and now we want to create a cube and this one is our let's just call it prefab instance you will know in a second what that means and there we go so right now it's a simple normal game object just a cube nothing special has its um components really um yeah boring and now let's assume that we do some fancy stuff with this cube and fancy stuff basically would mean we have some materials on this object for example let's make it red and imagine in a real project this would not be just a red color maybe it would be like a lot of stuff we add components to it maybe our jump component over here and then we do changes and we have like our nestings our parents and stuff like this all of these complicated stuff and now we have our final object which could be our enemy or a game it could be any scene object it could be a tree literally any kind of game object that is um configured by us and all this thing is pretty cool and we want to use multiple of those again like a tree would be a pretty good example because you would most likely have a lot of trees in your environment so you don't want to build another tree like go 3d object cube or tree or take your tree and drag it in and then do all these settings basically you want to have like one blueprint which is a term i like to use blueprint or template and then out of this template you generate all of these instances so how do we create these um prefabs very simple you go you take any object it doesn't matter which one of those over here from the hierarchy and drag them into your assets there we go they become blue and here you have your little prefab and the cool thing is this thing is now a file on your disk like anything else in this folder over here so you go over here prefab and you can actually just go and send it to someone and they will have as well you just have to make sure that the script and stuff like this are bundled together but that's a different topic but generally you can just reuse this prefab now also in different projects but now we are talking about this project and how to use it well you take it and drag it back to your scene and now you see you have multiple of these instances so you can just drag them into the scene or you can also drag them into the hierarchy just like this and then move them around and the third way which i think nobody ever teaches but it's technically possible you can also just duplicate an existing prefix and that will also make an instance out of it so now we have a bunch of red cubes basically but now okay what's the difference between these cubes now and if we just um go right click cube and take these one the normal cubes like we can just duplicate them and also use them like this as well like to pick it again and now we have a little road or a block where you can jump over could be anything so what is the difference between these prefabs now the instances and the normal ones let's figure out let's say you want to change the snow to every single object maybe you have a tree and you want to change the color of the tree or you want to make the tree a little bit bigger all of them so the casual way would be or the first initial way if you don't have this prefab option as we take this one make it bigger and take this one make it bigger and then we have to make sure it's the same size and this one and this one and then we go and make them green just like this okay and now imagine we don't want to do it for four but we want to do it for like 400 um objects anything really that would be kind of annoying right i know so unity thought of this and they came up with the way of yeah creating prefabs which she can edit and these changes inside of the master game object or prefab whatever you want to call it which is the asset over here will be applied to all instances so if you change an instance over here that will just remain so if i make it bigger i just have a bigger object basically and nothing special and it doesn't apply the others like sometimes i just don't want it i want to have my own object i want to do some this tree is a little bit different than this one maybe and stuff like this so of course these changes are not always linked together but if you want to do a change on every object which is quite common actually let's say you have an enemy and this enemy shoots at you and it has like a half points of 100 and it does like 20 damage and then you want um you see the enemies a little bit too strong and then you don't want to go to like 3 000 enemies and make them weaker you go to the one prefab instance oh sorry you go to the one prefab so the template over here and then you change it from here so instead of changing all of these to the new color for example so let's just say materials and let me just take this one by the way quick tip you can lock the inspector so with this unlock over here and then now it doesn't change the view so it always stays on the prefix as we can see here and now we go to materials and then we just yeah let's make it green as well chip there we go and now all of these turn green because we have changed the prefab itself not the instances we have changed the template so all the previous being applied and this of course does not only apply to color it's anything like i can remove the box collider now this is also still in the prefab over here let me just um unlock it and now if we check out all our instances all of them they don't have a collider anymore so this is one way in changing the um preset itself to be applied to all the others there are two other ways that i want to show you now and basically yeah they are all they all do the same it's just personal preference where you want to change it the first one is you change it in the s directly the second one is you can actually double click this prefab now you are in the prefab mode which basically is an isolated view of this prefab itself and here you can do some fancy density changes i don't know let's go and make it a blue this time and add some cool components to it so we make it blue and we see all of them are turning blue this is by the autosave over here so we can also disable the autosave and then they the changes are not being applied immediately so they make it like green again they stay blue of course and have to manually save it but again i most likely i always have it all set on because if i want to change it then yeah just saves me one step basically and in here of course you can do and all the other things as well i don't know you can if you decide to have a different mesh you want to have a capsule instead of a cube for example it's a cylinder now you want to change the form of your object so you want to add components all of these things is something that you can do also in the prefab mode let's go back again and now we have a bunch of cylinders the third way in manipulating your prefab is you can do it directly in the instance actually so let's just take any instance it doesn't matter which one okay now we have this one over here and we want to make it a small cylinder because we want to stack them up i don't know we want to do something cool with them so we change the size of it maybe we add the collider again but not a box color maybe just we just take a box collider and now it's a smaller cylinder with a box collider but now we have seen okay yeah maybe i would like to have it on all of the objects which basically means i want to apply it to our prefab uh we have this nice little window here over right which one which tells us all the changes that we have done to this specific um instance so if i change the let me see what else can i change the rotation i rotate it a little bit like 45 degrees or something like this doesn't matter and now we see okay the overwrites it shows me the transform actually we have changed everything already the position rotation and scale and this is something that you can look up here we can also compare it to the original source and there we see okay the position has changed and the yeah and basically everything has changed over here um and we also see the others so here we see okay edit so previously there was no collateral before because we removed it and now we edit it let's just assume okay we decided we want to go with the sphere because it's smoother i don't know then we can also look it up again we see okay we change the mesh filter from cylinder to a sphere and now we are like okay looks good the cool thing is and this is really really powerful if you think about it like all of these changes it's extremely time-saving and it's extremely efficient in doing these kind of changes like you would get insane if you would have to do this for like 2 000 elements like even to do it with like 10 elements would be way too much already so this is really cool and now we can apply all these changes individually like by going to these specific things and then apply apply to all and now everything is um on this position and the scale but they are not um a sphere yet because we didn't apply it as well you can also apply it individually again like i showed before or you can just apply all chip there we go and now i think it looks this looks pretty cool for a small small game kind of looks like m ms let's just make it a big bigger and this now and now we can apply it again okay this doesn't look like m m's anymore but never mind but now you see okay there are really cool ways and how we can use them now we can actually get rid of all of them there we go and we still have our prefab over here and since we applied the changes we can just drag all of our m ms over here and um one thing that you may not realize right now the best i think this is by far the best part we can control all of these things via code so we can spawn like we can instantiate v things not by just dragging it into the scene which is cool already but we can in runtime while the game is running let's say we have like bullets that we are shooting like these are bullets from a spaceship and when we hit space we want to shoot these bullets we can actually take these um prefabs as a field so we can drag them into our component for example let's say the jump on space would require a game object to be shooting or something like this then we can dynamically create these prefabs while our game is running and this is extremely powerful and this is really the core of working with unity pretty much everything is running with prefabs that you want to your use of course like if you have like one single main camera that you're never gonna um duplicate or the direction light or stuff like this then you don't have to do a preview part of this so not like literally any object but generally when you see you have to do something um a repeated number of times then it makes just sense to use prefabs for this let's now talk about nested prefabs and as you may think you can have a prefab and set a prefab instead of prefab and so on but let's keep it simple let's say we have this prefab and we want to put a prefab inside like we talked already about these um parentings basically so now like these cubes are together if i move the parent the others are moving with it just put them over here so we have clear vision always important and instead of having regular game objects inside of this we can also put another or any other prefab here as well so let's say we want to build a car a very basic car so first of all let me just unpair these again and we take this thing over here over here we rotate it 90 degrees is it 90 yes it is and this is our car body i know very simple we are minimalists here um car body we make a prefab out of it there we go so now this thing is a prefab and now we take our little m ms and we put them inside of the car body so we can just take our prefab instance and put it inside here and now this is an acid prefab and if you are um so this is the nested prefab now and now again as you may expect if i move the car body the other thing moves with it so let's kind of assign it now that it makes more sense let's just make it smaller and maybe like this again it's not we are not competing for a most beautiful car price but there we go and now i want to have four more of these so just ctrl d to duplicate there we go and two more i think this car looks pretty beautiful to be honest please let me know in the comments i would like to hear your thoughts there we go i'm already getting a becoming a 3d artist here cool so now you may think okay this is all cool but what does it help me actually to achieve let's actually get rid of the others so we are not being distracted by anything here we have our little car very cool we can move it around and now we may think okay i don't like these tires over here they kind of look ugly or i want to do something else with them and then we just go here to one of these and we can already apply it because now these instances they have not been applied to the main prefix so if i take another one it's still the old one the very big one for example because we did not apply it yet so if we go in any of these overwrite apply all now it's good we see a little plus over here so let me talk in this show in a second what that means first of all let's just um double check okay this is a small um thing again so it's fine and now what we can do is we can add these to our car body because now if i take another car body which is our original prefab it does not have the wheels and does not have the wheels because we didn't apply it to the outer level prefab as well so therefore we can go to overwrites and apply all we can also technically go to this one here double click and then take these in for example and then size them move them stuff like this but again since we have done it already let me just go back oh no not the other save now we don't want it prefab instance we see we don't want to change this card because now since we have built it here already it's easier to just go here check the overrides we see okay one two three four and apply and now if i take a car body i cannot remove it as well because all the changes are saved if i take a car body and i want to oh it has one too much i don't know if it's over there i don't where i don't know where this happens but i have to open it now because if you want to change it you have to open it again if you want to remove parts of the nester prefabs now we can get rid of this one over there so just delete it we can save it now go back and now we have our nice little car body and now the coolest part is if we take our reel and we make it i don't know we make it red or something again or we change the size but i think the size is fine now so let's just make it materials and let's make it red whereas the material selection over there there we go and now of course as expected it will apply to all of these and this is really helpful because now again like you imagine you have like a car racing game with like multiple of those and if that's it's not nested it will take you all of these single wheels for every single time and then imagine it's not wheels it's something else like like a leaf on a tree because that will be very annoying to change individually so there you see the benefit on why we should use these as well let me get rid of the other two there we go and this one as well so of course you can now have a prefab inside of this prefab again like you can dig very deep but i don't know i would not recommend it too much because most of the time it's not needed to have like these deep nestings and your project will just look like this let me show you my god you see you will end up in an infinite loop kind of like inception feelings let me just go back so you can of course now i don't know maybe a logo or something out of this in here as well you can get very creative basically but i think this one should really show you all the basics for working with them prefabs in general and nested prefabs so there's one last thing or a couple small last things that i want to talk about and then you're already done there's also a way to unprefab your prefabs so basically make them to normal game objects again and this is something that you can achieve by right clicking and then you can go to prefab and then unpack or unpack completely the difference between unpack and uncompact unpack completely is oh let's just do it and again like this is he thinks like how you learn best um just do it and then see what happens so if we go unpack okay if this thing turns um white again so this thing is not a prefab anymore let's hit ctrl z a couple times now it's a brief again and now prefab i guess you may expect i guess you may know what is happening now but completely all of these are normal object again and this can be helpful when you don't want this particular instance to be affected by your template or by your prefab in general so for example if i make this thing let's open it up and now i give it a fifth wheel in the middle of the top for whatever reason there we go but you don't want this on the other object now we see okay it's not being applied to this one because this is not a prefab anymore this is an individual game object can be helpful i haven't used it too often to be honest so it's kind of like personal preference but now you know one last thing that i want to talk about is called prefab variants so therefore let's actually um yeah we have a clean stage again and it's also give that stage a color so it's not like killing my eyes anymore and yours maybe as well let's just make it a bit gray so just like this and now our eyes are safe and now we can go to create an object again kind of any could be anything a sphere and let's just make a regular prefab by the way small thing i would um i mean if you do the other courses then you will know immediately but i would recommend you to always have a prefabs folder and there you can store all of your prefabs because then it's just them everything is clean and tidy sphere you just drag it in for a regular prefab basically but now let's just say that you have something else that is different but similar enough to be considered a prefab variant so it should still have a link to its um origin so to the sphere over here let me just call it sphere master sphere for example and now if i want to create a variant i take the same um prefab so an instance of it so if i would remove it i take it here this now is an instance of this um prefab and i take it into the same folder again so prefix unity will ask you do we want to create a complete news prefab which is basically it's completely separated there's no link it's just a random new prefab nothing fancy or a variant and a variant there we go and now we have our master sphere variant let's just call it sphere and there we go so now we have these two we have this one as you can see it looks a little bit different if you compare to a regular object this is the regular um prefab this is the variant and now the cool thing is i can do my changes to the variant independent of the master prefab but still apply global changes and this may sound like what the hell is the guy talking about but let me show you so let's say our first of all i would not call this master sphere anymore let's say i decide to change the color to red again red and very cool and i have like a bunch of normal spheres so again these could be like enemies this could be other objects could be a tree could be anything let's but for the sake of enemies now let's say we have these um human enemies and this is still an enemy so it still falls under the category it is an enemy but it is a animal a wild animal that attacks you for whatever reason so it's different you know and now what we can do and this is red and now i can take my human animal human enemies make them blue because humans are apparently blue let's see so i can take it and again i can change it in the individual instance or i can also change it in the master instance it doesn't really matter i personally just do it in one of these and then i apply the changes but again this is really just personal preference apply all and now you see we applied the changes to to the color and applied it to all but our animal did not get changed because we have an override and this is important like we changed the color already so it's not the default value if we would now for example go to our sphere again like this normal basically thing and for some reason we increased the radius because they now can see better whatever and now if i apply this it will also apply to our variant let me show you there we go it is applying to the variant because we did not change or we did not change anything in our sphere if i now for example change it let's say i make it super big because animals obviously they have like super range so now this is really big and of course if i change it here it does not apply the others because it's still the variant it's only its own prefab so if i take another variant of course this guy will have a huge collider but our others are still the same and now the interesting part because i've overwritten so we can check it by overwrites okay since i have applied it already it doesn't show it anymore but let me just make it big again even bigger it shows the override over here and also it's bold so if you see a blue thing over here and it's bold then it tells you that there has been some override basically and if i now go back to this again and i change the radius maybe i make it smaller like this go override apply of course it will change the others but it will not touch our huge variant so think of it like something when you don't touch it in the variant um the master changes will be applied to it but all the things that you are making different on this object are staying unique so maybe our variant also has a an extra component for example that the others don't but the others have another component maybe animal sense animal sense let's go whoops my ipad almost uh broke never mind so it takes a minute to load up everything we see this is a little plus so it shows us that it has been added again like if i now take a new variant it will not show the changes unless i apply them so overwrite apply play all and there we go uh okay yeah that was a a lot of stuff and um this is something that will come second nature to you and the nesting and the variants you don't use them like all the time but generally you want to get used to prefabs very fast whenever you build things that could be reused you can just think of doing them as a prefab also when you work with other people you are working with prefabs most of the time when you work with scripts you can drag in prefabs you will see this a lot in the upcoming unity and the upcoming vr and ar course and you will see a lot of usages for um yeah prefabs basically so don't worry if you didn't get everything it's not um yeah you don't you're not supposed to understand everything right away but if you did even better but most important play around do some testings do assumptions and if you do the other courses these things or more games and apps these things will just come second nature to you thank you so much for listening and yeah as always if you have any questions reach out to our discord server ask us over there that's the fastest way for you to get a response and as always thank you so much for listening and i see you in the next video
Info
Channel: Dinesh Punni
Views: 325
Rating: undefined out of 5
Keywords: learn unity for beginners, unity for beginners, unity for beginners 2021, unity for beginners tutorial, learn unity, learn unity 2021, learn unity engine, learn unity full course, unity tutorial for beginners, unity beginner tutorial, unity beginner tutorial 2021, unity tutorial, unity tutorial 2021, free unity course, dinesh, unity prefabs, unity prefab variants, unity prefabs tutorial, prefabs unity, nested prefabs unity, unity nested prefabs, unity prefabs explained
Id: AcMS_BBGttA
Channel Id: undefined
Length: 30min 48sec (1848 seconds)
Published: Tue Oct 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.