Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更有效地将字典数据或数组列表数据保存到playerprefs?_C#_Unity3d - Fatal编程技术网

C# 如何更有效地将字典数据或数组列表数据保存到playerprefs?

C# 如何更有效地将字典数据或数组列表数据保存到playerprefs?,c#,unity3d,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;

有人知道如何更有效地将字典数据或数组列表数据保存到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> ();
公共列表项=新列表();
第一: 如何将项目保存到PlayerRefs以提高效率

例如,我有字典上的数据:

public Dictionary <string, Dictionary <string, int> > productSellMargaret;
公共词典产品玛格丽特;
第二: 如何更有效地保存产品SellMargaret以播放参考


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

准确的答案是

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

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

但是

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

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

  • 有不同的方法可以做到这一点。其中之一是使用
    BinaryFormatter
    序列化数据对象,并使用
    System.IO
    将其保存在文件中。有关实现此目的的快速教程,请参考:

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

  • 如果您想使用
    XML
    而不是
    JSON
    ,这是一个不错的选择


  • 希望这有帮助。

    Playerpref只能保存某些数据类型。也就是说,如果你写/读一个文件(txt或二进制文件),会更快、更高效。将你的对象解析为JSON并保存在fileHello@UmairM中,我发现了这个Newtonsoft.JSON.Serialization。它可以将其转换为json。但我不明白如何把它称为统一体。我试着用牛顿打字,但它没有使用文件名牛顿。你有什么想法吗?@DennisLiu检查我的回答@Umair,我试试看。如果我有问题,我会再问。ThanksHi@Umair,我在尝试您的推荐代码savegame时遇到问题,请检查此链接,谢谢