[Unity] 3.2 uses presets to create reusable components, unityprefab
Classification: Unity, C #, VS2015
Created on: 1. Introduction
Prefab is a special resource stored in the Project View. It is a reusable game object (GameObject).
If there are multiple pre-fabricated objects (Prefab) in the Project, you can save all the pre-made objects to the Prefabs sub-folder of the Assets folder in the Project view for easy search.
All the pre-made bodies in the Prefabs sub-folder can be put into multiple scenarios, and can be used multiple times even in the same scenario.
Adding a premade body to a scenario is an instance for creating the premade body. In addition, because instances of all prefabricated bodies in the scenario (Scene) are automatically linked to the original prefabricated bodies (Prefab) in the Project, these instances are essentially clones of the original prefabricated bodies. Or, no matter how many pre-fabricated instances are in the scenario, you only need to make any changes to a Prefab in the Project, and you will see that these changes can be applied to all instances of the Prefab in the scenario immediately.
1. Create a Prefab in the Project View)
To create a Prefab in a Project, drag and drop a game object (GameObject) you created in the scenario (Scene) to the Project View) in a Prefab.
If the name of the game object (GameObject) in Hierarchy changes to blue, the object in Hierarchy is an instance of a prepared premade object. At this time, you can decide whether to retain the instance in Hierarchy view or delete the instance as needed. Whether you retain the instance or not, the original pre-fabricated objects created in the Project will not be affected.
2. Create an instance of the premade object in Hierarchy.
To create a "pre-fabricated instance" in the "current scenario", drag and drop a pre-fabricated object to the scenario (Scene) or Hierarchy directly from the Project View).
The "blue" font in the Hierarchy view indicates that the object is an instance of a premade body.
Once you create an instance of a premade object, the instance is automatically linked to the source premade object in the Project and displayed in blue in Hierarchy.
In this example, three game objects are in blue, indicating that they are all pre-fabricated instances.
3. Operate the prefabricated body in the Inspector
(1) Meaning of bold
Sometimes you may want to only change the attributes of a single premade body instance and keep the link to the source premade body intact. At this time, you can see that the variable name corresponding to this instance has changed to bold, indicates that the variable has been overwritten. Note that all override properties are not affected by changes to the source premade body.
(2) Select
If you select a premade instance and want to affect all the instances, you can directly go to the Inspector) click Select to Select the modified original premade body.
(3) Apply
If you want to update the source premade body and all its instances with the value of override, click Apply in Inspector. Note that such "application" does not work for "root location" and "rotation", because it affects the absolute location of the instance and causes all instances to be placed in the same location. However, Apply takes effect on the position and rotation of any child or ancestor element of the root, because they are calculated relative to the root transformation.
(4) Revert
If you want to discard all rewriting operations on a specific instance, click (Revert.
4. Import the prefabricated body
When you place Grid Resources in the Assets folder, Unity will automatically import files and generate something similar to what looks like a premade object outside the grid. However, this is actually not a Prefab, but it is only used by the resource file itself.
Note that the resource (asset) icon is slightly different from the preset (Prefab) icon.
A resource is an instance of a game object in a scenario. It is linked to the source resource rather than to the Prefab ). You can add or delete components to or from a game object ,. However, you cannot make any changes to the resource itself, because this will add data to the resource file itself! If you want to create something that can be reused, you should make it into a premade body.
When you select a resource instance, The Apply button in the Inspector will be replaced with the Edit button. Click this button to start the editing application (such as Maya or Max) for the resource ). Ii. Basic usage example of Prefabs
After the above introduction, You should have understood the concept of Prefabs from the basic level: Prefabs is essentially a collection of GameObjects (pre-defined game objects) and Components (Components, is a container object that can be reused in the game.
With Prefabs, you can easily instantiate complex GameObjects (Game objects) at runtime ).
1. How to Create game objects
In Unity, there are two ways to create a Game Object:
(1) Method 1: all are implemented using C # code. In this way, use the C # code to create and initialize the game object (GameObjects) from the beginning ).
(2) Method 2: implement with Prefabs. That is, you must first create the pre-fabricated body and then instantiate the pre-fabricated body through code during running.
Compared with method 1, method 2 (implemented with Prefabs) has multiple advantages:
L a line of code can be used to instantiate the Prefab with complete functions. Using code to create equivalent game objects (GameObjects) requires an average of five lines of code and more.
L you can quickly set, test, and modify prefabricated bodies in scenarios (Scene) and Inspector.
L you can change the instantiated premade body without changing the code of the instantiated premade body. For example, you can change a simple rocket to a super rocket without changing the code.
2. Example-create a wall
To illustrate the strength of Prefabs, let's take a look at some basic situations that can be used with Prefabs:
(1) Use a single "brick" premade block multiple times to create a wall at different locations.
(2) During the launch, the rocket launcher instantiates a flying rocket premade body. The premade body is a Mesh, Rigidbody, Collider, and Particle System with its own tail) ).
(3) robots are exploding into many fragments. At this time, the complete Operating Robot has been destroyed and replaced with a broken robot pre-fabricated body. The premade body contains a robot that is divided into multiple parts, all of which are equipped with their own Rigid Bodies (Rigidbodies) and Particle Systems (Particle Systems ). With the premade body, you can replace an object with a specified premade body with only one line of code, so that the robot can blow up into many fragments.
This example only demonstrates the advantages of the former (using Prefab to create an object) compared to the "using Prefab to create an object.
(1) Implementation Method 1
First, we will demonstrate the implementation method of "using code to create objects", because only comparison can help you understand the shortcomings of this method.
Create a subfolder named Scripts under the Assets in the Project view of the ch03Demos Project, and add a C # script named Demo1 to the subfolder:
Double-click the Demo1 script, and Unity will automatically start VS2015 and open the Demo1.cs file in VS2015. In solution resource manager, the folder structure under Assets is the same as that in Unity:
Change the Demo1.cs file to the following content:
using UnityEngine;using System.Collections;public class Demo1 : MonoBehaviour{ void Start () { for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.AddComponent<Rigidbody>(); cube.transform.position = new Vector3(x, y, 0); } } }}
Save the file.
Switch to Unity, select GameObject> Create Empty in the main menu to Create an Empty game object, and drag and drop the Demo1 script in the Scripts folder to the newly added Empty game object.
Switch to the Game view and click play to view the effect. At this point, we will see the cube wall completely created with code (because gravity is used by default, the wall will automatically collapse ):
Of course, because no texture is added, we only see a white wall (one cube after the collapse ). If you want to perform additional operations, such as changing the quality of the brick texture, friction, or rigid body, you also need to add other code to see How troublesome it is to be implemented in this way.
Delete the empty game object created in the hierarchical view.
(2) Implementation Method 2
In the 2nd method, you can first create a "wall" premade body, and then use the "create premade body instance" method to create multiple walls.
The procedure is as follows:
Choose GameObject> 3D Object> Cube on the main menu to create a Cube:
The created Cube is selected in the hierarchy view.
Drag the brick_D in the Texture folder to the cube in the hierarchy view.
In the Assets folder of the Project view, add a Prefabs sub-Folder:
Right-click the Prefabs sub-folder and choose Create> Prefab ]:
Change the name of the newly added Prefab to "Brick ".
Drag and Drop the cube created in the hierarchy view to the "Brick" created in the Project view.
In this way, a premade (or preset) named "Brick" is created ).
After a preset is created, the Cube added to the Hierarchy view cannot be used. You can select it from Hierarchy and press Delete to Delete it.
Add a new empty game object ).
Add a C # script (Demo2.cs) named Demo2.cs under the Scripts sub-folder in the VS2015 solution Resource Manager project, and change it to the following content:
using UnityEngine;using System.Collections;public class Demo2 : MonoBehaviour{ public Transform Brick; void Start () { for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) { Instantiate(Brick, new Vector3(x, y, 0), Quaternion.identity); } } }}
Press the <F5> key to attach it to Unity, and then press <Shift> + <F5>.
Switch to Unity and drag the script in the project view to the added Cube. This adds a Demo2 component to the object:
In addition, a new variable named "Brick" will appear in the Inspector. This Transform type variable can accept any game object or premade body.
From the Project View, drag the pre-fabricated "Brick" to the value of the Brick variable in the Inspector View (: create an instance of the premade body as the Brick variable value ):
Press Play to see the wall created using both the premade body and C # script:
It can be seen that the script code implemented in this way is not only very clean, but also the Brick premade body can be reused.
At the beginning, you may want to know why it is more flexible and convenient to create cubes in 1st ways? Why another Prefab?
This is because with Prefab, you can adjust it within a few seconds:
- Do you want to add or change the quality of all instances? You only need to add a rigid body to the pre-fabricated body and adjust the gravity of the rigid body at one time.
- Want to use different materials (Material) for all instances )? You only need to drag the Material (Material) to the premade body once.
- Want to change friction? You can use different physical materials (Physic Material) in the pre-fabricated collision tool.
- Want to add a Particle System for all cubes )? Add a sub-object to the premade object.
Of course, if you fail to find the brick_d.jpg file, you can easily find another .jpg image. The goal is to fill the wall with a cube and understand its meaning.