Unity Prefabs Explained: Nested Prefabs and Prefab Variants

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
prefab is one of the most used feature in unity in 2018 Unity made a huge update on prefabs and they became even more useful before showing you the use cases of prefabs in the unity we better understand the prefab concept as the first step to do that I want to give you a real life example say we are running a clothing company and the factory is producing thousands of t-shirts we need a base model for this production right and all of the shirts will be created with respect to this pattern the base model is the prefab the pattern that we are going to create instances from it that means once you have the prefab you can create an instance when you need one so you don't need to adjust properties and settings again and the best part we can still make change after they are created so let's say all of these shirts are in medium size which was a mistake because they should be large well there is nothing to do in real life but in unity you can just select this prefab make it bigger and all of the instances also affect it with this change which is really really time of shoot if I was going to build the Mario game from scratch today everything you see on the screen would be prefab the coins are prefab to use our prefab clouds are prefab everything in this game is used multiple times so we will use prefabs all the time now let's see how prefabs looks like and how we can create one in unity actually we can create a prefab for any kind of game object let's start simple go click plus button 3D object and select Coupe so that we can create a prefab from the scoop all right now I have the cube in my scene and this is just a normal game object right now but if I move this Cube to the assets folder here and you can drop here in any folder you want so that when you store the object you can see that the icon next to the object changed also the text becomes blue and all that means is that now this object is a prefab so it is that easy to create a prefab but normally people don't store prefabs here instead create a new folder called prefabs and store all of the prefabs inside of this folder for clarity and from now on we can create and store our prefabs in this folder okay now this is my prefab and here is an instance of it in the game you can delete this if you want since we already have the prefab and let's assume that we continue to develop the game until we want to use this prefab and at that point we can create as many objects as we want using the prefab I'm going to create three cubes and let me put them next to each other you can see all of them together in the game type on the right okay now after you create these using the prefab let's say you want to change the color or the size of the objects we've set off changing one by one you can double click on the prefab and that brings us into the prefab mult inside of the prefab mode everything you do will be applied to all of the objects that are using this profile let's see this with an example change the size for example I'm gonna make it larger and as you can see when I edit in the prefab mode all of the objects in the game tab instantly affect it now I want to add a new folder in the assets say materials and inside of it create a new material name this one blue and change the color to Blue from here okay now we have a blue material and open the perfect mode so that we can make the prefab blue using the new material and as you can see again all of the objects that inherit from this prefab becomes blue okay you got the point this saves lots of time when you work with bigger games next I want to talk about disabling the prefab relation for example we don't want this Cube at the very right to be affected by the prefab anymore then you can right click on the object say prefab unpack completely you can also see the icon is changed and it is now a normal game object that is independent from the prefab after that when I come to the prefab here and change something you can see the third Cube at the right is not affected because we just unpacked that one all right now I want to create a new material and say red this time okay the cube on the right is already not connected to the prefab and the others are select the middle one and I want to change the material of it individually if I drag the red material on top of it it is now red so you can see that it doesn't have to be exactly the same with the other instance when I do this you can see a tiny blue line next to the object in the hierarchy menu and that means this object overrides the prefab if you select object and go into the prefab line and click overwrites here you can see the overwrite inside of the mesh renderer we can understand the difference which is the color of the object if you check the same field in the first tube so we didn't overwrite this one it says no overrides here and that's how you can understand which objects overrides the prefabs ok now remember this Cube here overrides and maybe I want to open the prefabs folder and create a new prefab with this Neve object so you can again drag and drop this object inside of the prefabs folder but this time Unity prompts two options to go with which is really important decision and basically here Unity is like should I create a brand new original prefab or should I create variant of the previous prefab and that's because Unity is aware that this object is connected with the first prefab if you select original prefab then you will cut all of the relations so that no updates on the first prefab will no longer affect this object let's select and see now open the first prefab and change the size for instance as you can see only the first object is updated we already unpacked the third one and the second object is now related to the knee prefab which is Cube one so you cannot change it here if you want to edit this object you can again edit the new prefab just like this these two prefabs are totally same in terms of hierarchy so that means you can create new object from the first prefab and also create new objects from the second one as well now we don't need these objects so delete them and I want to show prefab variants select the cube that is still connected to the first prefab and duplicate this one so that we have four cubes in total I want to create a new material for the nevq and it can be yellow of this time change the color from here and assign this to the new object now let's try to create a new prefab with that object and this time I'm going to say prefab variant first of all as you can understand from the name this is a variant of the previous prefab and it still inherits from the first prefab what does it mean well if I come here and change the size of the first prefab the yellow object also change as you can see and that's because the size is not the characteristic feature of that variant this variant overwrites only the color property so that means this variant will inherit all of the chains except color because that's where it differs for example here we can assign red to this prefab but the yellow object stays yellow since it is the unique feature of that object but again you can change everything else lastly I want to explain the nested prefabs which is basically a prefab inside of a prefab here is a good example this house can be a prefab by itself but inside of it the door is another prefab because the store can also be used in different houses so we can nest prefabs alright let's see how we can do it in unity here I have cleaned the project and I want to show you another way of creating prefabs as well so you don't necessarily create it in the game scene you can come here in prefabs folder right click and create prefab I'm gonna name it door double click to go into prefab mode and inside let's create a 3D object again let me adjust the position for it to look like a door and I also want to create a sphere object in order to make the door handle adjust the size and position it on top of the door okay now I want to change the color of them and we already have the materials maybe this can be yellow and this can be red all right we are done with the door go back to prefabs folder and let's create the house this time this is also a prefab so right click create and prefab for Simplicity use another Cube object one more time and this is our great house this can be blue okay now if you move the door prefab inside of this Cube here you can see that it becomes a child object of the queue this is how you can Nest prefabs again let me adjust the position and the size and this may take a while and yeah now we have the house prefab which includes the door prefab inside of it and we can import it into the project so that you can see the house in the game tab as well but the best part is we can only edit the door by going into door prefab and the house is also changing as we added the prefab and you can follow it from the game tab here all right guys with that you now know the fundamentals of prefabs in unity I'm ready to use it in your projects [Music]
Info
Channel: Yeti Learn
Views: 6,680
Rating: undefined out of 5
Keywords:
Id: PSKKQKr2bnA
Channel Id: undefined
Length: 11min 35sec (695 seconds)
Published: Wed Feb 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.