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 Nomataka · Jan 08, 2020 at 09:34 AM · serializationsavejsonfilesystem

Problem whith save system,Problem whith save code

Hello, I've been blocking for some time with a report to a backup system that I'm using here. My backup is functional when I am on the scene but when I change the scene or stop the application, the backup is not taken into account and my json file does not create either. If you have ideas, I am interested.

My script : GameController

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.IO;
 
 public class GameController : MonoBehaviour {
 
     public Button saveButton;
     public Button loadButton;
 
     public GameObject playerPrefab;
 
     private static string dataPath = string.Empty;
     void Awake()
     {
         dataPath = Path.Combine(Application.persistentDataPath, "MyTest.json");
     }
     void Start()
     {
     
        // CreateActor(playerPath, new Vector3(0, 1.6f, 0), Quaternion.identity);
        // CreateActor(playerPath, new Vector3(5, 1.6f, 0), Quaternion.identity);
         //CreateActor(playerPath, new Vector3(-5, 1.6f, 0), Quaternion.identity);
     }
 
     public static Actor CreateActor(string path, Vector3 position, Quaternion rotation)
     {
         GameObject prefab = Resources.Load<GameObject>(path);
 
         GameObject go = Instantiate(prefab, position, rotation) as GameObject;
 
         Actor actor = go.GetComponent<Actor>() ?? go.AddComponent<Actor>();
 
         return actor;
     }
 
     public static Actor CreateActor(ActorData data, string path, Vector3 position, Quaternion rotation)
     {
         Actor actor = CreateActor(path, position, rotation);
 
         actor.data = data;
 
         return actor;
     }
 
     public void Save()
     {
         SaveData.Save(dataPath, SaveData.actorContainer);
      
     }
 
     public void Load()
     {
         SaveData.Load(dataPath);
         Debug.Log(Application.persistentDataPath);
 
     }
 
   public  void OnEnable()
     {
         saveButton.onClick.AddListener(Save);
         loadButton.onClick.AddListener(Load);
     }
   public  void OnDisable()
     {
         saveButton.onClick.RemoveListener(Save);
         loadButton.onClick.RemoveListener(Load);
     }
 }
 

SaveData :

 using UnityEngine;
 using System.Collections;
 using System.Xml.Serialization;
 using System.IO;
 
 public class SaveData
 {
 
     public static ActorContainer actorContainer = new ActorContainer();
 
     public delegate void SerializeAction();
     public static event SerializeAction OnLoaded;
     public static event SerializeAction OnBeforeSave;
 
 
     public static void Load(string path)
     {
         actorContainer = LoadActors(path);
 
         OnLoaded();
 
         ClearActorList();
     }
 
     public static void Save(string path, ActorContainer actors)
     {
         OnBeforeSave();
 
         //ClearSave(path);
 
         SaveActors(path, actors);
 
         ClearActorList();
     }
 
     public static void AddActorData(ActorData data)
     {
         actorContainer.actors.Add(data);
     }
 
     public static void ClearActorList()
     {
         actorContainer.actors.Clear();
     }
 
     private static ActorContainer LoadActors(string path)
     {
         string json = File.ReadAllText(path);
 
 
         return JsonUtility.FromJson<ActorContainer>(json);
       
 
     }
 
     private static void SaveActors(string path, ActorContainer actors)
     {
         string json = JsonUtility.ToJson(actors);
 
         StreamWriter sw = File.CreateText(path);
         sw.Close();
      
 
         File.WriteAllText(path, json);
     }
 }
 

Actor :

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class Actor : MonoBehaviour {
  
     public ActorData data = new ActorData();
 
     public static GameController controller;
 
     void Awake()
     {
     }
     void Start()
     {
     //    playerstats = GameObject.FindGameObjectWithTag("Health").GetComponent<PlayerStats>();
     }
     public void StoreData()
     {
   
         data.pos = transform.position;
      //   data.playerstats = playerstats;
 
     }
 
     public void LoadData()
     {
         transform.position = data.pos;
 
     //    playerstats = data.playerstats;
      
       
     }
 
     public void ApplyData()
     {
         SaveData.AddActorData(data);
     
     }
 
     void OnEnable()
     {
         SaveData.OnLoaded += LoadData;
         SaveData.OnBeforeSave += StoreData;
         SaveData.OnBeforeSave += ApplyData;
     }
 
     void OnDisable()
     {
         SaveData.OnLoaded -= LoadData;
         SaveData.OnBeforeSave -= StoreData;
         SaveData.OnBeforeSave -= ApplyData;
     }
 
 }
 
 [Serializable]
 public class ActorData
 {
 
 
     public GameObject fps;
     public Vector3 pos;

ActorContainer :

 using UnityEngine;
 using System;
 using System.Collections.Generic;
 
 [Serializable]
 public class ActorContainer {
     public List<ActorData> actors = new List<ActorData>();
     
 
 }
 

,Hello, I've been blocking for some time with a report to a backup system that I'm using here. My backup is functional when I am on the scene but when I change the scene or stop the application, the backup is not taken into account and my json file does not create either. If you have ideas, I am interested.

My Actor script :

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class Actor : MonoBehaviour {
  
     public ActorData data = new ActorData();
 
     public static GameController controller;
 
     void Awake()
     {
     }
     void Start()
     {
     //    playerstats = GameObject.FindGameObjectWithTag("Health").GetComponent<PlayerStats>();
     }
     public void StoreData()
     {
   
         data.pos = transform.position;
      //   data.playerstats = playerstats;
 
     }
 
     public void LoadData()
     {
         transform.position = data.pos;
 
     //    playerstats = data.playerstats;
      
       
     }
 
     public void ApplyData()
     {
         SaveData.AddActorData(data);
     
     }
 
     void OnEnable()
     {
         SaveData.OnLoaded += LoadData;
         SaveData.OnBeforeSave += StoreData;
         SaveData.OnBeforeSave += ApplyData;
     }
 
     void OnDisable()
     {
         SaveData.OnLoaded -= LoadData;
         SaveData.OnBeforeSave -= StoreData;
         SaveData.OnBeforeSave -= ApplyData;
     }
 
 }
 
 [Serializable]
 public class ActorData
 {
 
 
     public GameObject fps;
     public Vector3 pos;


My actor container :

 using UnityEngine;
 using System;
 using System.Collections.Generic;
 
 [Serializable]
 public class ActorContainer {
     public List<ActorData> actors = new List<ActorData>();
     
 
 }
 

SaveData :

 using UnityEngine;
 using System.Collections;
 using System.Xml.Serialization;
 using System.IO;
 
 public class SaveData
 {
 
     public static ActorContainer actorContainer = new ActorContainer();
 
     public delegate void SerializeAction();
     public static event SerializeAction OnLoaded;
     public static event SerializeAction OnBeforeSave;
 
 
     public static void Load(string path)
     {
         actorContainer = LoadActors(path);
 
         OnLoaded();
 
         ClearActorList();
     }
 
     public static void Save(string path, ActorContainer actors)
     {
         OnBeforeSave();
 
         //ClearSave(path);
 
         SaveActors(path, actors);
 
         ClearActorList();
     }
 
     public static void AddActorData(ActorData data)
     {
         actorContainer.actors.Add(data);
     }
 
     public static void ClearActorList()
     {
         actorContainer.actors.Clear();
     }
 
     private static ActorContainer LoadActors(string path)
     {
         string json = File.ReadAllText(path);
 
 
         return JsonUtility.FromJson<ActorContainer>(json);
       
 
     }
 
     private static void SaveActors(string path, ActorContainer actors)
     {
         string json = JsonUtility.ToJson(actors);
 
         StreamWriter sw = File.CreateText(path);
         sw.Close();
      
 
         File.WriteAllText(path, json);
     }
 }
 

GameController :

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.IO;
 
 public class GameController : MonoBehaviour {
 
     public Button saveButton;
     public Button loadButton;
 
     public GameObject playerPrefab;
 
     private static string dataPath = string.Empty;
     void Awake()
     {
         dataPath = Path.Combine(Application.persistentDataPath, "MyTest.json");
     }
     void Start()
     {
     
        // CreateActor(playerPath, new Vector3(0, 1.6f, 0), Quaternion.identity);
        // CreateActor(playerPath, new Vector3(5, 1.6f, 0), Quaternion.identity);
         //CreateActor(playerPath, new Vector3(-5, 1.6f, 0), Quaternion.identity);
     }
 
     public static Actor CreateActor(string path, Vector3 position, Quaternion rotation)
     {
         GameObject prefab = Resources.Load<GameObject>(path);
 
         GameObject go = Instantiate(prefab, position, rotation) as GameObject;
 
         Actor actor = go.GetComponent<Actor>() ?? go.AddComponent<Actor>();
 
         return actor;
     }
 
     public static Actor CreateActor(ActorData data, string path, Vector3 position, Quaternion rotation)
     {
         Actor actor = CreateActor(path, position, rotation);
 
         actor.data = data;
 
         return actor;
     }
 
     public void Save()
     {
         SaveData.Save(dataPath, SaveData.actorContainer);
      
     }
 
     public void Load()
     {
         SaveData.Load(dataPath);
         Debug.Log(Application.persistentDataPath);
 
     }
 
   public  void OnEnable()
     {
         saveButton.onClick.AddListener(Save);
         loadButton.onClick.AddListener(Load);
     }
   public  void OnDisable()
     {
         saveButton.onClick.RemoveListener(Save);
         loadButton.onClick.RemoveListener(Load);
     }
 }
 

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

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

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

Writing binary files on Android 1 Answer

Saving GameObject to file 2 Answers

[C#] Saving settings to a local file and reading from it at runtime 1 Answer

Is there possible to save scene gameobject data on external file? (not in the registry) 2 Answers

Accessing local system ( File Browser ) 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