2017-04-11 203 views
2

我使用Unity的JSON序列化在Unity中創建一個簡單的保存遊戲系統。我的保存遊戲非常簡單,我只保存角色的位置並加載它。在Unity中使用JSON保存遊戲

保存遊戲在遊戲中正常工作。當我啓動遊戲並點擊「保存」時,遊戲將被保存,並且JSON文件將隨我的玩家位置而更新。當我點擊「加載」時,遊戲加載正常,我的玩家被移到先前保存的位置。

然而,我的問題是,每當我關閉遊戲並重新啓動遊戲時,'加載'功能只會將我的角色重置爲我在其中生成的位置。它不會在我以前保存的最後一場比賽中的位置加載角色。

現在爲我的代碼。我有抱我的演員的數據的演員類:

using UnityEngine; 
using System.Collections; 
using System; 

public class Actor : MonoBehaviour 
{ 

    public ActorData data = new ActorData(); // to store our data 

    public void StoreData() // for storing our data 
    { 
     data.pos = transform.position; 
    } 

    public void LoadData() // for loadinjg our data 
    { 
     transform.position = data.pos; 
    } 

    public void ApplyData() 
    { 
     SaveData.AddActorData(data); 
    } 

    void OnEnable() // to enable the save/load to avoid hanging 
    { 
     SaveData.OnLoaded += LoadData; 
     SaveData.OnBeforeSave += StoreData; 
     SaveData.OnBeforeSave += ApplyData; 
    } 

    void OnDisable() // to disable the save/load to avoid hanging 
    { 
     SaveData.OnLoaded -= LoadData; 
     SaveData.OnBeforeSave -= StoreData; 
     SaveData.OnBeforeSave -= ApplyData; 
    } 
} 

[Serializable] // for storing in JSON 
public class ActorData // our players attributes in JSON 
{ 
    public Vector3 pos; 
} 

我也有一個ActorContainer來保存數據,不同的玩家:

using System.Collections.Generic; 
using System; 

[Serializable] 
public class ActorContainer 
{ 

    // To store our different actors data 
    public List<ActorData> actors = new List<ActorData>(); 
} 

我執行我的序列化一個保存數據類:

using UnityEngine; 
using System.Collections; 
using System.IO; 

public class SaveData 
{ 

    // every actos will be sorted in this actor container when loading the game 
    public static ActorContainer actorContainer = new ActorContainer(); // static so that we dont have to recreacte savedata everytime 

    public delegate void SerializeAction(); 
    public static event SerializeAction OnLoaded; // happens after loading 
    public static event SerializeAction OnBeforeSave; // right before saving 

    public static void Load(string path) // where the load is 
    { 
     actorContainer = LoadActors(path); 

     OnLoaded(); 

     ClearActorList(); // so that there's no duplicates 
    } 

    public static void Save(string path, ActorContainer actors) 
    { 
     OnBeforeSave(); // to throw of the data that was in OnBeforeSave 

     SaveActors(path, actors); // store all of our actors as json in the file 

     ClearActorList(); // clear the list so that the next time we save theres no duplicates 
    } 

    public static void AddActorData(ActorData data) 
    { 
     actorContainer.actors.Add(data); 
    } 

    public static void ClearActorList() // to avoid duplicate data 
    { 
     actorContainer.actors.Clear(); // access actors and clear it 
    } 

    private static ActorContainer LoadActors(string path) // path to the JSON 
    { 
     string json = File.ReadAllText(path); // loading all the text out of the file into a string, assuming the text is all JSON 

     return JsonUtility.FromJson<ActorContainer>(json); // parse the JSON into an ActorContainer type and return in the LoadActors 
    } 

    private static void SaveActors(string path, ActorContainer actors) // where to save and the actors to save 
    { 
     string json = JsonUtility.ToJson(actors); 

     StreamWriter sw = File.CreateText(path); // if file doesnt exist, make the file in the specified path 
     sw.Close(); 

     File.WriteAllText(path, json); // fill the file with the data(json) 
    } 
} 

最後我有一個GameController類,用作使用Unity溝通:

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 
using System.IO; 

public class GameController : MonoBehaviour 
{ // to tell unity what it has to do, a communicator to be used with unity 

    // trigger buttons 
    public Button saveButton; 
    public Button loadButton; 

    private static string dataPath = string.Empty; 

    void Awake() 
    { 
     dataPath = System.IO.Path.Combine(Application.persistentDataPath, "actors.json"); // persistentDataPath unity save game path, actors.json name of the file containing actors 
    } 

    // for button 
    public void Save() 
    { 
     SaveData.Save(dataPath, SaveData.actorContainer); 
    } 

    // for button 
    public void Load() 
    { 
     SaveData.Load(dataPath); 
    } 
} 

我的JSON文件actors.json包含以下內容:

{ 「演員」:[{ 「POS」:{ 「×」:419.6240539550781, 「Y」:5.0189208984375, 「Z」:0.0} }]}

歡迎任何建議。

+0

你從來沒有加載的值賦給''中Actor.cs' data.pos'。 – Smartis

回答

1

所以問題很簡單,你永遠不會從JSON分配加載的值到data.posActor.cs。相反,您可以從未更改的公共字段ActorData data加載它。

如果我能看到這對你有以下的Actor.cs改變:

using System.Linq; 

// .. 

public void LoadData() 
{ 
    // guessing you will implementing an identifier for the separate actors later.. 
    var currentActor = SaveData.actorContainer.First(); 
    transform.position = currentActor.pos; 

    // also set the loaded data to the field 
    data = currentActor; 
} 

// .. 
+0

你絕對正確!謝謝:) – arjwolf

+0

@arjwolf很高興我能幫忙! :d – Smartis