직렬화 문제가 있습니다. 간단한 3D 데이터 블록을 저장하고로드하고 싶습니다. 3D의 정수 배열뿐만 아니라 차원 (너비, 높이 및 길이)을 포함하는 클래스가 있습니다. JSON은 꽤 행복하게 클래스를 문자열로 변환하여 저장합니다.하지만 다시 변환 할 때 멋지게 재생되지는 않습니다.JSON을 사용하는 동안 클래스의 배열 문제
데이터 클래스 :
Public class cClusterData {
public int mWidth;
public int mHeight;
public int mLength;
public int[,,] mCellType;
public cClusterData()
{
mCellType = new int[32,32,32];
}
}
루틴 저장이 :
public void SaveCluster()
{
cClusterData lData = new cClusterData();
lData.mWidth = mWidth;
lData.mHeight = mHeight;
lData.mLength = mLength;
for (int lX = 0; lX < mWidth; lX++)
{
for (int lY = 0; lY < mHeight; lY++)
{
for (int lZ = 0; lZ < mLength; lZ++)
{
lData.mCellType[lX,lY,lZ] = (int)mCell[lX,lY,lZ].mType;
}
}
}
string lDataString = LitJson.JsonMapper.ToJson(lData);
cFileUtils.WriteStringToFile("TestCluster", lDataString);
Debug.Log("Done saving");
}
가 다시 다시로드 기능 :
public void LoadCluster()
{
string lDataString = cFileUtils.LoadStringFromFile("TestCluster");
cClusterData lData = new cClusterData();
lData = LitJson.JsonMapper.ToObject<cClusterData>(lDataString);
Debug.Log("Loaded header " + lDataString);
// convert cluster data to actual cluster
mWidth = lData.mWidth;
mHeight = lData.mHeight;
mLength = lData.mLength;
CreateBlankCluster();
for (int lX = 0; lX < mWidth; lX++)
{
for (int lY = 0; lY < mHeight; lY++)
{
for (int lZ = 0; lZ < mLength; lZ++)
{
mCell[lX,lY,lZ].SetType((cCell.eCellType)lData.mCellType[lX,lY,lZ]);
}
}
}
}
모두의 벌금 그것까지 lData.mCellType에 액세스하려고하면 NullReferenceException이 throw됩니다.
는 NullReferenceException : ElementAddr (개체, int, int, int)에
이방법을 포함하는 것입니다 내 생각 엔이 배열 인 : 개체 참조가 개체 (관리 - 투 - 관리되는 래퍼) 개체의 인스턴스로 설정되지 않았습니다 생성자에서 설정하고 나는 어딘가에 선을 놓치고 있습니다. 그러나 나는 그것을 해결할 수 없다. 가져와, internets!