Skip to main content

小技巧

改变游戏运行的速度 Time.timeScale

Time.timeScale = 2f;

创建物体

//1,第一种创建方法 
//GameObject go = new GameObject("Cube");
//2,第二种
//根据prefab
//根据另外一个游戏物体
//GameObject.Instantiate(prefab);//可以根据prefab 或者 另外一个游戏物体克隆
//3,第三种 创建原始的几何体
//GameObject.CreatePrimitive(PrimitiveType.Plane);
//GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);

创建组件

//go.AddComponent<Rigidbody>();
//go.AddComponent<API01EventFunction>();

查找物体

//Light light = FindObjectOfType<Light>();
//light.enabled = false;
//Transform[] ts= FindObjectsOfType<Transform>();//不查找 未激活的游戏物体
//foreach (Transform t in ts)
//{
// Debug.Log(t);
//}

//GameObject go = GameObject.Find("Main Camera");
//GameObject[] gos= GameObject.FindGameObjectsWithTag("MainCamera");
GameObject go= GameObject.FindGameObjectWithTag("Finish");

消息

广播消息 BroadcastMessage

给当前物体的所有激活的子物体发消息

target.BroadcastMessage("Attack", null, SendMessageOptions.DontRequireReceiver);

给自己发消息

target.SendMessage("Attack", null, SendMessageOptions.DontRequireReceiver);

给父亲发消息

target.SendMessageUpwards("Attack", null, SendMessageOptions.DontRequireReceiver);

获取物体的组件

    Cube cube = target.GetComponent<Cube>();
Transform t = target.GetComponent<Transform>();
Debug.Log(cube);
Debug.Log(t);
Debug.Log("---------------------------------");

Cube[] cubes = target.GetComponents<Cube>();
Debug.Log(cubes.Length);
Debug.Log("---------------------------------");

cubes = target.GetComponentsInChildren<Cube>();
foreach (Cube c in cubes)
{
Debug.Log(c);
}
Debug.Log("---------------------------------");
cubes = target.GetComponentsInParent<Cube>();
foreach (Cube c in cubes)
{
Debug.Log(c);
}