【问题标题】:Cannot serialize my game stats无法序列化我的游戏统计信息
【发布时间】:2018-05-21 14:07:06
【问题描述】:

好吧,我正在尝试使用系统可序列化保存我的游戏统计信息,但我无法做到这一点我有两个脚本,一个名为“Stats”,另一个名为“SaveLoad”,位于“Stats”中,我有我的可序列化和“SaveLoad”我有我的保存和加载脚本,我按照这里的说明进行操作

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

但由于我是这些东西的初学者,而且我的可序列化数据不同,我在保存它时遇到了问题。

我的“统计”

 using UnityEngine;
 using System.Collections;
[System.Serializable]
public class Stats : MonoBehaviour {

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;

// 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 () {
    if (score > personalbest) {
        personalbest = score;
    }
    //Debug.Log (" " + coins);
}

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

    }
}

void AddScore (){
    if (BunnyScript.BunnyAlive == true) {
        score += 1;
    }
}

}

还有我的“SaveLoad”脚本

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<Stats> savedGames = new List<Stats>();

public static void Save (){
    savedGames.Add(Stats);
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd");
    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<Stats>)bf.Deserialize(file);
        file.Close();
    }
}
}

【问题讨论】:

  • 如果可以不保存 currentumbdur 和硬币

标签: c# unity3d serialization save unity5


【解决方案1】:

你不应该序列化一个 MonoBehaviour 类来存储数据,下面有很多你在这种类型中看不到的东西,这是错误的。

此外,您有一个 stats 对象列表,但它只包含静态值,因此您的所有 Stats 对象都具有相同的内容。

 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 (){       
       stats.AddCoins();
    }

    void AddScore (){
       stats.AddScore();
    }
}
[Serializable]
public class StatContainer
{
    public int coins = 0;
    public int totalcoins = 0;
    public int score = 0;

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

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

    // and the rest

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

现在您可以像以前一样序列化 StatContainer。 因为不再有静态方法,每个 Stats 组件上的每个 StatContainr 都是唯一的,不会与其他人共享任何内容。

另外,您甚至可以使用 StatContainer 更轻松地执行单元测试,但这是另一个话题。

【讨论】:

    猜你喜欢
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多