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:
Assets\....\resources\prefabname.prefab
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