void OnGUI() {
if (GUILayout.Button ("创建立方体", GUILayout.Height (50))) {
var objCube = GameObject.CreatePrimitive (PrimitiveType.Cube);
objCube.AddComponent<Rigidbody> ();
objCube.name = "Cube";
Renderer renderer = objCube.GetComponent<Renderer> ();
renderer.material.color = Color.blue;
objCube.transform.position = new Vector3 (0.0f, 10.0f, 0.0f);
}
if (GUILayout.Button ("创建球体", GUILayout.Height (50))) {
var objSphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
objSphere.AddComponent<Rigidbody> ();
objSphere.name = "Sphere";
Renderer renderer = objSphere.GetComponent<Renderer> ();
renderer.material.color = Color.red;
objSphere.transform.position = new Vector3 (0.0f, 10.0f, 0.0f);
}
}
【0】GameObject.CreatePrimitive()方法,用于创建一个原始对象。
public enum PrimitiveType
{
Sphere,
Capsule,
Cylinder,
Cube,
Plane,
Quad
}
【1】AddComponent<>()方法,用于给对象添加一个组件(脚本也是组件)。
【2】renderer.material.color,设置渲染材质的颜色或者贴图。
【3】transform.position,设置对象的位置。