Hi,
I want to save and load a 3 dimensional(multidimensional array) int array, and it contains all my world info. And have been searching for hours, with no luck.
Hi,
I want to save and load a 3 dimensional(multidimensional array) int array, and it contains all my world info. And have been searching for hours, with no luck.
Though we could use a bit more information, I think that there is a generic solution that could avail you. C# can serialize data to files, and deserialize it when reading. This has the upside of making the data compact, and the downside of not allowing it to be human readable.
Serialized
Here’s a link that should get you started with serialization. There’s a bit of a learning curve, but if you’re worried about performance, you can go this route pretty easily.
XML
If you want human-readable code, think about using XML. Here’s a google start for the syntax.
For the schema, assuming that each dimension represents a location in the scene graph, something simple like this could work:
<world>
<worldX>
<worldY>
<worldZ item="car"/>
<worldZ/>
</worldY>
<worldY>
<worldZ/>
<worldZ item="boat"/>
</worldY>
</worldX>
<world>
Where your scene has a car at
world[0][0][0]and a boat at
world[0]['1]['1].
sorry for the apostrophes above, the markup was assuming I was trying to write a link.
Like I said, I can only make assumptions based on the limited information you give, but those are the two generic solutions which occur to me.
You have to write a save/load function. Use System.IO to write a formatted file, or you can use Unity’s PlayerPrefs if you want it ‘more hidden’ from the user.
Okay, after hours of hairpulling i got it to work, starting by making the 3 dimensional array to a 1d, by doing this:
private int CalculateNewPos(int x, int y, int z)
{
return z + y * size.z + x * size.z * size.y;
}
private int[] CalculateOldPos(int pos)
{
int x = 0;
int y = 0;
int z = 0;
if (pos >= size.y * size.z)
{
x = pos / (size.y * size.z);
pos %= size.y*size.z;
}
if (pos >= size.z)
{
y = pos / size.z;
pos %= size.z;
}
z = pos;
return new int[]{x, y, z};
}
Then after i got a 1 dimensional array i could just use filestream and BinaryFormatter to get it to save and load.