• 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 /
This question was closed Feb 21, 2014 at 02:49 PM by ICHeeryI for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by ICHeeryI · Feb 20, 2014 at 05:28 PM · writing to text

Saving/Loading 3 dimensional int

How to write to disk 3D byte/int? Example:

  int[,,] saveThis;

Thanks!

Edit: I could only save 1 dimension...Also how can I read it from file?

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

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by whydoidoit · Feb 21, 2014 at 08:39 AM

You should check out this article: http://unitygems.com/saving-data-1-remember-me/

Comment
Add comment · Show 10 · 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 ICHeeryI · Feb 21, 2014 at 09:15 AM 0
Share

Thanks! I did it. But how could I save to the same file more than one variable?

avatar image whydoidoit · Feb 21, 2014 at 09:21 AM 0
Share

Put them in a class and save that.

avatar image ICHeeryI · Feb 21, 2014 at 09:32 AM 0
Share

But I have more objects with that variable. I want to save for every object their variable in new line, also how could I then read it from file.

avatar image whydoidoit · Feb 21, 2014 at 10:01 AM 0
Share

So you'd make an array of them and save that perhaps. I'd need a bit more detail about what you are trying to do - I'm kinda guessing at the moment.

The principle is to create an in memory class which holds all of the items you want and save it.

However, you can just call Serialize and Deserialize multiple times on the same file and it will append the next object to the output or read it from the input.

avatar image whydoidoit · Feb 21, 2014 at 01:21 PM 1
Share

Are you trying to create it twice then? You need to have closed the previous one first.

What I am suggesting is:

  • Create the file

  • Write each thing to it by using BinaryFormatter.Serialize in a for loop or creating a single class and writing that

  • Close the file

Show more comments
avatar image
0

Answer by casimps1 · Feb 20, 2014 at 08:46 PM

If you're already working with a byte array, you can just do this:

 using System;
 using System.IO;
 
 FileStream fs = File.Create( Path.Combine( Application.persistentDataPath, "file.dat") );
 fs.Write( byteArray, 0, bytes.Length );        
 fs.Close( );

Then, you can read the bytes back in a similar way:

 FileStream fs = new FileStream( Path.Combine( Application.persistentDataPath, "file.dat" ), FileMode.Open );
 byte[] byteArray= new byte[ fs.Length ];
 fs.Read( byteArray, 0, Convert.ToInt32( fs.Length ) );
 fs.Close( );
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 ICHeeryI · Feb 20, 2014 at 09:34 PM 0
Share

The best overloaded method match for `System.IO.Stream.Write(byte[], int, int)' has some invalid arguments

That's the error I had. I'm using byte like this:

 byte[,,] saveThis;

And the code wants:

  byte[]

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Writing to text files 2 Answers

how to get Inputs from touch keyboard... 2 Answers

How To Update a Specific Line with StreamWriter? 1 Answer

Porting to Windows Store syntax problems 0 Answers

In-game scripting implementation 0 Answers

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