简体   繁体   English

加载保存的统一问题

[英]Unity problems with loading save

Loosing my mind over it, but still cant find what's wrong.对此失去了理智,但仍然找不到问题所在。 I have 2 List variables (items, playerEquipment.equipment), trying to save both of them and load after, but it gives me strange results.我有 2 个列表变量(项目、playerEquipment.equipment),试图保存它们并在之后加载,但它给了我奇怪的结果。 For items its always good, saves and loads all the time, but for playerEquipment.equipment behaviour is different - if i save and then immidiatly load without exiting play mode, i get right result from load function, but if i save, stop play mode, start play mode, and then load - i get list of "null" is result.对于项目它总是很好,一直保存和加载,但对于 playerEquipment.equipment 行为是不同的 - 如果我保存然后立即加载而不退出播放模式,我从加载 function 得到正确的结果,但如果我保存,停止播放模式,开始播放模式,然后加载 - 我得到“空”列表是结果。

Here is my save code这是我的保存代码

public void SavePlayer()
    {
        SaveData data = new SaveData();
        data.level = level;
        data.nick = nick;
        data.experience = experience;
        data.experienceToNextLevel = experienceToNextLevel;
        data.maxHP = maxHP;
        data.minAttack = minAttack;
        data.maxAttack = maxAttack;
        data.coins = coins;
        data.items = items;
        data.equipment = playerEquipment.equipment;
        for (int i = 0; i < data.equipment.Count; i++)
            Debug.Log("Saved type " + data.equipment[i].type + " with id " + data.equipment[i].id+" and type "+ data.equipment[i].type);
        //Save data from PlayerInfo to a file named players
        DataSaver.saveData(data, "players");
    }

Here is my load code这是我的加载代码

public void LoadPlayer()
    {
        SaveData data = DataSaver.loadData<SaveData>("players");
        if (data == null)
        {
            Debug.Log("ERROR: data not loaded");
            return;
        }
        level = data.level;
        nick = data.nick;
        experience = data.experience;
        experienceToNextLevel = data.experienceToNextLevel;
        maxHP = data.maxHP;
        currentHP = data.maxHP;
        minAttack = data.minAttack;
        maxAttack = data.maxAttack;
        coins = data.coins;
        items = data.items;
        playerEquipment.equipment = data.equipment;
    }

SaveData is保存数据是

[Serializable]
class SaveData
{
    //all data types and names that go to save
    public int level;
    public string nick;
    public int experience;
    public int experienceToNextLevel;
    public int maxHP;
    public float minAttack;
    public float maxAttack;
    public int coins;
    //public List<String> itemsString;
    //public List<String> equipmentString;


    public List<Item> items;
    public List<Item> equipment;
}

Save code i grabbed from this answer and didnt change it保存我从这个答案中获取的代码并且没有更改它

UPD: Item class is UPD:项目 class 是

public enum ItemType { HELMET, SHOULDERS, WEAPON_MAIN, WEAPON_SECOND, BODY, ARMOR, HANDS, PANTS, BOOTS }

[CreateAssetMenu(menuName = "item")]
[Serializable]
public class Item : ScriptableObject
{
    public int id;
    public Sprite sprite;
    public string itemName;
    public ItemType type;
    
}

You can not save and load a list of ScriptableObjects like that.您不能像那样保存和加载 ScriptableObjects 列表。 JsonUtility.ToJson saves these references as InstanceIDs which are nor persistent. JsonUtility.ToJson将这些引用保存为非持久性的 InstanceID。 That is why it works when you do not stop play mode.这就是为什么当您不停止播放模式时它会起作用的原因。

Since you already implemented an ID you could save a list of IDs instead and add a method which finds them by that references and adds adds them back to your playerEquipment.equipment由于您已经实现了一个 ID,因此您可以保存一个 ID 列表并添加一个方法,该方法通过该引用找到它们并将它们添加回您的playerEquipment.equipment

Edit: This means your statement of it working for the item list is likely false, you should check and edit your answer accordingly.编辑:这意味着您关于它适用于项目列表的陈述可能是错误的,您应该相应地检查和编辑您的答案。

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

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