unity3d Unity +油墨/油墨:如何加载已保存的故事状态JSON

wtlkbnrh  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(65)

所以在我们使用ink/inkle API for unity的视觉小说游戏中,我试图实现一个保存和加载系统,但是我目前在以我想要的方式加载保存的状态时遇到了麻烦。
根据API文档,要在游戏中保存故事的状态,可以调用:

string savedJson = _inkStory.state.ToJson();

然后加载:

_inkStory.state.LoadJson(savedJson);

下面是我如何使用PlayerPrefs保存故事的当前状态,它工作得很好。

public void saveGame()
{

    DialogueSystem dialogueSystem = GetComponent<DialogueSystem>();
    dialogueSystem.savedJson = dialogueSystem.dialogue.state.ToJson();
    PlayerPrefs.SetString("saveState", dialogueSystem.savedJson);

    Debug.Log(dialogueSystem.savedJson);
}

下面是我如何尝试在Start()上加载保存的状态,它不会加载保存的状态。

private void Start()
{
    dialogue = new Story(dialogueJSON.text);
    currentActiveBG = BGs[0]; 
    historyScript = GetComponent<History>();
    counterScript = GetComponent<counter>();
    
    if (PlayerPrefs.HasKey("saveState")) { // for loading the saved state

        dialogue = new Story(dialogueJSON.text);
        savedJson = PlayerPrefs.GetString("saveState");
        dialogue.state.LoadJson(savedJson);
        loadLine(); //this function contains story.Continue, which loads the next line
        Debug.Log(savedJson);
    }

    else {
        loadGame("Pre_Prep1"); // default starting line

    }

}

我怎样才能使这个工作?有没有更好的方法来实现这个保存/加载特性?

9udxz4iz

9udxz4iz1#

我不知道你要做哪个游戏,但是,我在我的项目中使用了下面的保存系统,它工作得很好,你也可以使用它。

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Saving System
public class UserData{
 #region Save Data Var's 
 // file Path
 private static readonly string FILE_PATH = Application.dataPath + 
 "/SaveFiles";
 public  int carToLoad;
 public  int levelToLoad;
 public  int unlockLevels;
 #endregion

 #region Methods

 // Save
 public static void SaveData(string contents){ // for Car Selection 
     File.WriteAllText(FILE_PATH + "/UserData.txt",contents);
 }

 // Load
 public static UserData LoadData(){
    string _data = File.ReadAllText(FILE_PATH + "/UserData.txt");
    return JSONToUserData(_data);
 }

 // Init 
 public static void UserData_init(){
    CheckFilesPreviousData();
    CheckDirectory();
 }

 // Selection Level Data 
 public static void SaveData(UserData LevelData) { // for Level 
    UserData tempObj = LoadData();
    tempObj.levelToLoad = LevelData.levelToLoad;
    SaveData(UserDataToJSON(tempObj));
 }

 // UnLock Level Data
 public static void SaveUnLockedLevelData(int LevelValue){
    UserData tempObj = LoadData();
    tempObj.unlockLevels = LevelValue;
    File.WriteAllText(FILE_PATH + "/UnLockLevels.text", 
 UserDataToJSON(tempObj));
}

 // Load UnLock Level Data
 public static UserData LoadUnLockLevelData(){
    string _data= File.ReadAllText(FILE_PATH+"/UnLockLevels.text");
    UserData tempObj = JSONToUserData(_data);
    return tempObj;
 }
 #endregion

#region Helper Methods
// !! important
 public static UserData JSONToUserData(string path) => 
  JsonUtility.FromJson<UserData>(path);// JSON to Obj
public static string  UserDataToJSON(UserData obj) => JsonUtility.ToJson(obj);// 
 Obj to JSON 

// Making Directory if not present
private static void CheckDirectory(){
    if (!Directory.Exists(FILE_PATH)){
        Directory.CreateDirectory(FILE_PATH);
    }
}

// Reset Data If User Play 1st Time
private static void CheckFilesPreviousData(){
    // Checking File is not Null
    UserData tempObj = LoadUnLockLevelData();
    if(tempObj == null){
        const int FIRSTLEVEL = 1;
        SaveUnLockedLevelData(FIRSTLEVEL);
        Debug.Log("Donee");
    }
    
}

#endregion

}

相关问题