从事二十一点风格的统一游戏。无法读写文件
Working on a blackjack style unity game. Can't read and write a file
所以。我正在尝试制作一个保存配置文件,但我有图片中显示的错误并且不知道如何修复它,因为我一直在关注 YouTube 上的教程并且从未发生过此错误。谁能帮我算一下我该怎么做。
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SavingControl : MonoBehaviour
{
public static SavingControl control;
public int cash = PlayerBetting.instance.bankBal;
public int difficulty = 2;
void Awake ()
{
if (control = null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if (control != this)
{
Destroy(gameObject);
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.BankBal = cash;
data.Difficulty = difficulty;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/PlayerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath = "/PlayerInfro.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
cash = data.BankBal;
difficulty = data.Difficulty;
}
}
}
[Serializable]
class PlayerData
{
public int BankBal;
public int Difficulty;
}
将 =
更改为 +
:
FileStream file = File.Open(Application.persistentDataPath + "/PlayerInfro.dat", FileMode.Open);
最好使用 Path.Combine
:
FileStream file = File.Open(Path.Combine(Application.persistentDataPath, "PlayerInfro.dat"), FileMode.Open);
所以。我正在尝试制作一个保存配置文件,但我有图片中显示的错误并且不知道如何修复它,因为我一直在关注 YouTube 上的教程并且从未发生过此错误。谁能帮我算一下我该怎么做。
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SavingControl : MonoBehaviour
{
public static SavingControl control;
public int cash = PlayerBetting.instance.bankBal;
public int difficulty = 2;
void Awake ()
{
if (control = null)
{
DontDestroyOnLoad(gameObject);
control = this;
}
else if (control != this)
{
Destroy(gameObject);
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.BankBal = cash;
data.Difficulty = difficulty;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/PlayerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath = "/PlayerInfro.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
cash = data.BankBal;
difficulty = data.Difficulty;
}
}
}
[Serializable]
class PlayerData
{
public int BankBal;
public int Difficulty;
}
将 =
更改为 +
:
FileStream file = File.Open(Application.persistentDataPath + "/PlayerInfro.dat", FileMode.Open);
最好使用 Path.Combine
:
FileStream file = File.Open(Path.Combine(Application.persistentDataPath, "PlayerInfro.dat"), FileMode.Open);