本文介绍了如何使用ienumerator或热键在C#中保存和加载数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过代码(Ienumerator)或热键(按F1保存/按F5加载)来实现AutoSave和AutoLoad功能..不需要使用GUI。
我尝试过:
I would like to implement an AutoSave and AutoLoad function through code(Ienumerator) or a Hotkey(Press F1 to Save / Press F5 to Load) ..without having to use a GUI.
What I have tried:
using UnityEngine; // For Debug.Log, etc.
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System;
using System.Runtime.Serialization;
using System.Reflection;
using System.Timers;
public static class SaveLoad {
public static List<Game> stellate = new List<Game>();
public static void Save()
{
SaveLoad.stellate.Add(Game.current);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/stellate.sgd");
bf.Serialize(file, SaveLoad.stellate);
file.Close();
}
public static void Load()
{
if (File.Exists(Application.persistentDataPath + "/stellate.sgd"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/stellate.sgd", FileMode.Open);
SaveLoad.stellate = (List<Game>)bf.Deserialize(file);
file.Close();
}
}
}
/*private Timer timer1;
public void InitTimer()
{
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 2000; // in miliseconds
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
save();
}*/
推荐答案
这篇关于如何使用ienumerator或热键在C#中保存和加载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!