Unity3d Basics-Dynamically create and set up game objects

Source: Internet
Author: User

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

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.