【问题标题】:How to save dictionary data or array list data to playerprefs more effeciently?如何更有效地将字典数据或数组列表数据保存到 playerprefs?
【发布时间】:2016-11-22 10:00:22
【问题描述】:

有谁知道如何更有效地将字典数据或数组列表数据保存到 playerprefs 中?

例如我有一个类项:

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

// Make Class Item
public class item {
    public string itemName;
    public int itemID;
    public string itemDesc;
    public string itemIcon;
    public GameObject itemModel;
    public int itemTime;
    public int hightprice;
    public int stdprice;
    public int itemStock;
    public int harvest;
    public RawTree rawTree;
    public ItemType itemType;
    public ItemProd itemProd;
    public ItemLocation itemLocation;
    public ItemMerchant itemMerchant;
    public int Lvlunlock;
    private string baseName;
    public int spawnRate;
    public int minqtySpawnBuy;
    public int maxqtySpawnBuy;
    public int minqtySpawnSell;
    public int maxqtySpawnSell;
    public int itemExp;


    public enum ItemType {
        Raw,
        Admirable,
        Valuable
    }

    public enum RawTree {
        BigTree,
        SmallTree,
        Field,
        None
    }

    public enum ItemProd {
        Corps,
        Dairy,
        JuiceJamMaker,
        Kitchen,
        Bakery,
        CraftHouse,
        ChickenCoop,
        FishingSpotMountain,
        CowPasture,
        LunaMine,
        PigPen,
        FishingSpotLake,
        TropicalWood,
        SheepPasture,
        FishingSpotSea,
        Beebox,
        HomeIndustry,
        Merchant
    }

    public enum ItemLocation { 
        Home,
        Orchard,
        Forest
    }

    public enum ItemMerchant { 
        Margaret,
        Lucy,
        Bobby,
        Roger,
        Grace
    }

    public item (string name, int ID, string desc, int harvestx, int time, int stdpricex, int hightpricex, int stock, int Lvlunlockx, RawTree RawTree, ItemType type, ItemProd prod, string folderx, ItemLocation location, ItemMerchant merchant, int rate, int minspawnBuy, int maxspawnBuy, int minspawnSell, int maxspawnSell, int Exp) {
        itemName = name;
        itemID = ID;
        itemDesc = desc;
        harvest = harvestx;
        itemTime = time;
        stdprice = stdpricex;
        hightprice = hightpricex;
        itemStock = stock;
        Lvlunlock = Lvlunlockx;
        rawTree = RawTree;
        itemType = type;
        itemProd = prod;
        itemIcon = folderx;
        itemLocation = location;
        itemMerchant = merchant;
        spawnRate = rate;
        minqtySpawnBuy = minspawnBuy;
        maxqtySpawnBuy = maxspawnBuy;
        minqtySpawnSell = minspawnSell;
        maxqtySpawnSell = maxspawnSell;
        itemExp = Exp;
    }

    public item() {

    }
}

然后我将创建一个列表项:

public List<item> items = new List<item> ();

首先: 如何更有效地将项目保存到 playerprefs?

例如我有字典上的数据:

public Dictionary <string, Dictionary <string, int> > productSellMargaret;

第二: 如何更有效地将 productSellMargaret 保存到 playerprefs?

如何将它们都保存到 playerprefs 中,以便在退出游戏并使用 continue 数据再次玩游戏时继续调用数据?

【问题讨论】:

  • Playerpref 只能保存某些数据类型。话虽这么说,如果您写入/读取文件(txt 或二进制),它会更快、更高效。
  • 将您的对象解析为 JSON 并保存在文件中
  • 你好@UmairM,我找到了这个Newtonsoft.Json.Serialization。它可以将其转换为 json。但我不明白如何统一称呼它。我尝试使用 newtons 键入,但它没有使用文件名 newtons。你有什么想法吗?
  • @DennisLiu 检查我的答案

标签: c# unity3d


【解决方案1】:

准确的答案是

“不要将较大的数据保存到 PlayerPrefs”

PlayerPrefs 用于保存游戏设置等小值。在 PlayerPrefs 中保存此类配置是一种很好的方法。

但是

在保存大型数据(如对象列表或嵌套字典)时。建议保存到文件中。

我应该如何解析数据对象并将其保存到文件中?

  1. 有不同的方法可以做到这一点。其中之一是使用BinaryFormatter 序列化数据对象并使用System.IO 将其保存在文件中。要获得实现此目的的快速教程,我建议您:TutsPlus: How to Save and Load Your Players' Progress in Unity

  2. 同样,这只是一种方法。您也可以使用其他方法。 Unity 的新 JSON Serializer 是您的另一个不错的选择。

  3. 如果您想使用XML 而不是JSON,XML Serializer 是一个不错的选择。

希望这会有所帮助。

【讨论】:

  • 嗨@Umair,我会试试的。如果我有问题我会再问。谢谢
  • 嗨@Umair,我在尝试您的推荐代码存档游戏时遇到问题,请检查此链接link 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多