【发布时间】:2016-10-12 15:39:17
【问题描述】:
我正在尝试统一制作一个瓦片网格图,其中每个瓦片都是一个包含某些信息的类。所有的瓦片信息都保存在一个名为 TileData 的脚本中。我有四种瓷砖类型,每一种都有 4 个变量。 (平铺图形、平铺名称、平铺位置 X 和平铺位置 Z。)用于构建地图的所有信息都包含在第二个脚本中。 (我在地图脚本中调用了本地版本的 tiledata 脚本。)
我可以通过使用一个多维数组来成功构建带有瓷砖的网格,该数组从瓷砖类数组中随机选择一个瓷砖类。 我的问题是,当我尝试为图块位置分配位置 x 和位置 z 时,它会覆盖图块数组中的信息。最终结果是每个图块显示所有其他类似图块的 x、z,而不是单独显示。
我想要做的是构建一个随机类瓦片的二维数组,但为每个新瓦片分配自己的 x 和 z 坐标。 (我打算稍后将其用于移动/寻路等)
我尝试了所有我能想到的方法,甚至尝试了一个多维锯齿状数组,但无法让它工作。
下面的代码(去掉了不相关的部分):
TileData 脚本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TileData {
[System.Serializable]
public class Tile //Public class Tile to act as Tile Template.
{
public int tileGraphic = 0; //An int to point to our Tile Graphic
public string tileName = "Unknown"; //Name of the Tile.
public int tilePositionX = 0;
public int tilePositionZ = 0;
}
public Tile[] tileTypes = new Tile[4]; //Makes a new Tile Array from the Tile Class
public void makeArray() //Constructs the array.
{
tileTypes[0] = new Tile { tileGraphic = 0, tileName = "Grassland"}; //Makes the new Tile and puts them in the array.
tileTypes[1] = new Tile { tileGraphic = 1, tileName = "Water"};
tileTypes[2] = new Tile { tileGraphic = 2, tileName = "Forest"};
tileTypes[3] = new Tile { tileGraphic = 3, tileName = "Mountain"};
}
地图脚本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))] //Ensures the object has a filter, Renderer and Collider when created.
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class missionMap : MonoBehaviour
{
//Map Variables
public int sizeX = 25; //The left/right size of the map (in tiles). Public so it can be changed from the editor.
public int sizeZ = 25; //The forward/backward size of the map (in tiles).
public float tileSize = 1.0f; //The size of each tile. (How many vertices wide the tile is.)
//Tile Variables
public TileData localTileData = new TileData(); //Pulls the tile types in from their own script.
public TileData.Tile[,] localTileArray = new TileData.Tile[25, 25]; //Builds an Array of our Tile class the size of our map, so each tile on the map has a Tile Class corresponding to it.
void Start()
{
localTileData.makeArray(); //Calls the makeArray from the Tile Data script, so we can access the array later.
//localTileArray[0, 0] = localTileArray[sizeX,sizeZ];
BuildMap(); //Builds the map when the code starts.
//BuildPathGraph();
}
}
我省略了地图的实际构建并跳到图块。
public void BuildTiles()
{
for (int z = 0; z < sizeZ; z++) //A double for loop to populate our tile position array with the size of the our map in x and z.
{
for (int x = 0; x < sizeX; x++)
{
localTileArray[x, z] = localTileData.tileTypes[Random.Range(0, 4)]; //Fills the Array with Tile Copies of type 0 - 3 (0 = grass, 2 = water, 3 = forest, 4 = mountain.)
}
}
【问题讨论】:
标签: c# arrays class multidimensional-array unity3d