- Home /
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);
}
}
Your answer
Follow this Question
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