13-06-2016, 05:00 PM
Ho un serio problema: precedentemente ho creato vari script globali necessari per il sistema di salvataggio, ad esempio per creare delle List dove si elencano tutti gli oggetti raccolti e non, o dove si elencano tutte le porte aperte o chiuse, e così via...
Invece lo script del sistema di salvataggio è questo:
Il problema è che a volte gli oggetti non vengono caricati correttamente (tipo un item che nonostante fosse stato raccolto ancora è presente nella scena, o una porta ancora chiusa benché sia stata aperta).
Quale potrebbe essere il problema?
Invece lo script del sistema di salvataggio è questo:
Codice:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.SceneManagement;
public class SaveSystem : MonoBehaviour {
GameObject player;
GameObject cam;
GameObject start;
GameObject glob;
GameObject inv;
Start_Scene startScript;
Global_ItemInventory globItem;
Global_Entities globEntity;
Global_TriggerEvents globTrigger;
Global_ScriptedEvents globEvent;
Inventory_System invScript;
public string savePoint;
public IEnumerator waitLoadScene;
private Scene scene;
void Update()
{
player = GameObject.Find ("PlayerController");
cam = GameObject.Find ("Camera");
start = GameObject.Find ("Start Scene");
glob = GameObject.Find ("GlobalParameters");
inv = GameObject.Find ("Inventory");
startScript = start.GetComponent<Start_Scene> ();
globItem = glob.GetComponent <Global_ItemInventory> ();
globEntity = glob.GetComponent <Global_Entities> ();
globTrigger = glob.GetComponent <Global_TriggerEvents> ();
globEvent = glob.GetComponent<Global_ScriptedEvents> ();
invScript = inv.GetComponent<Inventory_System> ();
}
public void SaveData ()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/Data.dat");
PlayerData data = new PlayerData ();
scene = SceneManager.GetActiveScene ();
data.sceneName = scene.name;
data.itemID = new int [globItem.itemSt.Count];
data.itemState = new string [globItem.itemSt.Count];
data.itemName = new string [globItem.itemSt.Count];
for (int i = 0; i < globItem.itemSt.Count; i++)
{
data.itemID [i] = globItem.itemSt [i].ID;
data.itemState [i] = globItem.itemSt [i].State;
data.itemName [i] = globItem.itemSt [i].Name;
}
globItem.SaveData ();
data.maxInvSlots = invScript.maxSlots;
data.slotID = new int[invScript.maxSlots];
for (int i = 0; i < invScript.maxSlots; i++)
{
data.slotID [i] = invScript.itemObj [i].ID;
}
invScript.SaveState ();
data.doorID = new int [globEntity.doorSt.Count];
data.doorState = new string [globEntity.doorSt.Count];
data.lockState = new string [globEntity.doorSt.Count];
data.doorName = new string [globEntity.doorSt.Count];
for (int i = 0; i < globEntity.doorSt.Count; i++)
{
data.doorID [i] = globEntity.doorSt [i].ID;
data.doorState [i] = globEntity.doorSt [i].DoorState;
data.lockState [i] = globEntity.doorSt [i].LockState;
data.doorName [i] = globEntity.doorSt [i].Name;
}
data.slidoorID = new int [globEntity.slidedoorSt.Count];
data.slidoorState = new string [globEntity.slidedoorSt.Count];
data.slilockState = new string [globEntity.slidedoorSt.Count];
data.slidoorName = new string [globEntity.slidedoorSt.Count];
for (int i = 0; i < globEntity.slidedoorSt.Count; i++)
{
data.slidoorID [i] = globEntity.slidedoorSt [i].ID;
data.slidoorState [i] = globEntity.slidedoorSt [i].DoorState;
data.slilockState [i] = globEntity.slidedoorSt [i].LockState;
data.slidoorName [i] = globEntity.slidedoorSt [i].Name;
}
globEntity.SaveData ();
data.triggerID = new int [globTrigger.trgDoorSt.Count];
data.triggerState = new string [globTrigger.trgDoorSt.Count];
data.triggerName = new string [globTrigger.trgDoorSt.Count];
for (int i = 0; i < globTrigger.trgDoorSt.Count; i++)
{
data.triggerID [i] = globTrigger.trgDoorSt [i].ID;
data.triggerState [i] = globTrigger.trgDoorSt [i].State;
data.triggerName [i] = globTrigger.trgDoorSt [i].Name;
}
globTrigger.SaveData ();
data.eventName = new string [globEvent.eventSt.Count];
data.eventState = new string[globEvent.eventSt.Count];
for (int i = 0; i < globEvent.eventSt.Count; i++)
{
data.eventName [i] = globEvent.eventSt [i].Name;
data.eventState [i] = globEvent.eventSt [i].State;
}
globEvent.SaveData ();
data.savePointName = savePoint;
bf.Serialize (file, data);
file.Close ();
}
public void LoadData()
{
if(File.Exists (Application.persistentDataPath + "/Data.dat"))
{
waitLoadScene = loading();
StartCoroutine (waitLoadScene);
}
}
IEnumerator loading ()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/Data.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close ();
PlayerPrefs.DeleteAll ();
SceneManager.LoadScene (data.sceneName, LoadSceneMode.Single);
AsyncOperation async = SceneManager.LoadSceneAsync (data.sceneName);
while (!async.isDone)
{
yield return null;
}
startScript.LoadDataState (data.savePointName);
globItem.LoadData (data.itemID, data.itemState, data.itemName);
invScript.LoadData (data.maxInvSlots, data.slotID);
globEntity.LoadData (data.doorID, data.doorState, data.lockState, data.doorName, data.slidoorID, data.slidoorState, data.slilockState, data.slidoorName);
globTrigger.LoadData (data.triggerID, data.triggerState, data.triggerName);
globEvent.LoadData (data.eventName, data.eventState);
StopCoroutine (waitLoadScene);
}
}
[Serializable]
class PlayerData
{
public string sceneName;
public string savePointName;
public int[] doorID;
public string[] doorState;
public string[] lockState;
public string[] doorName;
public int[] slidoorID;
public string[] slidoorState;
public string[] slilockState;
public string[] slidoorName;
public int[] triggerID;
public string[] triggerState;
public string[] triggerName;
public string[] eventName;
public string[] eventState;
public int[] itemID;
public string[] itemState;
public string[] itemName;
public int maxInvSlots;
public int[] slotID;
}
Quale potrebbe essere il problema?