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 Fuzzyursa · Jan 15, 2014 at 05:42 PM · savelevel editorsave filegame savelevel-editor

I am making a level editor and need ideas on how to save the level file.

I have been creating my own level editor for my game and i have it to the point where you can make your level, but now i need a way to save the level to a file. i am not sure on the best way to do this and i am looking for suggestion. Basically all i need it to do is save the type of object, its position and scale and any variable associated with that object. Right now i am looking to be able to run my game on desktop and webplayer, but i am looking to migrate it to mobile later too, in case that matters. Thanks in advance for any and all help.

Comment
Add comment · Show 7
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 ABerlemont · Jan 15, 2014 at 06:02 PM 0
Share

What kind of level editor ? 2D/3D ? What kind of data to store ? Object + behaviours + components ? Basically : how complexe is it ?

avatar image Fuzzyursa · Jan 15, 2014 at 06:12 PM 0
Share

it is 3d but it is a 2d side scroller so i only care about 2 dimensions and it just needs to store what type of object(ie door, wall, character), its position and scale, and some other variables for certain objects.

avatar image OP_toss · Jan 15, 2014 at 08:09 PM 0
Share

Id suggest writing custom serializers for each class you care about. Then all you have to do is go through your objects and Serialize them and append to file. That or just go with a simple Json save/load method, but that's not as slick.

avatar image Fuzzyursa · Jan 15, 2014 at 08:15 PM 0
Share

I have seen a lot about the serialization, but I don't really understand how it works. Do you have any sites/tutorials that would explain it to me?

avatar image OP_toss · Jan 15, 2014 at 11:02 PM 0
Share

Basically you write 2 methods in your class. One that saves/Serializes, another that loads/Deserializes.

The loader/Deserializer will receive what the saver/Serializer writes as input. So you can assume some things about your input.

You can use whatever means you want, but I tend to build a dict or hashtable, then use a JSON lib to convert it to text. Simple, transferable, and uses standard .NET objects for building.

Say you have a complex building hierarchy that looks like this:

BuildingGrp |-Window |-Walls |--Door |-Stairs

You could have 1 script on the grp called Building, and another script on the door called Door. When it deserializes it, you could write out a list of lists (tree) representing your hierarchy to text. Then when you Deserialize it, you can rebuild the heirarchy with gameobjects, and add any necessary sub-scripts (Door).

Basically ins$$anonymous$$d of having one monolithic Save/Load method, you let each object do their own saving and loading. Then in your main save/load, you tell everyone else to save/load and compile it to one file. $$anonymous$$akes for much cleaner and simpler uses.

Also super useful when you want to transfer say a Building definition over a network to your other game clients. BOO$$anonymous$$, just serialize and send.

Hope I'm not confusing you more than helping...

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fabkins · Jan 15, 2014 at 06:40 PM

Here is something that saves the content of 100x100 tile map. Could be anything really. The format of the file is text and is the object type ("tile" only in this example") and then however many variable for your objects. This is separated by ":". Means you need to ensure you dont use ":" for anything else.

 function SaveMap(filepathIncludingFileName : String)
 {
     Debug.Log("Saving....");
     var sw : StreamWriter = new StreamWriter(filepathIncludingFileName);
     var y:int;
     for(y=0;y<100;y++)
     {
         var x=0;
         for(x=0;x<100;x++)
         {
                 sw.WriteLine("Tile:"+x.ToString()+":"+y.ToString()+":"+metaMap[x,y].tileType.ToString());
         }
     }
     sw.Flush();
     sw.Close();
     Debug.Log("Save Complete");
 }
  
 function LoadMap(filepathIncludingFileName : String) {
     Debug.Log("Loading....");
     var sr = new File.OpenText(filepathIncludingFileName);
  
     var input = "";
     while (true) 
     {
         input = sr.ReadLine();
         if (input == null) { break; }
         var paramaters = input.Split(":"[0]);
         switch( paramaters[0])
             {
             case "Tile":
                 var x: int=System.Int32.Parse(paramaters[1]);
                 var y: int=System.Int32.Parse(paramaters[2]);
                 var i: int=System.Int32.Parse(paramaters[3]);
                 SetInternalMap(x,y,i);
                 break;
             }
     }
     completeUVoperation();
     sr.Close();
     Debug.Log("Load Complete");
 }

Comment
Add comment · Show 6 · 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 Fuzzyursa · Jan 15, 2014 at 06:42 PM 0
Share

i have one question with this. Would it work if i deploy my game to a website?

avatar image Fabkins · Jan 15, 2014 at 07:55 PM 0
Share

No , you would need to post something on the server and have server side code to do it.

avatar image Fabkins · Jan 15, 2014 at 08:01 PM 0
Share

You would need to post a form with whatever metadata. Use WWWForm . I would create a random unique identifier that you could reference for retrieving the save files or have a log on method. If you had something unique you could store that in the PlayerPrefs. Shame you cant save a file in the same place that PlayerPrefs does.

If you had really small amount of data, you could always use PlayerPrefs, but not really designed for keep maps and the such.

avatar image Fuzzyursa · Jan 15, 2014 at 08:16 PM 0
Share

Ok cool thanks for you suggestions.

avatar image OP_toss · Jan 15, 2014 at 08:28 PM 0
Share

Can't you just write it to a text file on the server, then read it back?

For my game, I generate unique game ids using the current time + the game host name, as it is impossible for the same host to start multiple games at the same time. Then I salt it and md5 it to make it a little more abstracted.

Also, no PlayerPrefs is not something you want to save levels to. That'd be bad.

Show more comments

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

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

UnitySerializer Only Saving one Game Problem 0 Answers

Save File for Android Game.. 2 Answers

Save scene 1 Answer

Save Data Strategies 1 Answer

Save and Load 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