Jos*_*ins 0 c# arrays unity-game-engine unity5
所以在Unity中我的this.tilesX和this.tilesY都是公共变量,它们都有一个值.它们设置在Unity的检查员中.数组初始化后的debug.log读出"10 x tiles 10 y tiles".所以我知道这两个变量都已初始化.
但是,当我去检查this.tileLayer1 2D数组的元素是否为null时,它返回debug.log打印出"tile is null".我完全迷失了.下面是初始化数组的函数以及我的自定义Tile类的构造函数.
void Start () {
this.tileLayer1 = new Tile[this.tilesY, this.tilesX];
Debug.Log(tilesX + " x tiles " + tilesY + " y tiles");
for (int y = 0; y < this.tileLayer1.GetLength(0); y++)
{
for (int x = 0; x < this.tileLayer1.GetLength(1); x++)
{
if (this.tileLayer1[x, y] == null)
{
Debug.Log("tile is null");
}
}
}
this.BuildMesh();
}
Run Code Online (Sandbox Code Playgroud)
这是新Tile代码调用的构造函数.
public Tile () {
this.totalVerts = this.vertX * this.vertY;
this.vertices = new Vector3[totalVerts];
this.normals = new Vector3[totalVerts];
this.uv = new Vector2[totalVerts];
this.triangles = new int[6];
}
Run Code Online (Sandbox Code Playgroud)
我不认为构造函数与它有很大关系,但谁知道呢.
那是因为this.tileLayer1 = new Tile[this.tilesY, this.tilesX];只用null值初始化数组.
您需要初始化每个值
for (int y = 0; y < this.tileLayer1.GetLength(0); y++) {
for (int x = 0; x < this.tileLayer1.GetLength(1); x++) {
this.tileLayer1[x, y] = new Title();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1420 次 |
| 最近记录: |