unity3d中通过代码实现创建一堵墙的功能

 在unity3d中可以通过拖拽创建一堵墙,但是这对墙缝隙的把握要求很高,很难创建出标准的一堵墙,我们就可以通过算法实现创建一堵墙的功能,条用unity圣典中vector3方法实现创建墙的效果,代码非常简单。

using UnityEngine;
using System.Collections;
/*
    实现创建一堵墙的功能
     
     */
public class createwall : MonoBehaviour {
    public GameObject zhuankuai;
    // Use this for initialization
    void Start () {
//复杂的方法创建的一堵墙,不推荐使用,代码没有可读性
        //GameObject.Instantiate(zhuankuai,new Vector3(0,0,0),Quaternion.identity);
        //GameObject.Instantiate(zhuankuai, new Vector3(1, 0, 0), Quaternion.identity);
        //GameObject.Instantiate(zhuankuai, new Vector3(2, 0, 0), Quaternion.identity);
        //GameObject.Instantiate(zhuankuai, new Vector3(3, 0, 0), Quaternion.identity);
        //GameObject.Instantiate(zhuankuai, new Vector3(4, 0, 0), Quaternion.identity);
        //GameObject.Instantiate(zhuankuai, new Vector3(5, 0, 0), Quaternion.identity);
        //GameObject.Instantiate(zhuankuai, new Vector3(6, 0, 0), Quaternion.identity);
        //GameObject.Instantiate(zhuankuai, new Vector3(7, 0, 0), Quaternion.identity);
        //GameObject.Instantiate(zhuankuai, new Vector3(8, 0, 0), Quaternion.identity);
        //GameObject.Instantiate(zhuankuai, new Vector3(9, 0, 0), Quaternion.identity);
        //GameObject.Instantiate(zhuankuai, new Vector3(10, 0, 0), Quaternion.identity);
        //使用for循环提高代码的可重用性
        for (int i = 0; i <= 5; i++) {
            for (int j = 0; j <= 5; j++) {
                GameObject.Instantiate(zhuankuai, new Vector3(i, j, 0), Quaternion.identity);//创建好了一堵实用的墙
            }
           
        }
    }
    
}


你可能感兴趣的:(unity3d,C#,c#,unity3d)