【问题标题】:Need help in serialization在序列化方面需要帮助
【发布时间】:2017-12-07 20:41:24
【问题描述】:

过去两个小时一直在尝试学习如何序列化我的数据,我需要一个在“StatContainer”中序列化我的数据的代码,我试过这个:

https://gamedevelopment.tutsplus.com/tutorials/how-to-save-and-load-your-players-progress-in-unity--cms-20934

还有很多,但我无法理解如何序列化我的数据

using UnityEngine;
using System.Collections;

public class Stats : MonoBehaviour 
{
    StatContainer stats = new StatContainer();

    // Use this for initialization
    void Start () {
        InvokeRepeating ("AddCoins", 4.0f, 2.0f);
        InvokeRepeating ("AddScore", 1.5f, 1.5f);
    }

    // Update is called once per frame
    void Update () {
        this.stats.Update();
    }

    void AddCoins (){       
        if (BunnyScript.BunnyAlive == true) {
            StatContainer.coins += 1;    
        }
    }

    void AddScore (){
        if (BunnyScript.BunnyAlive == true) {
            StatContainer.score += 1;
        }
    }
    }
    [System.Serializable]
    public class StatContainer
    {
    public static int coins = 0;
    public static int totalcoins = 0;
    public static int score = 0;

    public static int personalbest = 0;
    public static float UmbrellaSpeed = 0.1f;
    public static float currentumbdur = 500;

    public static int CarrotSpawnRateLVL = 1;
    public static float CarrotSpawnRate = 60f;
    public static int CarrotSpawnRateUpgradeCost = 15;


    public static int UmbrellaDurabilityLVL = 1;
    public static float UmbrellaDurability = 500;
    public static int UmbrellaDurabilityUpgradeCost = 30;

    public static int UmbrellaSizeLVL = 1;
    public static float UmbrellaSize = 0f;
    public static int UmbrellaSizeUpgradeCost = 25;


    public static int CarrotEffectLVL = 1;
    public static float CarrotEffect = 20;
    public static int CarrotEffectUpgradeCost = 25;


    public static int HealthRegenLVL = 1;
    public static float HealthRegenTime = 4f;
    public static int HealthRegenCost = 100;


    public static int BuyTreesCost = 250;

    public static int Tree1Bought = 0;
    public static float Tree1Size = 0;
    public static int Tree1SizeLVL = 1;
    public static int Tree1SizeUpgradeCost = 50;

    public static int Tree2Bought = 0;
    public static float Tree2Size = 0;
    public static int Tree2SizeLVL = 1;
    public static int Tree2SizeUpgradeCost = 50;

    public static int Tree3Bought = 0;
    public static float Tree3Size =0;
    public static int Tree3SizeLVL = 1;
    public static int Tree3SizeUpgradeCost = 50;
    // and the rest

    public void Update(){
        if (score > personalbest) {
            personalbest = score;
        }
    }
}

这就是我尝试序列化它的方式

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

public static class SaveLoad {
    public static List<StatContainer> savedGames = new List<StatContainer>();

    //it's static so we can call it from anywhere
    public static void Save() {
        SaveLoad.savedGames.Add(StatContainer.current);
        BinaryFormatter bf = new BinaryFormatter();
        //Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
        FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd"); //you can call it anything you want
        bf.Serialize(file, SaveLoad.savedGames);
        file.Close();
    }   

    public static void Load() {
        if(File.Exists(Application.persistentDataPath + "/savedGames.gd")) {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
            SaveLoad.savedGames = (List<StatContainer>)bf.Deserialize(file);
            file.Close();
        }
    }
}

我在游戏开始时调用加载函数,并使用每 2 秒重复一次的调用进行保存

【问题讨论】:

  • 那么,你的问题是什么?
  • StatContainer中的数据如何序列化
  • 您需要添加有关您的问题的更多信息。
  • 好的,我添加了更多信息
  • 我不是专家,但是,如果我是你,我不会声明类的属性static。

标签: c# unity3d serialization


【解决方案1】:

首先移除StatContainer中的静态成员
要访问您的数据,请让 Load 函数返回 StatContainer 并且在您的类 Stats 中,您可以将 StatContainer 置于静态或创建一个 singleton of 统计数据。

其次,我不知道您的目的,但也许您可以查看xml serialization 而不是 BinaryFormat,以获得人类可读的文件,这样您就可以轻松更改数据.
在这种情况下,我建议您查看 Xml 标记,如下所示。
(并且您可以在发布游戏时加密您的 xml)

[System.Serializable]
[XmlRoot(ElementName = "stats-of-something")]
public class StatContainer
{
     [XmlAttribute(AttributeName = "coins")]
     public int coins = 0;
     [XmlElement(ElementName = "umbrella-speed")]
     public float UmbrellaSpeed = 0.1f;
}

或

[System.Serializable]
[XmlRoot(ElementName = "stats-of-something")]
public class StatContainer
{
     [XmlAttribute(AttributeName = "coins")]
     private int _coins = 0;
     [XmlElement(ElementName = "umbrella-speed")]
     private float _umbrellaSpeed = 0.1f;

     public int coins { get { return _coins; } set { _coins = value; } }
     public float umbrellaSpeed  { get { return _umbrellaSpeed ; } set { _umbrellaSpeed = value; } }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2011-08-06
    • 1970-01-01
    • 2021-08-26
    • 2014-01-10
    相关资源
    最近更新 更多