[Introduction to Unity] 21. Prefab

[Getting Started with Unity] Prefabs

    Hello everyone, I am Lampard~~

    Welcome to the Unity Getting Started series of blogs. The knowledge I have learned comes from Teacher Afa at Station B~Thanks 

 

(1) Prefabricated body production

(1) What is a prefab

    In this chapter of the blog, we will learn the concept of a prefab. What is a prefab?

    Unity's prefabs are reusable game object compositions, a mechanism for combining game objects into a whole and saving them as a single asset

    By combining one or more game objects into a prefab, we can quickly create multiple game objects based on the same properties

    To put it simply, we can quickly create a game object in the compiler through the FBX model file

    But this function is not convenient enough, because it only brings the mesh, material, and texture information of an object. There are no other components and attribute information on this model

    For example, the car we created before is equipped with AudioSource's car music and script components that control the movement of the car. If we want to package all the information in the object object (it can be copied directly for next use), we need to use the concept of prefab

(2) Make a prefab

    How to make a prefab? The method is very simple, we only need to drag the game object on the Hierarchy panel directly to the Assert directory

    For example, if we make the car into a prefab, first create a prefab folder in the Assert directory, and then drag it over.

    Our previous Earth-Moon system can also be made into a prefab

(3) The composition of the prefabricated body

    After exporting as a prefab, we can right-click and open in the folder to see the files with the Prehab suffix, as well as the .meta file  

    I said before that .meta is just the log file of unity, so we don’t care about it. We mainly look at what the prehab file is. Open the right file and find that it contains some component information contained in the prefab, as well as some parameter information, etc.

    In other words, the prehab file is just a record file and does not contain the resources owned by the object. We can right-click to export this prefab, and unity will remind you to package the resources used by this prefab

 

(2) Use of prefabricated bodies

(1) Create game objects using prefabs

    Using prefabs to create objects is the same as before, just drag it to the game scene

    For example, we now use the Earth-Moon system prefab to create 4 identical game objects

    Click to run and they will spin at the same speed 

(2) Modify the prefab

    Another advantage of the prefab is that if we need to modify the information of the prefab, such as changing a skin or modifying the value of a parameter, we only need to modify the prefab , and all the objects generated by it will be modified synchronously

    So how to operate the prefab? We can double-click the prefab itself, and the entire interface will turn blue

    Or in the Hierarchy layer manager, select the object generated by the prefab, click the small arrow to enter the editing interface, but when entering from here, the interface will turn gray by default

     We can modify the rotation speed of the earth to try it out. It was 30 before, but now we change it to 360. Haha, we can see that the earth is rotating very magically.

(3) Break away from prefabricated body relationship

    Then I don't want to be affected by the prefab, can I? Although they were all born by the same mother, I just want to be different, and I don’t want to be affected by the original prefabricated body

     Yes, we only need to right-click the object we want to touch, and select the unpack option under the prehab menu . After contact, it will not be affected by the original prefab.

    It can be seen that the object after unpacking, even if the speed of the prefab is modified again, it still runs with the original properties

 

Alright, that's all for today, thank you all for reading! ! !
Like and follow! ! !

 

Guess you like

Origin blog.csdn.net/cooclc/article/details/130441740
21.