Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by unityplease · Apr 07, 2016 at 06:49 PM · serializationsaveload

Save a game scene to file at runtime with many dynamically instanced gameObjects in and textures

I've created an RPG with lots of dynamically instanced character game objects in, each with randomly generated names and textures, they even have dynamically generated sprite icons rendered from a camera as texture2d and saved as sprites in their root game object. Each character has a lot of stats and clothes/weapon/items each with their own meshes reference on the character.

Now I need to save the scene. I've built a save/load UI but after looking at many serialization tutorials none appear simple. The stock serialization to file unity tutorial doesn't even talk about serializing unity types and instead just talks about simple types.

I have put all my character and item references into a single script container.... how do I save the scene and reload it at runtime with all my characters , stats, items, textures and positions in place.

I hope it would be something simple like Application.saveScene(filename).... but alas no.

Comment
Add comment · Show 4
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 Jessespike · Apr 07, 2016 at 07:15 PM 0
Share

It won't be a easy one line solution. You'll need to write all of the data to a file, then during load, instantiate all of the needed objects and set their data.

Say you have 3 NPC,s each with a unique name. You'll write in a file that there are 3 npcs with those names. Then during load, you'll see that there was 3 NPCs, so you'll need to instantiate the 3 NPCs and set their names.

Unity asset store has many serialization/deserialization or file io solutions which will simplify the task. I prefer json, but it's up to your preference, there's nothing wrong with using xml, binary or whatever. Just some assets do things a bit differently, some are easier to understand while some are more complex to understand. I would suggest browsing around and see what they have to offer.

avatar image unityplease · Apr 07, 2016 at 08:18 PM 0
Share

I know its hard because I've spent the last week or so looking for a solution but the thing is I don't really know why it is so hard. I understand in some specific circumstances you may care about the memory size of the save game and so you perhaps wouldn't want to save everything including the scene objects... but for a simple blanket solution why can't you save the entire scenestate ?

It would make sense to save every gameobject at a particular time in a scene? Then to load it you'd just load the entire scene in the same way that unity saves and loads scene files and the objects in them at loadtime.

In your example about the 3 npcs why wouldn't the instances be saved completely with all their names and other parameters intact?

avatar image ChristopherSullivan44 · Aug 24, 2016 at 09:42 PM 0
Share

Hello, I have the same problem you originally had and I was wondering if you found a solution? Anything simple Unity offers which would solve it? Thanks

avatar image leegod · Sep 05, 2016 at 06:27 AM 0
Share

Hi. I need same save scene solution. Anyone knows something good?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by D3mon1zA · Sep 08, 2016 at 09:57 AM

it is alot easier than what it seems, bear with me it is a bit long, I hope this helps.

first step:

 using.UnityEngine;
 using.System;
 using.System.IO;
 using.System.Collections;
 using System.Runtime.Serialization.Formatters.Binary;

next declare all your variables (I have mine as public, as this is the core for my character):

 DateTime    now         = DateTime.Now;
 string      format      = "dd MM yyyy  hh:mm";
 string      loadDate;
 string      saveDate;
 
 public int  Level        = 1;
 public int  currentXP    = 0;
 public int  toNxtLvl     = 500;
 public int  health       = 5;
 public int  strength     = 1;
 public int  stanima      = 1;
 public int  agility      = 1;
 Vector3     playerPos;
 
 public static _GameControl control; //with this you can acces the variables in this class from another //script by going _GameControl.control.health;

now you need to put in this to ensure that the data in this script is stored between scenes and so you cant accidently spawn 2 of this class ( so if you change scene the original will change scene with you and will remove any copys in the new scene) to do that you need this:

 void Awake()
 {
     if (control == null)
     {
         DontDestroyOnLoad(gameObject);
         control = this;
     }
     else if(control != null)
     {
         Destroy(gameObject);
     }
 }

now you need a new class that can serialise your data which looks like this:

 [System.Serializable]
 class playerData

 {

 public int      level;
 public int      curXp;
 public int      toNxt;
 public int      health;
 public int      strength;
 public int      stanima;
 public int      agility;
 public Vector3  pPos;

//these all need to be public so you can access them through save and load }

now you need a Save Function:

 void Save()

 {
     playerPos = GameObject.Find("Player").transform.position;

     saveDate = (now.ToString(format));

     BinaryFormatter binary  = new BinaryFormatter();
     FileStream file         = File.Create(Application.persistentDataPath +"/SaveGame.dat"); //the creates a file in the unity app data path
     playerData dat          = new playerData();

     dat.level       = Level;    //this transfers the data you want to your serializable class
     dat.curXp       = currentXP;                      
     dat.toNxt       = toNxtLvl;                           
     dat.health      = health;                           
     dat.strength    = strength;
     dat.stanima     = stanima;
     dat.agility     = agility;
     dat.pPos        = playerPos;

     binary.Serialize(file, dat);
     file.Close();


and finally the load function which is pretty much the same as the save but kinda reversed:

 void Load()
 {
     if (File.Exists(Application.persistentDataPath + "/SaveGame.dat"))
     {
         BinaryFormatter binary  = new BinaryFormatter();
         FileStream file         = File.Open(Application.persistentDataPath + "/SaveGame.dat", FileMode.Open);
         playerData dat          = (playerData)binary.Deserialize(file);
         file.Close();

         Level       = dat.level; //this sets all the saved data back onto your character or npc's
         currentXP   = dat.curXp;
         toNxtLvl    = dat.toNxt;
         health      = dat.health;
         strength    = dat.strength;
         stanima     = dat.stanima;
         agility     = dat.agility;
         playerPos   = dat.pPos;
     }
 }



Comment
Add comment · Show 2 · 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 D3mon1zA · Sep 08, 2016 at 09:58 AM 0
Share

https://www.youtube.com/watch?v=yxziv4ISfys This video explains this alot better than I have haha

avatar image SkedgyEdgy · Oct 04, 2018 at 07:50 PM 0
Share

They're asking for a way to save the entire scene, not just the playerdata, but that does help a bit.

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

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

Storing the state of a collectible in save-file? 1 Answer

Debugging Save / Load Glitches? (Serialization) 1 Answer

Loading and Saving a Serializable Array. 1 Answer

problem with loading data with serialization in the right time 0 Answers

Why Does My Saved Data Not Load? 0 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