Go Prefabs (presets) and instantiate (instantiation)

Source: Internet
Author: User

Explanation: Prefabs (Preset) is a type of resource that can be reused for game objects. It can be placed into multiple scenes or can be placed multiple times in a scene. When you add a prefabs to a scene, you instantiate a prefabs. All prefabs instances are clones of prefab, and all prefabs instances will change as long as the prefabs prototype changes.

Prefabs usage: If you want to create something that you want to reuse, then you should use it.

Next we'll write a small example of how to use it

1 Build the following scene: A floor, a falling cube, adjust the camera's angle and position, add a parallel light, position reference scene window, the effect as shown in the game window. Also adds a rigid body attribute to the cube (Component, Physics, Rigidbody)

Then adjust the angle of the cube, run it, and let it roll.

2 Creating Prefabs

In the Preject window, point create-> Prefab. We then drag the cube from the hierarchy window onto the newly created prefab in the Project window. When the cube in the hierarchy becomes blue, the prefabs completes the fill.

Then you drag the created prefabs from the Preject window into the scene window and run it. A clone was completed. They're like twins.

3 We then cooperate with instantiate to create instances of multiple prefabs in code.

Instantiate: Clones the original object. Set the position, angle. if an object, component, script instance is passed in. Will clone the whole to love the sad level, as well as all the child objects.

Fellow students, let's create a script in the Project window dot create, JavaScript

I found that the AS3 can be used to write the JS in this, please look at the students

public var pre:gameobject;//used in the editor to bind the prefabs

Private Function Update (): void

{

Varinstance:gameobject=instantiate (pre,transform.position,transform.rotation);

Instantiate usage, note parameters

Pre used to clone the Prefabs

The location of the Transform.position script binding object is like This.transform ....

Transform.rotation the angle of the script binding object is like This.transform ....

}

Is there a feeling of kindness?

4 after writing the script, we will bind it to an object. At this point, we can delete the cube in the Hierarchy window and then create an empty game object (Gameobject->createempty) and place it on the original cube. Drag and drop the script onto the empty object of the new key in the Hierarchy window, and let it execute the code.

5 Click the empty object in the hierarchy window to see the bound script in its inspector window. We then dragged the previously created prefabs onto the box to the right of the script pre to complete the binding of the variables in the script.

6 Press the small triangle to run a bit, like a particle effect, is not very hi?

Originated from blog:http://blog.sina.com.cn/s/blog_82b6aa3d0100w8sx.html

Go Prefabs (presets) and instantiate (instantiation)

Related Article

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.