Create objects dynamically in Unity3d and create prototype objects in three ways prefab

Source: Internet
Author: User

Create objects dynamically in Unity3d and create prototype objects in three ways prefab
U3d in dynamically created objects, you need to use prefab
And the object that Monobehaviour.instantiate (Gameobject orignal) needs when it is created is a prototype.

This article provides three ways to get prefab objects.

Method One: Use the public field of the script
Find a good prefab directly in Project view and drag it to the specified public gameobject field of the specified script.

Way Two: Resource class
1. Create a directory named resources in any location under the assets directory. Put the good prefab in this directory, path forms such as the following:
2. Use the Resource.load or Loadall function in the code to get the prototype object.

You do not need to specify an extension (. prefab) when specifying prefab, such as the following:
Gameobject prototype = resource.load ("Prefabname") as Gameobject;
Can have a random number of resources directory, it is suspected that the resource class initialization will be collected in all resources directory of the file name.

Mode three: Load into the scene
In general, when we make perfab, we create the gameobject in the hierarchy view, and then build the prefab.

The original gameobject must be deleted afterwards.
So we can keep this gameobject. Then find this object (code mode) after the scene is loaded, or use the Script public field (editor mode).

Use a more consistent style of unity.
I use mode two, because I am a program Ape ~ ~ Want to control everything in code.

The way three is more wordy.

Instantiate () as gameobject can be replaced by Gameobject, for which he formulates the corresponding compment

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Create objects dynamically in Unity3d and create prototype objects in three ways prefab

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: 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.