- 最后登录
- 2019-12-25
- 注册时间
- 2012-8-24
- 阅读权限
- 90
- 积分
- 71088
 
- 纳金币
- 52352
- 精华
- 343
|
通过二维数组生成地图的代码- 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;
- }
- }
- }
- }
- }
复制代码 |
|