![](/img/trans.png)
[英]how can i add a single leaderboard with level and high score in google play services
[英]How can I add a score from a level to a currency outside of said level when the level ends?
我有一个名为“Stash”的 int 变量。 当我通关时,游戏将保存。 当它保存时,无论我的分数是那个水平,我都希望 Stash 上升。 目前,我的代码没有保存我的 stash 值,它只是将它设置为“0(stash 默认)+ Score(无论我的分数是那个级别)”。 我如何在此代码中编写以实现我的目标?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[RequireComponent(typeof(GameData))]
public class SaveScript : MonoBehaviour
{
private GameData gameData;
private string savePath;
void Start()
{
gameData = GetComponent<GameData>();
savePath = Application.persistentDataPath + "/gamesave.save";
}
public void SaveData()
{
var save = new Save()
{
SavedStash = gameData.Stash + gameData.Score //<------ (This code)
};
var binaryFormatter = new BinaryFormatter();
using (var fileStream = File.Create(savePath))
{
binaryFormatter.Serialize(fileStream, save);
}
Debug.Log("Data Saved");
}
public void LoadData()
{
if (File.Exists(savePath))
{
Save save;
var binaryFormatter = new BinaryFormatter();
using (var fileStream = File.Open(savePath, FileMode.Open))
{
save = (Save)binaryFormatter.Deserialize(fileStream);
}
gameData.Stash = save.SavedStash; //<---------- (and this code)
gameData.ShowData();
Debug.Log("Data Loaded");
}
else
{
Debug.LogWarning("Save file doesn't exist");
}
}
}
我的 GameData 文件中还有相关内容:
public int Stash { get; set; }
public int StashNew { get; set; }
public int Score { get; set; }
StashNew 只是我必须让整个事情工作的一个想法。
首先:永远不要简单地使用+ "/"
作为系统文件路径!
不同的目标设备可能具有具有不同路径分隔符的不同文件系统。
而是使用Path.Combine
,它会根据运行的操作系统自动插入正确的路径分隔符。
savePath = Path.combine(Application.persistentDataPath, "gamesave.save");
然后是主要问题:
属性如
public int Stash { get; set; }
public int StashNew { get; set; }
public int Score { get; set; }
不会被BinaryFormatter
/序列化!
因此当你做
binaryFormatter.Serialize(fileStream, save);
数字甚至没有写入输出文件!
然后,当读取相同的内容不起作用并且您的属性只是保持int
默认值0
。
只需删除这些{ get; set; }
{ get; set; }
{ get; set; }
为了你的财产转化为可序列化字段,你应该罚款。
public int Stash;
public int StashNew;
public int Score;
仅用于演示我使用此代码
public class MonoBehaviourForTests : MonoBehaviour
{
[SerializeField] private GameData In;
[SerializeField] private GameData Out;
[ContextMenu("Test")]
private void Test()
{
var formatter = new BinaryFormatter();
using (var fs = File.Open(Path.Combine(Application.streamingAssetsPath, "Example.txt"), FileMode.OpenOrCreate, FileAccess.Write))
{
formatter.Serialize(fs, In);
}
using (var fs = File.Open(Path.Combine(Application.streamingAssetsPath, "Example.txt"), FileMode.Open, FileAccess.Read))
{
Out = (GameData)formatter.Deserialize(fs);
}
}
[Serializable]
private class GameData
{
public int Stash;
public int StashNew;
public int Score;
}
}
正如您所看到的,这也有副作用,即现在这些字段实际上出现在 Inspector 中并且可以进行编辑。 我只将值写入In
实例,在点击Test
后,您可以看到它们通过文件加载到Out
实例中。
但是总的来说,我根本不建议在这里使用BinaryFormatter
......它增加了很多你根本不需要的开销垃圾。 只需选择JSON或类似的简单文件格式,如 XML 或 CSV,它们只存储相关数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.