Unity3D study Note 3 Prefab component usage,

Source: Internet
Author: User

Unity3D study Note 3 Prefab component usage,
In this tutorial, we create a simple Prefab component. For more information about this tutorial, see the "Unity game engine basic getting started video tutorial".
Speaking of Prefab, the Chinese translation is the preset body. The explanation in the official Unity book "Unity4.X from entry to mastery" is: it can be understood as a collection of Game objects and their components, the objective is to make the game objects and resources reusable. The same object can be created through a preset body. This process can be understood as instantiation. When the status is stored in a Project file (Project view), the preset body acts as a resource and can be applied to different scenarios in a Project. An instance is created when you drag the preset to the scene. If the instance is associated with its original preset body, the preset body is changed and the instance is modified synchronously. Prefab can improve resource utilization and development efficiency. For example, a Prefab is a standard room model we have created. When we need to create another room in the scene, we do not need to manually splice floors, walls, and ceilings, instead, you can drag a Prefab directly to the Scene view and modify the Prefab.
Create the Prefab component. In the previous tutorial, there are three models that do not match in our scenario. For example, next we will select these three parts by clicking them, locate Position in the Inspector view, and change their XYZ coordinates to 0, 0, we will find that the model will become like this.

We found that the MineShaft_Wall center is in the middle of the wall, so we need to modify its Y coordinate. Select the wall model and change the Y coordinate of the MineShaft_Wall wall model to 3. In fact, the wall height of this model is 6 units. Note that if you want to move a model in one direction, that is, to keep the other two coordinate values unchanged, only one coordinate value is changed, then you need to hold one of the three directions of the coordinate system and drag it.
Next, we need to move the current middle wall to one side, and copy several walls to four sides. Directly dragging or modifying coordinates may produce various errors. Here we use Vertex Snapping ). We first select the object we want to move, and then press V, we will find that when we move the mouse, the axis will only stay in the vertex of the model. For example, when you move the mouse to the lower left corner, the coordinate axis appears at the lower left point. In this case, you can click the mouse to select the square area (a small yellow square in the middle) at the coordinate axis of the vertex, and drag the model to combine the vertex with another vertex we move the mouse below. For example, after I select This vertex, drag it to the vertex in the lower left corner of the floor to make the two vertices match. This is a quick way to move the model.

Next we will use this wall to copy the other wall and move it. Select the wall we just moved and press Ctrl + D (Command + D in Mac) to copy it. We will find a MineShaft_Wall added in the Hierarchy view, then we adopt the same method just now. Press V, select the coordinate axis in the lower left corner of the wall, drag it to the vertex in the lower right corner of the floor, and the wall we copied just now will be opposite to the existing wall. But as we mentioned earlier, our wall cannot be seen from the front. After translation, we can see the other side of the wall, so there is only one coordinate system and no wall. If we move a little closer to the right, we will see the copied wall. For example:
Figure 1: No moving angle of view Figure 2: Moving Angle of View

What we need to do is to rotate the wall 180 degrees. Select the wall and press E to activate the Rotate tool. The axis changes as follows:

Here, we press and hold the Green Line to ensure that the Y axis angle is inconvenient, and then press CTRL (hold down Command under Mac) to keep dragging in the clockwise direction. Press and hold CTRL to rotate in 15 degrees, after Rotation, we will find that the Y angle in the Inspector view is changed to-180 (sometimes 179.99). Of course, you can also directly modify the angle in the Inspector view's Rotation, make sure that the axis you modified is correct. After rotation, return to the original coordinate system by W.
Now let's move this model into a Prefab component. First, create a Prefabs folder in the ImportedAssets folder in the Project view, which is used to place our preset objects. Go to the Prefabs folder, right-click, Create, and Prefab, and name the new empty Prefab PFB_Straight.
Next we will move the line of sight to the Hierarchy view. We can see four blue names, which indicate that the object is a preset body or dragged into the scene from the file. Now we need to put the objects in the Prefab. We can find that when all 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 that there will be an Empty GameObject in Hierarchy. We name it PFB_Straight. That's right. It corresponds to our Prefab name, and the name of this object is white now. Next, we will establish a parent-child relationship between the object and our four objects and import the object to Prefab.
Select all four objects and drag them to PFB_Straight to form a parent-child relationship. Then, select PFB_Straight and drag it to the Prefab created in the Project view, the initial creation of Prefab is complete. In Figure 2, all objects in Hierarchy are blue, and our Prefab has a preview in the lower right corner of the interface. We can drag the mouse in the preview to view the content of the Prefab.

Figure 1:
Figure 2:


So far, a very simple Prefab has been created. Next we will drag this component into the scene, and we will find that we can simply put the components we created earlier in the scenario. This is the advantage of Prefab, greatly improving the work efficiency.
Now we can select a component, hold down V, and create a long tunnel using Vertex Snapping. Be sure to select the entire component instead of a part of the component. Do not move the floor or wall only. If the movement is incorrect, press CTRL (Command) + Z to cancel the operation. You can try to create a tunnel.


2014.1.1 0: 03By 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.