• 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 X$$anonymous$$L 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 X$$anonymous$$L.

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

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.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Coding help: How to use xml serialization 1 Answer

[Solved] Built in GUI continuously Loads through different Options. 1 Answer

The name "Game Foundation" does not exist in the current context 1 Answer

How do I save and load the state of the GameObjects with PlayerPrefs? 0 Answers

Saving GameObject to file 2 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