Unity API Parsing (3)--gameobject class

Source: Internet
Author: User

The Gameobject class is the accumulation of all entities in a unity scene. A Gameobject object is typically composed of multiple component component and contains at least one transform component.

Activeself the active identity of the--gameobject property

The function of the Activeinhierarchy property is to return the activation state of the Gameobject instance when the program is run, which returns true only if the state of the Gameobect instance is activated. It is also affected by the activation state of the parent class object. If an object in its parent class to the topmost object is not activated, Activeinhierarchy returns false

Using unityengine;using System.collections;public class Activeself_ts:monobehaviour {public    gameobject cube1, Cube2, Cube3;void Start () {        //pair Cube2 is set to False, the other is set to True        cube1. SetActive (true);        Cube2. SetActive (false);        Cube3. SetActive (true);        Debug.Log ("activeself:");        Although Cube2 is set to False, the Activeself return value of its subclass cube3 is still true        Debug.Log ("cube1.activeself:" + cube1.activeself);        Debug.Log ("cube2.activeself:" + cube2.activeself);        Debug.Log ("cube3.activeself:" + cube3.activeself);        Debug.Log ("\nactiveinhierarchy:");        The Activeinhierarchy return values for Cube2 and cube3 are false        Debug.Log ("Cube1.activeinhierarchy:" + cube1.activeinhierarchy);        Debug.Log ("Cube2.activeinhierarchy:" + cube2.activeinhierarchy);        Debug.Log ("Cube3.activeinhierarchy:" + Cube3.activeinhierarchy);}}

Gameobject Construction Method

Public gameobject ();

Public Gameobject (string name);

Public Gameobject (String name,params type[] components)

using unityengine;using System.collections;public class Constructors_ts:monobehaviour {        void Start () {//Use constructor Gameobject (name:string) gameobject g1 = new Gameobject ("G1"); G1.        Addcomponent<rigidbody> ();        Use the constructor Gameobject () gameobject g2 = new Gameobject (); G2.        Addcomponent<fixedjoint> (); Use the constructor gameobject (name:string, params components:type[]) gameobject g3 = new Gameobject ("G3", typeof (Meshren        Derer), typeof (Rigidbody), typeof (Springjoint));        Debug.Log ("G1 name:" + G1.name + "\nposition:" + g1.transform.position);        Debug.Log ("G2 Name:" + G2.name + "\nposition:" + g2.transform.position); Debug.Log ("G3 Name:" + G3.name + "\nposition:" + g3.transform.position);}} 

Gameobject class instance method

Getcomponent Method--Get component

Public T getcomponent<t> () where t:component;

Public Component getcomponent (string type);

Public Component getcomponent (type type);

Using unityengine;using System.collections;public class Getcomponent_ts:monobehaviour {void Start () {Debug.Log ( The following are the relevant usage codes for getcomponent.        \ n The Getcomponet method is used to get the first component in the current gameobject that conforms to type ");        Getcomponent (type:type) Rigidbody RB = Getcomponent (typeof (Rigidbody)) as Rigidbody; Debug.Log ("Get Rigidbody with Getcomponent (type:type)" + RB.        Getinstanceid ());        Getcomponent.<t> () rb=getcomponent<rigidbody> (); Debug.Log ("Use getcomponent.<t> () to get Rigidbody" + RB.        Getinstanceid ());        Getcomponent (type:string) RB = Getcomponent ("Rigidbody") as Rigidbody; Debug.Log ("Get Rigidbody with Getcomponent (type:string)" + RB.        Getinstanceid ()); Debug.Log ("The following is the relevant usage code for Getcomponentinchildren.        \ n The Getcomponentinchildren method to get the first component in all subclasses of the current gameobject that conforms to type ');        Getcomponentinchildren (type:type) RB = Getcomponentinchildren (typeof (Rigidbody)) as Rigidbody; Debug.Log ("Use Getcomponentinchildren (tyPe:type) Get Rigidbody "+ rb.name);        Getcomponentinchildren.<t> () rb=getcomponentinchildren<rigidbody> ();        Debug.Log ("Use getcomponentinchildren.<t> () to get Rigidbody" + rb.name);        Getcomponents (Type:type) component[] CJs = getcomponents (typeof (Configurablejoint)) as component[]; foreach (Configurablejoint cj in CJs) {Debug.Log ("Use Getcomponents (Type:type) to get Configurablejoint" + CJ.        Getinstanceid ());        }//getcomponents.<t> () CJs = getcomponents<configurablejoint> (); foreach (Configurablejoint cj in CJs) {Debug.Log ("Use getcomponents.<t> () get Configurablejoint" + CJ .        Getinstanceid ()); }//getcomponentsinchildren (Type:type, Includeinactive:boolean = false) CJs = Getcomponentsinchildren (type        Of (Configurablejoint), false) as component[]; foreach (Configurablejoint cj in CJs) {Debug.Log ("Use Getcomponentsinchildren (tYpe:type, false) get Configurablejoint "+ cj.name);        } CJS = Getcomponentsinchildren (typeof (Configurablejoint), true) as component[]; foreach (Configurablejoint cj in CJs) {Debug.Log ("Use Getcomponentsinchildren (Type:type, true) to get Configur        Ablejoint "+ cj.name); }//getcomponentsinchildren.<t> (Includeinactive:boolean) CJs = Getcomponentsinchildren<configura        Blejoint> (TRUE);  foreach (Configurablejoint cj in CJs) {Debug.Log ("Use Getcomponentsinchildren.<t> (includeinactive:        Boolean) Get Configurablejoint "+ cj.name);        }//getcomponentsinchildren.<t> () CJs = getcomponentsinchildren<configurablejoint> (); foreach (Configurablejoint cj in CJs) {Debug.Log ("Use getcomponentsinchildren.<t> () get Configurablejo        int "+ cj.name); }}}

SendMessage method: Send Message

Objects of their own rank will not receive messages

Sendmessageoptions is available in two options: Sendmessageoptions.requirereceiver and Sendmessageoptions.dontrequirereceiver The former requires the receiver of the information must have the method of receiving information, or the program error, the latter does not have this requirement

Broadcastmessage sends messages to itself and to all subclasses, and objects of its own kind do not receive messages

The function of the Sendmessageupwards method is to send a message to the Gameobject itself and all its parents, and the object of its own sibling is not subject to the message

Using unityengine;using System.collections;public class Sendmessageupward_ts:monobehaviour {void Start () {//To parent class and send        gameobject.sendmessageupwards ("Getchildrenmessage", Gameobject.name + ": Use Sendmessageupwards send!"); /Shangzi class and send your own message gameobject.broadcastmessage ("Getparentmessage", Gameobject.name + ": Use Broadcastmessage send");// Send yourself a message gameobject.sendmessage ("Getselfmessage", Gameobject.name + ": Use SendMessage send");}    private void Getparentmessage (String str)    {        Debug.Log (Gameobject.name + "received the message sent by the parent class" + str);    }    private void Getselfmessage (String str)    {        Debug.Log (gameobject.name + "message sent by itself" + str);    }    private void Getchildrenmessage (String str)    {        Debug.Log (gameobject.name + "message sent by subclass" + str);}    }

createprimitive--Creating Gameobject Objects

Static methods

Using unityengine;using system.collections;public class createprimitive_ts:monobehaviour{void Start () {//        Use the Gameobject.createprimitive method to create gameobject gameobject g1 = gameobject.createprimitive (Primitivetype.sphere);        G1.name = "G1";        G1.tag = "Sphere_tag"; Add the component G1 using AddComponent (classname:string).        AddComponent ("Springjoint"); Add the component G1 using AddComponent (Componenttype:type).        AddComponent (typeof (Guitexture));        G1.transform.position = Vector3.zero;        Gameobject g2 = gameobject.createprimitive (Primitivetype.sphere);        G2.name = "G2";        G2.tag = "Sphere_tag"; Add component G2 using Addcomponent.<t> ().        Addcomponent<rigidbody> ();        g2.transform.position = 2.0f * vector3.right; G2.        Getcomponent<rigidbody> (). Usegravity = false;        Gameobject g3 = gameobject.createprimitive (Primitivetype.sphere);        G3.name = "G1";        G3.tag = "Sphere_tag"; G3.transform.position = 4.0f * vector3.right; Get Gameobject using the Gameobject.findà class method, return the first object that meets the criteria Debug.Log ("Use Find:" + gameobject.find ("G1"). Transform.position)        ; Get Gameobject using the Gameobject.findgameobjectwithtag class method, return the first object that meets the criteria Debug.Log ("Use Findgameobjectwithtag:" + gameobje Ct.        Findgameobjectwithtag ("Sphere_tag"). Transform.position); Get Gameobject using the Gameobject.findgameobjectswithtag class method, return all objects that meet the criteria gameobject[] Gos = Gameobject.findgameobjectswith        Tag ("Sphere_tag"); foreach (Gameobject go gos) {Debug.Log ("Use Findgameobjectswithtag:" + go.name + ":" + go.transform.        position);        }//change G1,g2, and G3 's hierarchical relationship g3.transform.parent = g2.transform;        G2.transform.parent = G1.transform;        Debug.Log ("Use Find again1:" + gameobject.find ("G1"). Transform.position);    Find Gameobject Debug.Log ("Use Find again2:" + gameobject.find ("/g1/g2/g1") by using the "/" condition. Transform.position); }}

To get a component in the Gameobject object where the current script is located, use the Getcompoent method directly, as

Rigidbody br = getcomponent<rigidbody> ()

To get a component in the Gameobject object that is not the current script, you need to have Gameobject as the predecessor reference, as the variable go is a reference to the Gameobject object.

Rigidbody br = go. Getcomponent<rigidbody> ()

Using unityengine;using System.collections;public class Gameobjectandcomponent_ts:monobehaviour {public    Gameobject sp;void Start () {        //The following 3 modes of expression function the same        rigidbody rb1 = rigidbody;        Rigidbody Rb2 = getcomponent<rigidbody> ();        Rigidbody Rb3 = rigidbody. Getcomponent<rigidbody> ();        Debug.Log ("Rb1μ? Instanceid£o "+ RB1. Getinstanceid ());        Debug.Log ("Rb2μ? Instanceid£o "+ RB2. Getinstanceid ());        Debug.Log ("Rb3μ? Instanceid£o "+ Rb3. Getinstanceid ());        Use a predecessor reference to get the rigidbody component of the referencing object        Debug.Log ("Instanceid rigidbody in the predecessor reference SP object" +SP. Getcomponent<rigidbody> (). Getinstanceid ());}}

Unity API Parsing (3)--gameobject class

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.