• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Xnary · Dec 07, 2012 at 07:02 PM · c#saveloadfilemultidimensional array

Saving world info

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Xnary · Dec 10, 2012 at 11:05 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FlowStateGames · Dec 11, 2012 at 10:50 AM 0
Share

Thanks for adding the final solution, much appreciated. It would be interesting to know why you chose this.

avatar image Xnary · Dec 11, 2012 at 12:04 PM 0
Share

Because i have so many object to save, that if i used XML like you recommended, it would have taken up a lot more hard disk space. If i where only to save a few objects with float precision, i maybe would have chosen XML.

avatar image FlowStateGames · Dec 11, 2012 at 12:08 PM 0
Share

Out of curiosity, did you look into serializing? I'm curious if it's more efficient. If it's not (which I doubt), it would also 'hide' the data from the player.

avatar image
0

Answer by FlowStateGames · Dec 10, 2012 at 09:31 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xnary · Dec 10, 2012 at 09:36 PM 0
Share

Thanks for your comment. And you're right, i didn't give enough information. But your method would have been great for a small worlds, but some information i forgot to include is that i need to save 8'750'000 objects, with an id(int) and the position is all ready written to the multidimensional array. Since i don't need more precision than an int can give me.

avatar image
0

Answer by Loius · Dec 10, 2012 at 09:03 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xnary · Dec 10, 2012 at 09:05 PM 0
Share

Thanks for the reply, i ended doing something like that. I will write the answer in a second.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

create text file in C#. 2 Answers

Using EditorUtility.OpenFilePanel outside editor? 1 Answer

C# Issues with either PlayerPrefs.SetVariable or UnityEvent.Invoke 1 Answer

Saving GameObject to file 2 Answers

Trouble creating a text file 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges