Unity3d the use of the three prefab components of learning notes

Source: Internet
Author: User
For this tutorial, let's create a simple prefab component. Tutorial refer to the translation tutorial from the People's Material translation group, the basic Getting Started video tutorial of Unity game engine.
Speaking of prefab, the Chinese translation is a preset, and in Unity's official book "Unity4.x from Beginner to Mastery," the explanation is that it can be understood as a collection of game objects and their components, in order to make the game objects and resources can be reused.      The same objects can be created from a single preset, and this process can be understood as instantiation. When stored in the project file (Project view) state, the preset body acts as a resource and can be applied to different scenarios in a project. When you drag a preset to the scene, an instance is created. When the instance is associated with its original preset, changes are made to the preset, and the instance is modified synchronously.      Prefab can improve the utilization of resources and the efficiency of development. Like a prefab is a standard room model we create, when we need to build another room in the scene, we do not need to manually put the floor, wall, ceiling splicing, but you can drag a prefab to the scene view directly, And make changes to this prefab.
Below we start to create our prefab component. After a tutorial, there are three models in our scene that are not matched to each other. As shown below: Next we will click to select the three parts, and in the Inspector view, find the position, change their XYZ coordinates to 0,0,0, we'll find that the model will become the following figure.
We found the Mineshaft_wall center in the middle of the wall, so we're going to modify his y-coordinate.      Select the wall model and change the y-coordinate of the Mineshaft_wall wall model to 3, in fact the model has a wall height of 6 units. Note here that if you want to move a model in one direction, that is to keep the other two coordinate values changing only one coordinate value, then you have to hold down one of the coordinate system three directions and drag it.
What we're going to do next is put the wall now in the middle, move it to one side, and copy a few walls to move to four weeks.       Dragging or modifying coordinates directly can produce various errors, where we use vertex snapping (Vertex snapping). We first select the object we want to move, and then press V, and we'll see that when we move the mouse, the axes stop at the vertices in the model. As shown below, when you move the mouse to the lower-left corner, the axes appear at the lower-left point. At this point we can click the mouse and select the square area at the vertex axis (as in the small yellow square in the picture), and drag the model to combine the vertex with the other vertex that we moved the mouse to below. As shown in the following figure, when I select the vertex, drag it to the top left corner of the floor to make the two vertices coincide. This is a quick way to move the model.
Next we will use this wall to copy out another wall and move the position. We select the wall we just moved, press Ctrl+d (Mac for command+d) to copy, we will find a mineshaft_wall in hierarchy view, and then we take the same method, press V, Select the lower-left axis of the wall and drag it to the bottom-right corner of the floor, and the wall that we just copied is opposite the existing wall. But as we mentioned earlier, our walls are not visible from the front, and after the shift we see the other side of the wall, so there is only one coordinate system and no walls. If we move a little bit to the right, we'll see the wall we copied. The following figure:
Fig.: Non-moving angle of view figure II: Moving angle of view

All we have to do is rotate the wall 180 degrees. Select this wall, press E, activate the Rotate tool, and the axis will change as follows:

Here we hold the Green line to make sure the y-axis angle is inconvenient, then hold down CTRL (command on MAC) and drag counterclockwise, holding down CTRL is rotated in 15 degrees, and after rotation we will find the Inspector View, The y angle becomes 180 (sometimes 179.99), and of course you can directly modify the angle directly in the Inspector view amount rotation, to make sure that the axes you have modified are correct. Press W to return to the original coordinate system when the rotation is complete.
Now let's start putting this model into a prefab component. In Project view, create a new prefabs folder within the Importedassets folder, specifically for placing our presets. Then go to the Prefabs folder, right-click, Create,prefab, and name the newly created empty Prefab as pfb_straight.
Below we move the line of sight to the hierarchy view, we can see four blue names, the blue name indicates that the object is a preset or dragged from the file into the scene. Now to put the object into the prefab, we can find that when all of these objects are selected, they cannot be dragged into our new prefab. Here we need to create an empty game object in hierarchy------menu bar, gameobject,create empty, so hierarchy will be more than an empty gameobject. We named it pfb_straight, yes, and our prefab name corresponds, and now the name of the object is white. Let's create a parent-child relationship for this object with our four objects and import it into prefab.
Select all four objects, then drag into the Pfb_straight, will form a parent-child relationship like figure one, next select Pfb_straight, and drag into the project view in our new prefab, completed the initial creation of prefab, as shown in Figure Ii. In Figure two, the objects in hierarchy all turn blue, and our prefab has a preview in the lower right corner of the interface, we can drag in the preview to see the contents of prefab.

Figure One:
Figure II:


At this point, a very simple prefab is created, and below we drag this component into the scene, we will find that we can already make the components we created earlier very simple if the scene. This is the advantage of prefab, greatly improving the efficiency of the work.
We can now select a component, press and hold V, and use the previously mentioned vertex snaps (Vertex snapping) to build a long tunnel. Note To select the entire component, not part of the component, do not just move the floor or the wall, and if the move is wrong, press CTRL +z to cancel the operation. You can try to create a tunnel.


2015.1.1 0:03 by Mr.losers


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.