• 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 d112570 · May 21, 2014 at 10:00 PM · streamreader

Converting StreamWriter to StreamReader

 public static int [,,,,,] gridLocation = new int [10,10,10,10,27,7];

I have saved an multi array show on top using the following code.

 var path = Application.dataPath + "Save";
 if (!Directory.Exists (path)) Directory.CreateDirectory(path);
 using(StreamWriter sw = new StreamWriter(path + "/Beyond.txt")) foreach(var item in gridLocation) sw.WriteLine(item);

How do I reverse the process?

I tried this, is there an easier way? This one did not work, cannot convert string to integer. I don't even know if this code will work at all, was unable to test it. Is this the correct way? It would not let me convert to array nor integer.

 var path = Application.dataPath + "Save";
 if (!Directory.Exists (path)) Directory.CreateDirectory(path);
 using(StreamReader sr = new StreamReader(path + "/BeyondTemp.txt"))
             for (g = 0; g <=9; g++){ // Galaxy (g)
                 for (s = 0; s <=9; s++){ // Star (s)
                     for (p = 0; p <=9; p++){ // Planet (p)
                         for (m = 0; m <=9; m++){ // Satellite (m)
                             for (b = 0; b <=26; b++){ // GridBox (b)
                                 for (a = 0; a <=6; a++){ // Area (a)
                                     gridLocation[g,s,p,m,b,a] = sr.ReadLine();  // cannot convert string to int
                                 }}}}}}
Comment
Scribe

People who like this

1 Show 3
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 trojanfoe_ · May 22, 2014 at 06:57 AM 0
Share

Shouldn't you be using a class to hold all those different values, rather than a multi-dimensional array?

avatar image d112570 · May 22, 2014 at 09:38 AM 0
Share

umm what do you mean? I have 9 galaxies, each galaxy holds 9 stars, each star holds 9 planets and each planet holds 9 satellites. Each Planet and Satellites can hold a maximum of 27 building grids and each building grid has a total of 6 expansions. I use an int to represent a building number like 1 for command center, 2 for farming.

MyScript.setupGridList(1,1,4,3,1,1,1,5) means take me to Galaxy #1, star #4, planet #3, Satellite #1, Grid #1, and at area #1 has building type #5. And the first number represents 0 = Format, 1 = write, 2 = Read. I found this to be very fast, I didn't notice any slowdown even when saving to a txt format of 3,8 MB in size. Would there be a better way to do this, I am all open, this is the only one I could think of that worked.

avatar image trojanfoe_ · May 23, 2014 at 12:40 PM 0
Share

What I am suggesting is that you create Model objects for each of these data elements; for example:

public class Galaxy { public List < Star >  stars; ... }
so the manipulating these objects becomes easier. You could then use C# Serialization techniques to get the objects to/from file: http://msdn.microsoft.com/en-us/library/ms233843.aspx

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by kamgru123 · May 22, 2014 at 06:14 AM

You can convert a string to int by using int.Parse()

 string s = "123";
 int i = int.Parse(s);
Comment
Scribe

People who like this

1 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 d112570 · May 22, 2014 at 09:46 AM 0
Share

OK fixed that problem, after retrieving the data at the specific location I got building #5. Same results before and after saving so it works.

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 by June 9. 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

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

Related Questions

Save text files in web player 1 Answer

TextAsset read non txt files 3 Answers

Reading from a file not working on Android 0 Answers

Get txt out of web? 1 Answer

Im trying to read and split a txt file, then place them into a list, When I tested it, it only had added one entry to the list. Can you guys help me understand whats going wrong ? Thank you 1 Answer


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