Reproduced from the original text: http://m.blog.csdn.net/blog/smbroe/43488627
1.gameobject primitivetypecube (Cube), (Sphere), capsuleplanecylinder (cylinder); Created objects do not need add and stuff like that can be displayed.
gameobject cubeobject = gameobject.createprimitive (Primitivetype.cylinder); CubeObject.transform.position = new Vector3 (- 2.0f , 0.5f , 4.0f ); <span style= font-family: Arial; " > Specify the location of the game object
2. Adding components dynamically: Via addcomponent<rigidbody> (); You can add a rigid body component to a Gameobject object, where rigidbody represents a rigid body, and we can add different components to it by changing the type. When we add a rigid component, we can get the Gameobject Rigidbody object and set the rigid body
Cubeobject.addcomponent<rigidbody> (); // add a rigid body component to a game object 0.5f; // sets the density of the rigid body, which defaults to 1cubeObject.rigidbody.AddForce (Vector3.forward *); // to exert a forward force on it.
3. Add scripts dynamically: You can Add scripts dynamically to the Gameobject object by addcomponent the method and passing in a string object that represents the name of the script .
Cubeobject.addcomponent ("autodestory"); // Add a script file Autodestory
4. Dynamic destruction of objects:Destroy (cubeobject) is the immediate destruction of the game object,Destroy (cubeobject,1) is delayed one second destroy the game object
5. Dynamic destroy script:Destroy (Cubeobject.getcomponent ("Autodestory") destroys the attached foot on the Cubeobject object.
6. In general we will destroy or stop the action when the object is not visible, and perform its action when it is captured and visible, so that we can make a copy of the onbecameinvisible method (which is executed when the game object is not visible) . The Onbecamevisible method, which is executed when the game object is visible, achieves this effect.
using Unityengine; using System.Collections; // destroy when the game object is not visible Public class autodestory : monobehaviour{ void onbecameinvisible () { Destroy ( this. gameobject);} }
7. Copying objects: In the game sometimes the same objects will be used many times, such as a large number of bullets launched, if the use of the method of copying objects can improve efficiency. Calling Gameobject 's Instantiate method returns an object object that is strongly forwarded to get copied.
Gameobject Cubeclone = (gameobject) gameobject.instantiate (cubeobject);
8. dynamically set the material of the game object, add stickers for the game object: We can get the material of the game object by CubeObject.renderer.material, and assign the value to it, generally we can declare a public Material The member variable and assigns it to the game object so that we can change the decal at any time in Unity's editor.
usingUnityengine;usingSystem.Collections; Public classTest:monobehaviour { PublicMaterial cubmeterial; //Use this for initialization voidStart () {gameobject cubeobject=gameobject.createprimitive (Primitivetype.cylinder); CubeObject.transform.position=NewVector3 (-2.0f,0.5f,4.0f); CubeObject.renderer.material=cubmeterial; }}
Unity3d Basics-Dynamically create and set up game objects