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 /
avatar image
0
Question by MikGames · Jan 16, 2018 at 04:08 PM · transformsavexmlsavegame

Save entire Transform objects with childs

Hello,

I would like to save some Transform objects in a file (for example XML) including the state of the child objects of them (enabled/disabled).

I haven't got any idea how to realize this (so with saving the child objects and their states).

Thanks in advance for any helpful answer.

Sincerely MG

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 Cherno · Jan 16, 2018 at 08:52 PM 0
Share

You could take a look at the Unity Save Load Utility I wrote which does what you are asking for. If you choose to write a solution yourself, be aware that this is an extremely difficult and complex task and some things are even impossible to save and load in Unity (like animation states).

Unity Save Load Utility (Free!) - Save and load your data

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MikGames · Jan 30, 2018 at 06:37 PM

Thanks for all the answers and sorry for my late response!

I fixed this using a bool List with the same index as the childs have. So the states of the objects are saved in this list and loaded and set with starting the game.

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

Answer by OrionGamesInc · Jan 16, 2018 at 06:07 PM

For starters, you need a save system. To create a save system you can use a basic skeleton for one here:

 using System;
 using System.IO;
 using UnityEngine.Windows;
 using System.Collections;
 using System.Runtime.Serialization.Formatters.Binary;
 using UnityEngine;
 using UnityEngine.UI;
 
 public static class SaveLoadManager {
 
     public static void SavePlayer(Player player){
 
         BinaryFormatter bf = new BinaryFormatter();
         var createdir = Directory.CreateDirectory(Application.persistentDataPath + "/saves");
         String dir = Application.persistentDataPath + "/saves";
         PlayerData data = new PlayerData (player);
 
         DirectoryInfo dir_i = new DirectoryInfo(dir);
         FileInfo[] info = dir_i.GetFiles("*.wyrm");
         foreach (FileInfo f in info) {
             Debug.Log (f);
         }
 
         FileStream stream = new FileStream (dir + "/player" + (info.Length + 1) + ".wyrm", FileMode.Create);
 
         bf.Serialize (stream, data);
         stream.Close ();
     }
 
     public static float[] LoadPlayer(){
 
         String dir = Application.persistentDataPath + "/saves";
 
         DirectoryInfo dir_i = new DirectoryInfo(dir);
         FileInfo[] info = dir_i.GetFiles("*.save");
         foreach (FileInfo f in info) {
             Debug.Log (f);
         }
 
         if (info.Length >= 1) {
             
             BinaryFormatter bf = new BinaryFormatter ();
             var createdir = Directory.CreateDirectory(Application.persistentDataPath + "/saves");
             FileStream stream = new FileStream (dir + "/player" + (info.Length) + ".save", FileMode.Open);
             PlayerData data = bf.Deserialize (stream) as PlayerData;
 
             stream.Close ();
             return data.stats;
         } else {
             return null;
         }
     }
 }
 
 [Serializable] //serializable means it is able to be saved
 public class PlayerData {
 
     public float[] stats;
 
     public PlayerData(Player player){
         stats = new float[5]; //create an empty array and use it to store values
         stats [0] = player.Level; //you can change these into whatever you want to save, but it must be either a float or int.
         stats [1] = player.Experience;
         stats [2] = player.transform.position.x;
         stats [3] = player.transform.position.y;
         stats [4] = player.transform.position.z;
     }
 }



Now that you have a SaveLoadManager you must create a Save() and Load() method inside your Player script. If you want to save booleans as integers you can look up any tutorial on it, such as this: Stack Overflow Tutorial

 public void Save(){ //compress the data into Binary using the SaveLoadManager
         Debug.Log ("Saving data (player)");
 
         SaveLoadManager.SavePlayer (this); //you do not need a reference to the SaveLoadManager as it is not MonoBehavior
     }
     public void Load(){ //load the data from the file and set the values accordingly
         Debug.Log ("Loading data (player)");
 
         float[] loadedStats = SaveLoadManager.LoadPlayer (); //grab the list of values that are returned from loading and set them into a new array
 
         Level = (int) loadedStats [0]; //level is an int, so you can save ints as floats and load them back into ints
         Experience = loadedStats [1];
 
         transform.position = new Vector3 (loadedStats[2],loadedStats[3],loadedStats[4]); //this is how you can save your exact player position

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 MikGames · Jan 19, 2018 at 08:26 PM 0
Share

Thanks, but I've already got a save script structure. $$anonymous$$y problem now is, to save the Transform components because they are not serializable.

avatar image Cherno MikGames · Jan 19, 2018 at 11:49 PM 0
Share

You could use SerializationSurrogates for non-serializable types. For a Transform instance, you would need SSs for Vector3 and Quaternion (position, rotation and scale). SerializationSurrogates have been discussed here on UA countless times and you will even find complete examples for the types mentioned above.

avatar image
0

Answer by anant_1910 · Jan 16, 2018 at 04:33 PM

You can drag the transform object (the parent) and place it to the Project menu

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 MikGames · Jan 16, 2018 at 04:36 PM 0
Share

I'm not sure if you have unterstood my question correctly. So: I would like to save gameobjects in a simple file (like X$$anonymous$$L). Later I load them from the file and spawn them in the scene.

avatar image Noxury MikGames · Jan 16, 2018 at 06:02 PM 1
Share

You cannot save the actual gameobjects with all the instanced data. You need to write the relevant values in the file like position, rotation, scale and custom script variables. When the game started, simply cycle through each item, representing a gameObject, in the file and try to use prefabs and instance them and initialize the values to each prefab. You can also use JSON to initialize serializable classes of the attached scripts.

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

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

XML Encryption 1 Answer

How to save my serialized levels ? 1 Answer

Transform Position? 1 Answer

Mapping between classes for serialization 2 Answers

Saving gameobjects' properties for Dynamically growing prefabs 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