c# - 如何更有效地将字典数据或arraylist数据保存到playerprefs?

标签 c# unity3d

有谁知道如何更有效地将字典数据或数组列表数据保存到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> ();

首先: 如何更有效地将元素保存到播放器偏好?

例如我有字典上的数据:

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

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

如何将它们都保存到播放器偏好设置中,以便在退出游戏并使用继续数据再次玩游戏时继续调用数据?

最佳答案

一个准确的答案是

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

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

但是

在保存对象列表或嵌套字典等大规模数据时。建议保存到文件中。

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

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

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

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

希望这对您有所帮助。

关于c# - 如何更有效地将字典数据或arraylist数据保存到playerprefs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38459689/

相关文章:

unity3d - Unity PBR 着色器未显示在 Sphere 上

android - 在移动设备上的 Unity 中显示 Google map

c# - 如何在 C# 中以编程方式保存用户使用 CTRL-C 复制到剪贴板的 Outlook 电子邮件附件(例如 PDF)

c# - Process.Start() 中的错误——系统找不到指定的文件

c# - EF 排序和分页 - 两次订购速度慢?

c# - 确定多边形何时被新边封闭

c# - OpenCV + 安卓 + Unity

android - 升级到 Unity 5 后 res/raw 中的 Android 库插件 .js 文件出现问题

c# - 如何在 C# 中解析 Odata 过滤器字符串?

c# - 无法将类型 System.Collections.Generic.IEnumerable<T> 隐式转换为 System.Collections.Generic.List<B>