【Unity API】3---GameObject

1.创建游戏物体的三种方法

    public GameObject prefab;
	// Use this for initialization
	void Start () {
        //1.第一种创建方法
        GameObject go = new GameObject("Cube");

        //2.第二种 ,可以实例化特效或者角色等等
        GameObject.Instantiate(prefab);//可以根据prefab  或者另外一个游戏物体克隆

        //第三种
        //CreatePrimitive创建的是基本模型,Plane,Cube,Sphere等Unity自带的模型
        GameObject.CreatePrimitive(PrimitiveType.Plane);
        GameObject.CreatePrimitive(PrimitiveType.Cube);

    }

2.GameObject的属性(都是常用的)

3.GameObject独有的静态方法

4.GameObject的Public 方法 

BoradcastMassage

在此游戏对象或其任何子对象的每个MonoBehaviour上调用名为(方法名字)的方法。
接收方法可以通过零参数选择忽略参数。 如果将选项设置为SendMessageOptions.RequireReceiver,则在任何组件未拾取消息时将打印错误。

例子:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void ApplyDamage(float damage) {
        print(damage);
    }
    void Example() {
        gameObject.BroadcastMessage("ApplyDamage", 5.0F);
    }
}

猜你喜欢

转载自blog.csdn.net/A2689863090/article/details/82969890
今日推荐