• 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
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

People who like this

0 Show 0
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

  • Sort: 
avatar image
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

People who like this

0 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

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

People who like this

0 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

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

People who like this

0 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.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Save and Load data from Local XML file won't work 1 Answer

Trouble creating a text file 4 Answers

Loading data from a txt file - C# 3 Answers

Only able to save inside the editor problem 2 Answers

Can't get serialization Load/Save to work!C# 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges