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 JtheSpaceC · Dec 04, 2014 at 06:46 PM · savingserializablepersistence

Serializing a GameObject, or otherwise saving a scene

Hello.

I've done a hefty amount of googling and searching on Answers for this one. My problem is that I want to save a scene as it is (think of Total War's strategy map), go fight a battle (like Total War battle using the armies from the strategy map), then come back to the strategy map as it was, except with some units now dead or injured.

I've done tutorials on serialization and can save things like a simple health value, but to be practical I really need to save the scene as it is, or save a 'bucket' with every GameObject that's in the hierarchy in it.

Here's my script for the latter attempt: using UnityEngine; using System.Collections; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using System.Collections.Generic;

 public class SaveAndLoad : MonoBehaviour {
 
     public static SaveAndLoad saveandload;
     
     public bool usingSaveA = true;
     private int SAVEaORb;
     private int LOADaORb;
     public List<GameObject> saveBucket;
 
     void Awake()
     {
         if(saveandload == null)
         {
             DontDestroyOnLoad(gameObject);
             saveandload = this;
         }
         else if (saveandload != this)
         {
             Destroy(gameObject);
         }
     }
 
     public void SaveTemporary()
     {
         if(usingSaveA)
         {
             SAVEaORb = 1;
             usingSaveA = false;
             LOADaORb = 1;
         }
         else 
         {
             SAVEaORb = 2;
             usingSaveA = true;
             LOADaORb = 2;
         }
         BinaryFormatter bf = new BinaryFormatter ();
         FileStream file = File.Create (Application.persistentDataPath + "/tempSave" + SAVEaORb + ".dat");
 
         STRGameData data = new STRGameData ();
         //PUT DATA BELOW HERE IN THE FORM data.xy&z = xy&z
 
         foreach (GameObject obj in Resources.FindObjectsOfTypeAll<GameObject>())
         {
             saveBucket.Add (obj);
         }
         data.saveBucket = saveBucket;
 
         bf.Serialize (file, data);
         file.Close ();
     }
 
     public void LoadTemporary()
     {
         if(File.Exists(Application.persistentDataPath + "/tempSave" +SAVEaORb +".dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/tempSave" +LOADaORb + ".dat", FileMode.Open);
             STRGameData data = (STRGameData)bf.Deserialize(file);
             file.Close();
 
             //PUT DATA BELOW HERE IN THE FORM xy&z = data.xy&z
             saveBucket = data.saveBucket;
         }
     }
 
 }//end of MONOBEHAVIOUR
 
 [Serializable]
 class STRGameData
 {
     public List<GameObject> saveBucket;
 }
 

NOTE: the 'saveAorB' is set up just to allow me to have two fallback autosaves, instead of one. Like HalfLife did.

Apparently GameObjects aren't serializable though, according to my research and since I'm getting this error: alt text

Without resorting to plugins (which I am aware of but want to try do this the "correct" way) what options have I? Can I make GameObjects serializable? Can I save an entire scene the way it is at a moment in time?

Thanks Kevin

error.png (34.6 kB)
Comment
Add comment · Show 1
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 MarksTeq · Dec 04, 2014 at 09:46 PM 0
Share

see this answer: Serialise Code

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TSRajesh · Dec 04, 2014 at 09:41 PM

Short answer is.. NO. I recently went thru the same phase.. - All Static objects need not be saved. That are part of scene file. - In dynamic objects, we may want to just save some parameters (like position etc.. ?)

Ultimately I ended up maintaining a "slimmed down SnapShot" of the scene that i want to restore. Yes, there are some plugins (assets / scripts) available.. I tried it. But somehow, I don't get the performance i needed..

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Serializing lists 0 Answers

Save changes made by player in level 1 Answer

Scriptable objects become volatile on android 1 Answer

Persistence of saving data not working 2 Answers

How to make it so Unity remembers purchases made by players whenever they play? (C#) 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