简体   繁体   English

Unity中使用ScriptableObjects制作的基于文本的Adventure游戏中的保存/加载系统问题

[英]Issues with Save/Load System in a Text Based Adventure game made with ScriptableObjects in Unity

I am almost done with my Text Based Adventure Game and am trying to setup a Save/Load system that works. 我的基于文本的冒险游戏几乎完成了,并试图设置一个有效的保存/加载系统。 I can't seem to accomplish this. 我似乎无法做到这一点。

I have viewed multiple tutorials and I feel that Json may be what I need. 我看过多个教程,我觉得Json可能正是我所需要的。 Unfortunately I can't seem to get Json code correct for the 'Load' part. 不幸的是,我似乎无法正确获取“加载”部分的Json代码。 I am also concerned that the amount of data I am trying to save is too much for a simple Serialization process OR I am over-complicating this and there is a simpler solution. 我还担心我要保存的数据量对于一个简单的序列化过程来说太多了,或者我对此过于复杂了,并且有一个更简单的解决方案。

This is the data I am tryin to Save/Load. 这是我正在尝试保存/加载的数据。 As you can see I have Dictionaries and Lists, as well as a 'Room' ScriptableObject that I am trying to save, namely, the currentRoom the Player is in when the game is saved. 如您所见,我有“词典”和“列表”,还有一个我要保存的“ Room” ScriptableObject,即保存游戏时Player所在的currentRoom。 The currentRoom is tracked in the GameController. 在GameController中跟踪currentRoom。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class SaveData
{
public Room saveRoomNavigation;
public List<string> saveNounsInInventory = new List<string>();
public List<InteractableObject> saveUseableItemList = new 
List<InteractableObject>();
public Dictionary<string, string> saveExamineDictionary = new 
Dictionary<string, string>();
public Dictionary<string, string> saveTakeDictionary = new 
Dictionary<string, string>();
public List<string> saveNounsInRoom = new List<string>();
public Dictionary<string, ActionResponse> saveUseDictionary = new 
Dictionary<string, ActionResponse>();


public SaveData (GameController controller, InteractableItems 
interactableItems)
{
    saveRoomNavigation = controller.roomNavigation.currentRoom;
    saveNounsInInventory = interactableItems.nounsInInventory;
    saveUseableItemList = interactableItems.useableItemList;
    saveExamineDictionary = interactableItems.examineDictionary;
    saveTakeDictionary = interactableItems.takeDictionary;
    saveNounsInRoom = interactableItems.nounsInRoom;
    saveUseDictionary = interactableItems.useDictionary;

}


}

Then this is my Save System code where I am trying to implement the Serialization with Json and binary. 然后,这是我的“保存系统”代码,我试图在其中使用Json和二进制文件实现序列化。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public static class SaveSystem
{
public static void SaveGame (GameController controller, 
InteractableItems interactableItems)
{
    BinaryFormatter formatter = new BinaryFormatter();

    string path = Application.persistentDataPath + "/savegame.rta";
    FileStream stream = new FileStream(path, FileMode.Create);

    SaveData data = new SaveData(controller, interactableItems);

    var json = JsonUtility.ToJson(data);
    formatter.Serialize(stream, json);
    stream.Close();
}

public static SaveData LoadGame()
{
    string path = Application.persistentDataPath + "/savegame.rta";
    if (File.Exists(path))
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream stream = new FileStream(path, FileMode.Open);

        SaveData data = 
JsonUtility.FromJsonOverwrite((string)formatter.Deserialize(stream, 
????));
        //SaveData data = formatter.Deserialize(stream) as 
SaveData;
        stream.Close();

        return data;

    }
    else
    {
        Debug.LogError("Save file not found in " + path);
        return null;
    }
}
}

Where I put the '????' 我在哪里放'????' above is where I can't seem to get it to accept the Object I am trying to overwrite, which I thought was 'SaveData' since that is where I pull the information to put into the game when the Player clicks the 'Load' button. 上面是我似乎无法接受要覆盖的对象的地方,我认为这是“ SaveData”,因为那是我在玩家单击“加载”按钮时将信息放入游戏中的地方。 It tell me "'SaveData' is a type, which is not valid in the given context" 它告诉我“'SaveData'是一种类型,在给定的上下文中无效”

Here is the code I call when the 'Save' and 'Load' buttons are used, it is within my GameController Class where most everything is tracked during Game Play. 这是使用“保存”和“加载”按钮时我调用的代码,它位于我的GameController类中,在游戏过程中大多数内容都可以跟踪。

public void SaveGame()
{
    SaveSystem.SaveGame(this, interactableItems);
}

public void LoadGame()
{
    SaveData data = SaveSystem.LoadGame();

    roomNavigation.currentRoom = data.saveRoomNavigation;

    interactableItems.nounsInInventory = data.saveNounsInInventory;
    interactableItems.useDictionary = data.saveUseDictionary;
    interactableItems.takeDictionary = data.saveTakeDictionary;
    interactableItems.examineDictionary = 
    data.saveExamineDictionary;
    interactableItems.useableItemList = data.saveUseableItemList;
    interactableItems.nounsInRoom = data.saveNounsInRoom;


    DisplayRoomText();
    DisplayLoggedText();

}

When used in game, I get "SerializationException" errors, so that is when I attempted to try Json. 在游戏中使用时,出现“ SerializationException”错误,因此就是我尝试使用Json的时候。 I read where it can serialize a lot more than the basic Unity Serializer. 我读到它可以比基本的Unity Serializer进行序列化的地方更多。 The errors went away and it seems to save OK with Json, but now I am unable to get the Load syntax correct. 错误消失了,似乎可以与Json一起使用,但是现在我无法正确获取Load语法。

I am a novice so maybe I am making this harder then it has to be... I also looked into Userprefs, but I don't think I can convert the Scriptable Object, Room, into a type String. 我是新手,所以也许我正在使它变得更困难,而必须这样做……我也研究了Userprefs,但我认为我无法将可脚本编写的对象Room转换为String类型。

Thanks in advance! 提前致谢!

[UPDATE] Here is what I am able to have it save in .txt format. [更新]这是我能够将其保存为.txt格式的内容。 This proves that it is saving, just not sure "instanceIDs" will give me the Load that I need. 这证明它正在保存,只是不确定“ instanceIDs”是否会给我所需的负载。

ˇˇˇˇö{"saveRoomNavigation": {"instanceID":11496},"saveNounsInInventory": ["sword"],"saveUseableItemList":[{"instanceID":11212}, {"instanceID":11588},{"instanceID":10710},{"instanceID":11578}, {"instanceID":10718},{"instanceID":11388}, {"instanceID":11276}],"saveNounsInRoom":["rocks","hole"]} {ö{“ saveRoomNavigation”:{“ instanceID”:11496},“ saveNounsInInventory”:[“ sword”],“ saveUseableItemList”:[{“ instanceID”:11212},{“ instanceID”:11588},{“ instanceID”: 10710},{“ instanceID”:11578},{“ instanceID”:10718},{“ instanceID”:11388},{“ instanceID”:11276}],“ saveNounsInRoom”:[“ rocks”,“ hole”]}

Take a look at the documentation for Unity's built-in JsonUtility: 查看Unity内置JsonUtility的文档:

https://docs.unity3d.com/Manual/JSONSerialization.html https://docs.unity3d.com/Manual/JSONSerialization.html

It says types such as "Dictionaries" are not supported. 它说不支持诸如“字典”之类的类型。 Seeing as though you have several Dictionaries in your SaveData example, might I suggest using a more robust JSON library such as JSON.NET, which is free on the asset store: 好像您的SaveData示例中有几个词典,我可能建议使用更强大的JSON库,例如JSON.NET,该库在资产商店中免费提供:

https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347 https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM