This article is not written by me, I feel good So copy it, Heheh, original link: http://bbs.9ria.com/thread-97976-1-1.html
Explanation: prefabs is a type of resource that can be reused. It can be placed in multiple scenarios or multiple times in one scenario. When you add a prefabs in a scenario, you instantiate a prefabs. All prefabs instances are cloned by Prefab. As long as the prefabs prototype changes, all prefabs instances will change.
Prefabs usage: If you want to create something that you want to reuse, you should use it.
Next, let's write a small example to see how to use it.
1. Set up the following scenario: a floor, a falling cube, adjusting the camera angle and position, adding a parallel light, and referring to the scene window. The effect is shown in the game window. Add the rigid body attribute (Component
-> Physics-> Rigidbody)
Then adjust the angle of the cube, run it, and let it roll.
2. Create prefabs
In the preject window, click Create-> Prefab. Drag the Cube from the hierarchy window to the prefab created in the project window. When the cube in hierarchy turns blue, the prefabs completes filling.
Then, drag the created prefabs from the preject window into the scene window and run it. A clone is completed. They are like twins.
3. Then we use instantiate to create multiple prefabs instances with code.
Instantiate: Clone the original object. Set the location and angle. If an object, component, or script instance is passed in. Clone the entire layers that are sad to each other and all sub-objects.
In the project window, click Create-> JavaScript to create a script.
I found that JS can be written in as3. Please refer
- Public var pre: gameobject; // prefabs used for binding in the editor
- Private function update
(): Void
- {
- VaR instance: gameobject = instantiate (PRE, transform. Position, transform. Rotation );
- // Instantiate usage. Pay attention to the parameters.
- // Prefabs used for cloning
- // The Position of the object bound to the transform. Position script is like this. transform ....
- // The perspective of the transform. Rotation script binding object is like this. transform ....
- }
Copy code
Is there a kind feeling?
4. After writing the script, we need to bind it to an object. At this time, we can delete the cubes in the hierarchy window, create an empty game object (gameobject-> Create empty), and place it to the original cube location. Drag and Drop the script to the empty object of the new key in the hierarchy window to execute the code.
5. Click an empty object in the hierarchy window. The bound script is displayed in its Inspector window. Then we drag the previously created prefabs to the box on the right of the script pre to bind the variables in the script.
6. Just run it in a triangle and make it look like a particle, isn't it so cool?