您试图将一个简单的保存脚本统一附加到GameObject(立方体)中。一切看起来都很好(没有错误),除了Serialize部分(说'方法'没有重载'Serialize'需要1个参数)。任何人都可以指导我吗?这是代码的示例。非常感谢!
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SavingTheGame : MonoBehaviour {
public void OnTriggerEnter (Collider collider)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/SaveGame.dat");
bf.Serialize(file);
file.Close();
}
}
答案 0 :(得分:1)
您没有序列化任何内容,您需要提供要序列化的对象
public void OnTriggerEnter (Collider collider)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/SaveGame.dat");
// Change here
var obj = "This Will Be Serialized";
bf.Serialize(file, obj);
file.Close();
}
BinaryFormatter.Serialize需要2个参数,stream和object
您可以在Unity中关注使用BinaryFormatter进行保存/加载的精彩教程:https://gamedevelopment.tutsplus.com/tutorials/how-to-save-and-load-your-players-progress-in-unity--cms-20934