2017-12-07 6 views
2

私は自分のゲームの統計をシリアライズ可能なシステムで保存しようとしていますが、私はそれを行うことができません "Stats"という名前の2つのスクリプトと "Stats"私serializablesと「SaveLoadは」私は私の保存と読み込みスクリプトを持って、私はここの指示に従っに私のゲームの統計をシリアル化できません

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

を持っているが、私はこのようなものでbegginerと私のseriazableデータは、私は保存上の問題を抱えてい異なっていますので、それ。

私の「統計」

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(); 
    } 
} 
} 
+0

また、現在の硬貨と硬貨を保存できない場合 –

答えて

1

あなたは、データを格納するためのMonoBehaviourクラスをシリアライズべきではありません、そんなにその下にありますが、この中で見ることができませんちょうど間違っているとタイプしてください。

また、統計オブジェクトのリストがありますが、すべての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でユニットテストを簡単に行うこともできますが、これは別のトピックです。

関連する問題