美文网首页
GameObject

GameObject

作者: 幽灵小子 | 来源:发表于2018-01-08 15:02 被阅读7次

构造方法

        //使用构造函数GameObject (name : String)
        GameObject g1 = new GameObject("GO1");
        g1.AddComponent<Rigidbody>();
        //使用构造函数GameObject () 
        GameObject g2 = new GameObject();
        g2.AddComponent<FixedJoint>();
        //使用构造函数GameObject (name : String, params components : Type[])
        GameObject g3 = new GameObject("G3",typeof(MeshRenderer),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);

实例属性

Active

GameObject有两个常用属性activeSelf,activeInHierarchy
他俩都是判定当前GO是否是激活状态,也就是下图中的对钩是否打上,但他们的区别是,activeSelf是完全判断这个对钩是否打上,而activeInHierarchy则要求在整个Hierarchy中是否被激活,它依赖它所有父节点,如果有一个父节点激活状态为false那么activeInHierarchy则为false。


image.png
        //初始状态:cube1激活,cube2非激活,cube3激活
        Debug.Log("cube1.activeSelf:" + cube1.activeSelf); //true
        Debug.Log("cube2.activeSelf:" + cube2.activeSelf); //false
        Debug.Log("cube3.activeSelf:" + cube3.activeSelf); //ture
        Debug.Log("cube1.activeInHierarchy:" + cube1.activeInHierarchy);//ture
        Debug.Log("cube2.activeInHierarchy:" + cube2.activeInHierarchy);//false
        Debug.Log("cube3.activeInHierarchy:" + cube3.activeInHierarchy);//false

实例方法

1.获取第一个组件

GetComponent(typeof(Rigidbody))as Rigidbody;
GetComponent<Rigidbody>();
GetComponent("Rigidbody") as Rigidbody;

2.获取本身和孩子的第一个组件

GetComponentInChildren(typeof(Rigidbody)) as Rigidbody;
GetComponentInChildren<Rigidbody>();

3.获取组件数组

Component[] cjs = GetComponents(typeof(ConfigurableJoint)) as Component[];
GetComponents<ConfigurableJoint>();

4.获取本身和孩子的组件数组。其中的bool类型代表是否获取未激活状态的组件。

GetComponentsInChildren(typeof(ConfigurableJoint), false) as Component[];
GetComponentsInChildren(typeof(ConfigurableJoint), true) as Component[];
GetComponentsInChildren<ConfigurableJoint>(true);
GetComponentsInChildren<ConfigurableJoint>();

5.发送信息SendMessage

利用反射机制,调用自身或者其他类的方法。例如有3个cube,cube1作为父级,cube2作为子级,cube3作为cube2的子级。

//向子类及自己发送信息,所有子类和本身都会调用一次GetParentMessage方法。
gameObject.BroadcastMessage("GetParentMessage",gameObject.name+":use BroadcastMessage send!");
//向自己发送信息,调用自己类的GetSelfMessage方法
gameObject.SendMessage("GetSelfMessage",gameObject.name+":use SendMessage send!");
//向父类及自己发送信息,所有父类和本身调用一次GetChildrenMessage方法
gameObject.SendMessageUpwards("GetChildrenMessage",gameObject.name+":use SendMessageUpwards send!");
   //一个接受父类发送信息的方法
    private void GetParentMessage(string str){
        Debug.Log(gameObject.name + "收到父类发送的消息:" + str);
    }
    //一个接受自身发送信息的方法
    private void GetSelfMessage(string str)
    {
        Debug.Log(gameObject.name + "收到自身发送的消息:" + str);
    }
    //一个接受子类发送信息的方法
    private void GetChildrenMessage(string str)
    {
        Debug.Log(gameObject.name + "收到子类发送的消息:" + str);
    }

静态方法

CreatePrimitive方法,创建GameObject对象。

GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(0, 0.5F, 0);
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = new Vector3(0, 1.5F, 0);
GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
capsule.transform.position = new Vector3(2, 1, 0);
GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
cylinder.transform.position = new Vector3(-2, 1, 0);

相关文章

网友评论

      本文标题:GameObject

      本文链接:https://www.haomeiwen.com/subject/dkkbnxtx.html