查看: 1064|回复: 0
打印 上一主题 下一主题

[其他] 通过二维数组生成地图的代码

[复制链接]
may    

8830

主题

81

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52352
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-8-31 06:59:47 |只看该作者 |倒序浏览

通过二维数组生成地图的代码
  1. using UnityEngine;
  2. using System.Collections;

  3. public class CreateMap : MonoBehaviour {
  4.     public GameObject cubePrefab;
  5.     private int[,] map;
  6.     public int rows;
  7.     public int columns;
  8.     private GameObject[,] createMap;
  9.     private float gap = 1f;
  10.     void Awake() {
  11.         rows = 10;
  12.         columns = 10;
  13.         map = new int[rows, columns];
  14.         for (int i = 0; i < rows; i++)
  15.         {
  16.             for (int j = 0; j < columns; j++)
  17.             {
  18.                 map[i, j] = Random.Range(0, 2);
  19.             }
  20.         }
  21.         createMap = new GameObject[rows, columns];
  22.     }
  23.         // Use this for initialization
  24.         void Start () {
  25.         CreateCubes();
  26.         }
  27.        
  28.         // Update is called once per frame
  29.         void Update () {
  30.         
  31.             
  32.         }

  33.     public void CreateCubes() {
  34.         float posX = 0;
  35.         float posZ = 0;
  36.         for (int i = 0; i < rows; i++)
  37.         {
  38.             for (int j = 0; j < columns; j++)
  39.             {
  40.                 posX = j * gap;
  41.                 posZ = i * gap;
  42.                 if (1 == map[i, j]) {
  43.                     createMap[i, j] = Instantiate(cubePrefab, new Vector3(posX, 0f, posZ), Quaternion.identity) as GameObject;
  44.                 }
  45.             }
  46.         }
  47.     }

  48. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-28 03:44 , Processed in 0.061844 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部