MissingFieldException:构造函数中缺少模板化字段

时间:2017-08-15 06:28:40

标签: c# templates exception unity3d

我正在尝试创建一个2D数组类,通过分解数据并以非连续方式存储数据来允许非常大的数据集。但是,我在构造函数中得到了一个我根本不理解的MissingFieldException。我正在使用Unity的c#。

这是令人讨厌的电话:

 ChunkArray<int> lastData = new ChunkArray<int>(textureWidth, textureHeight);

异常是“找不到MissingFieldException:Field'.ChunkArray`1.data'。” 课程如下:

using System.Collections;
using System.Collections.Generic;

public class ChunkArray<T> 
{
    private const int DefaultSideDivision = 16;

    T[,][,] data;

    int xChunks;
    int yChunks;

    int subWidth;
    int subHeight;

    public int w;
    public int h;

    public ChunkArray(int width, int height, int sideDivision = DefaultSideDivision) 
    {
        w = width;
        h = height;
        subWidth = width / sideDivision;
        subHeight = height / sideDivision;

        int widthRemainder = width % sideDivision;
        int heightRemainder = height % sideDivision;

        xChunks = (widthRemainder == 0) ? sideDivision : sideDivision + 1;
        yChunks = (heightRemainder == 0) ? sideDivision : sideDivision + 1;

        data = new T[xChunks, yChunks][,];

        for (int i = 0; i < xChunks; i++) 
        {
            for (int j = 0; j < yChunks; j++) 
            {
                int x = (i < sideDivision) ? subWidth : widthRemainder;
                int y = (j < sideDivision) ? subHeight : heightRemainder;
                data[i, j] = new T[x,y];
            }
        }           
    }

    public T get(int x, int y)
    {
        return data[x / subWidth, y / subHeight][x % subWidth, y % subHeight];
    }

    public void set(int x,int y, T value) {
        data[x / subWidth, y / subHeight][x % subWidth, y % subHeight] = value; 
    }
}

任何帮助都会受到赞赏,因为我完全被难倒了。我希望我的格式正确。

1 个答案:

答案 0 :(得分:1)

这看起来像是Mono中的一个错误:它无法实例化多维数组的通用多维数组。但是,当我尝试用T [] []替换T [,] [,]时,它有效,所以我建议用二维数组模拟二维数组:

public class ChunkArray<T>
{
    private const int DefaultSideDivision = 16;

    T[][] internalData;

    int xChunks;
    int yChunks;

    int subWidth;
    int subHeight;

    public int w;
    public int h;

    public ChunkArray(int width, int height)
    {
        int sideDivision = DefaultSideDivision;
        w = width;
        h = height;
        subWidth = width / sideDivision;
        subHeight = height / sideDivision;

        int widthRemainder = width % sideDivision;
        int heightRemainder = height % sideDivision;

        xChunks = (widthRemainder == 0) ? sideDivision : sideDivision + 1;
        yChunks = (heightRemainder == 0) ? sideDivision : sideDivision + 1;

        internalData = new T[xChunks * yChunks][];

        for (int i = 0; i < xChunks; i++)
        {
            for (int j = 0; j < yChunks; j++)
            {
                internalData[GetFirstIndex(i,j)] = new T[subWidth * subHeight];
            }
        }
    }

    private int GetFirstIndex(int i, int j)
    {
        return i + j * xChunks;
    }

    private int GetSecondIndex(int i, int j)
    {
        return i + j * subWidth;
    }

    public T get(int x, int y)
    {
        return internalData[GetFirstIndex(x / subWidth, y / subHeight)][GetSecondIndex(x % subWidth, y % subHeight)];
    }

    public void set(int x, int y, T value)
    {
        internalData[GetFirstIndex(x / subWidth, y / subHeight)][GetSecondIndex(x % subWidth, y % subHeight)] = value;
    }
}

为简单起见,我已将所有数据块设置为相同的大小,因此最后一个块有一些未使用的空间。