Android + unity3d prefabs (preset) and instantiate (instantiated)

Source: Internet
Author: User

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

  1. Public var pre: gameobject; // prefabs used for binding in the editor

  2. Private function update
    (): Void
  3. {
  4. VaR instance: gameobject = instantiate (PRE, transform. Position, transform. Rotation );
  5. // Instantiate usage. Pay attention to the parameters.
  6. // Prefabs used for cloning
  7. // The Position of the object bound to the transform. Position script is like this. transform ....
  8. // The perspective of the transform. Rotation script binding object is like this. transform ....
  9. }

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?

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.