sing UnityEngine;
using System.Collections;
public class IntroInstiate : MonoBehaviour {
public GameObject _boxPrefb;
public Transform Wall;
private float X;
private float Y;
private float Z;
// Use this for initialization
void Start () {
X=gameObject.transform.position.x;
Y = gameObject.transform.position.y;
Z = gameObject.transform.position.z;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Z)){
CreateWall ();
}
}
private void CreateWall(){
for(int i = 0;i <4 ;i++){
for(int j=0;j<4;j++){
GameObject go = Instantiate (_boxPrefb,new Vector3(X+j,Y+i,Z), Quaternion.identity) as GameObject;
<span style="font-family:Menlo;"><span style="color:#444444;">go</span><span style="color:#444444;">.</span><span style="color:#444444;">name</span><span style="color:#444444;">=</span><span style="color:#f57c00;">"</span><span style="color:#f57c00;">"</span><span style="color:#444444;">+(</span><span style="color:#444444;">i</span><span style="color:#444444;">*</span><span style="color:#f57c00;">4</span><span style="color:#444444;">+</span><span style="color:#444444;">j</span><span style="color:#444444;">);</span>
<span style="color:#444444;"> </span><span style="color:#444444;">go</span><span style="color:#444444;">.</span><span style="color:#444444;">AddComponent</span><span style="color:#444444;"><</span><span style="color:#3363a4;">Rigidbody</span><span style="color:#444444;">>();</span>
<span style="color:#444444;"> </span><span style="color:#444444;">go</span><span style="color:#444444;">.</span><span style="color:#444444;">rigidbody</span><span style="color:#444444;">.</span><span style="color:#444444;">useGravity</span><span style="color:#444444;">=</span><span style="color:#3363a4;">false</span><span style="color:#444444;">;</span>
<span style="color:#444444;"> </span><span style="color:#444444;">go</span><span style="color:#444444;">.</span><span style="color:#444444;">transform</span><span style="color:#444444;">.</span><span style="color:#444444;">parent</span><span style="color:#444444;"> = </span><span style="color:#444444;">Wall</span><span style="color:#444444;">;</span></span>
}
}
}
}
用游戏对象制造制造一面墙
Instantiate 复制一个游戏对象 点击打开链接
AddComponent 添加组件 点击打开链接