如何使用ienumerator或热键在C#中保存和加载数据? [英] How do I use the ienumerator or a hotkey to save and load data in C#?
本文介绍了如何使用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();
}*/
推荐答案
我厌恶System.Timers,所以我会考虑使用System.Reactive。它可以通过NuGet获得。
我写了一篇利用这个软件包的文章 - SQLXAgent - SQL Express的工作 - 第4部分,共6部分 [ ^ ]
多部分系列的这一部分特别提到它,但我的建议是下载coa-de并查看我如何使用它,或者更好的是,谷歌System.Reactive获取信息。
I abhor System.Timers, so I would consider using System.Reactive. It's available via NuGet.
I wrote an article that utilizes this package - SQLXAgent - Jobs for SQL Express - Part 4 of 6[^]
This part of the multipart series specifically mentions it, but my advice is to download the coa-de and see how I used it, or better yet, google "System.Reactive" for information.
这篇关于如何使用ienumerator或热键在C#中保存和加载数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文