纳金网
标题:
通过二维数组生成地图的代码
[打印本页]
作者:
may
时间:
2015-8-31 06:59
标题:
通过二维数组生成地图的代码
通过二维数组生成地图的代码
using UnityEngine;
using System.Collections;
public class CreateMap : MonoBehaviour {
public GameObject cubePrefab;
private int[,] map;
public int rows;
public int columns;
private GameObject[,] createMap;
private float gap = 1f;
void Awake() {
rows = 10;
columns = 10;
map = new int[rows, columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
map[i, j] = Random.Range(0, 2);
}
}
createMap = new GameObject[rows, columns];
}
// Use this for initialization
void Start () {
CreateCubes();
}
// Update is called once per frame
void Update () {
}
public void CreateCubes() {
float posX = 0;
float posZ = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
posX = j * gap;
posZ = i * gap;
if (1 == map[i, j]) {
createMap[i, j] = Instantiate(cubePrefab, new Vector3(posX, 0f, posZ), Quaternion.identity) as GameObject;
}
}
}
}
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5