Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Hertzole · Nov 13, 2014 at 08:28 AM · lineread

Set a variable using ReadLine()

Hello!

I am working on a level editor and it is going really well. But now I need to save the level author and the game time in the saved file and read it. I can save the game time and author but when it comes to loading it back in, I can not get it to work. I save my file with "file.WriteLine(gameTime)" and "file.WriteLine(levelAuthor)".

If anyone of you know how I could load it back it and assign the gameTime and levelAuthor variables with this, please tell me!

Thanks in advance!

EDIT: I added the file that it generates a bit of my code

The file it generates. The names in front of the values are not really a part of the file. I added them so you can see what they are

 Game Time 120
 Level Author Hertzole
 Object ID 0
 Pos X 0
 Pos Y 0
 Pos Z 0
 Rot X 0
 Rot Y 0
 Rot Z 0
 Rot W 1
 0
 0
 0
 Color R 1
 Color G 1
 Color B 1
 Lenght 1
 Width 1
 Height 1

Now this is the huge chunk of code that I am using to read the text

 currentRead = "s";
             
             //Loading objects.
             while(currentRead != "TerrainBlock"&& currentRead != null){
                 currentRead = file.ReadLine();
                 //Number of lines each object uses, amount of writes in for loop up above ^.
                 if(i < 17){
                     //Keep reading down the lines
                     i ++;    
                 }
                 //We've finished reading an object, time to create it.
                 else{
                     //Build a new object.
                     GameObject tempObject = Instantiate(levelEditorSettings.objectList[tempObjectId], tempPosition, tempRotation) as GameObject;
 
                     //Run the adjust object function in new object.
                     if(!tempObject.GetComponent<CubeBlock>().isLight) tempObject.GetComponent<CubeBlock>().AdjustObject(tempWidth,tempHeight,tempLength, tempRotation.eulerAngles.x,tempRotation.eulerAngles.y,tempRotation.eulerAngles.z);
                 
                     //Set object id so the object can be saved again.
                     tempObject.GetComponent<CubeBlock>().objectId = tempObjectId;
     
                     //Reset the counter.
                         i = 1;    
                 }
 
                 //Depending on which line of the object we are on, what we do. 
                 switch(i){
                     case 1:
                     //1st line grabs object id.
                     if(currentRead != "TerrainBlock" && currentRead != null) tempObjectId = int.Parse(currentRead);
                     break;
                     
                 case 2:
                     //2st line grabs x position.    
                     tempPosition = new Vector3(float.Parse(currentRead),0,0);
                     break;
                     
                 case 3:
                     //3st line grabs y position.    
                     tempPosition = new Vector3(tempPosition.x,float.Parse(currentRead),0);
                     break;
                     
                 case 4:
                     ////4st line grabs z position.    
                     tempPosition = new Vector3(tempPosition.x, tempPosition.y,float.Parse(currentRead));
                     break;
                     
                 case 5:
                     //5st line grabs x rotation.
                     tempRotation = new Quaternion(float.Parse(currentRead),0,0,0);
                     break;
                     
                 case 6:
                     //6st line grabs y rotation.
                     tempRotation = new Quaternion(tempRotation.x,float.Parse(currentRead),0,0);
                     break;
                     
                 case 7:
                     //7st line grabs z rotation.
                     tempRotation = new Quaternion(tempRotation.x,tempRotation.y,float.Parse(currentRead),0);
                     break;
                     
                 case 8:
                     //8st line grabs w rotation.
                     tempRotation = new Quaternion(tempRotation.x,tempRotation.y,tempRotation.z,float.Parse(currentRead));
                     break;
                 case 9:
                     //9st line grabs material id.
                     break;
                 case 10:
                     //10st line grabs painted bool.
                     break;
                 case 11:
                     //Grabs paint age or spot angle.
                     tempSpotAngle = float.Parse(currentRead);
                     break;
                     
                 case 12:
                     //Grabs color R.
                     tempColor.r = float.Parse(currentRead);
                     break;
                     
                 case 13:
                     //Grabs color G.
                     tempColor.g = float.Parse(currentRead);
                     break;
                     
                 case 14:
                     //Grabs color B.
                     tempColor.b = float.Parse(currentRead);
                     break;
                 case 15:
                     //Grabs Length.
                     tempLength = float.Parse(currentRead);
                     tempLightType = int.Parse(currentRead);
                     break;
                 case 16:
                     //Grabs Width.
                     tempWidth = float.Parse(currentRead);
                     tempRange = float.Parse(currentRead);
                     break;
                 case 17:
                     //Grabs Height.
                     tempHeight = float.Parse(currentRead);
                     tempIntensity = float.Parse(currentRead);
                     break;
                 }
                 
              }
             // close the file
             file.Close();
Comment
Add comment · Show 2
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 Hertzole · Nov 13, 2014 at 08:19 AM 0
Share

@robertbu I have added the file it generates and the code I am using to read it.

avatar image CHPedersen · Nov 13, 2014 at 09:40 AM 1
Share

I strongly recommend you look into serializing your object either to binary or to X$$anonymous$$L, and then restore it on load using deserialization. Your current method is error prone and the switch statement you're using to restore the data looks like it might grow so big with time that it becomes difficult to maintain it.

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Line Reading 1 Answer

Unity iPhone - Using the file system in my app/game? 1 Answer

Saving My World 1 Answer

System.IO documentation? 2 Answers

XmlException when trying to read/write XML from iOS device 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