因为在开发游戏的时候需要重复游戏来测试游戏,但是游戏目前还没有多存档的计划,所以我们先整理了这样一个编辑工具 原则非常暴力,即阅读不同的存档文件
首先定义一个存档槽,用于对准 存档、读取等操作,保存时复制原始存档文件,截图Assets在目录下,文件在阅读时被覆盖 可以使用原始文件
存档的路径 与文件名 需要根据自己的项目修改
此外,存档时需要保存截图并显示,因此需要协程读取保存的资源图片,直接按顺序编写代码。github一个编辑器携程工具 https://github.com/marijnz/unity-editor-coroutines
[Serializable] public class SaveSlot {
[HideLabel] [HorizontalGroup("slot")] [VerticalGroup("slot/ver")] public string note = "存档名"; [HideLabel] [VerticalGroup("slot/ver")] public string time = DateTime.Now.ToString("s"); [PreviewField(ObjectFieldAlignment.Left)] [HideLabel] [HorizontalGroup("slot")] public Texture texture; private string path; [Button("保存在此存档槽中")] [ResponsiveButtonGroup(/span>"SaveSlot")] void Save() {
if (Directory.Exists(Application.persistentDataPath)) {
path = Application.persistentDataPath + "/savedata_Beta-" + note + ".sav"; File.Copy(Application.persistentDataPath + "/savedatas/savedata_Beta.sav", path, true); ScreenCapture.CaptureScreenshot(Application.dataPath + "/Game/SaveSlot/savedata_Beta-" + note + ".png"); this.StartCoroutine(LoadImg("Assets/Game/SaveSlot/savedata_Beta-" + note + ".png")); } } IEnumerator LoadImg(string path) {
AssetDatabase.ImportAsset("Assets/Game/SaveSlot/savedata_Beta-" + note + ".png"); yield return new WaitForSeconds(2f); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); yield return new WaitForSeconds(2f); texture = AssetDatabase.LoadAssetAtPath<Texture>(path); Debug.Log("保存存档图片"); } [Button("读取此存档槽")] [ResponsiveButtonGroup("SaveSlot")] void Load() {
path = Application.persistentDataPath + "/savedata_Beta-" + note + ".sav"; if (File.Exists(path)) {
File.Copy(path, Application.persistentDataPath + "/savedatas/savedata_Beta.sav", true); Debug.Log("读取存档:" + note); } } public void Delete() {
if (Directory.Exists(Application.persistentDataPath)) {
path = Application.persistentDataPath + "/savedata_Beta-" + note + ".sav"; if (File.Exists(path)) {
File.Delete(path); Debug.Log("删除存档:" + note); } File.Delete(Application.dataPath + "/Game/SaveSlot/savedata_Beta-" + note + ".png"); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); } } [Button("打开文件位置")] [ResponsiveButtonGroup("SaveSlot")] void Open() {
OpenDirectory(path); } } //打开文件夹并选中文件(windows下) public static void OpenDirectory(string path) {
if (string.IsNullOrEmpty(path)) return; var newPath = @"" + path.Replace("/", "\\"); var args = string.Format("/Select, {0}", newPath); if (!File.Exists(path)) {
Debug.LogError("没有文件: " + path); return; } ProcessStartInfo pfi = new ProcessStartInfo("Explorer.exe", args); Process.Start(pfi); }
然后用一个list把存档槽存起来
这里用到了odin的对集合改变时监测的功能,当删除一个存档槽时调用它的删除方法去删除存档文件以及预览图
[FoldoutGroup("存档工具")] [OnCollectionChanged("OnChangedBefore", "OnChangedAfter")] [LabelText("存档槽")]
public List<SaveSlot> saveSlots;
void OnChangedBefore(CollectionChangeInfo info, object value)
{
if (info.ChangeType == CollectionChangeType.RemoveIndex)
{
saveSlots[info.Index].Delete();
}
}
void OnChangedAfter(CollectionChangeInfo info, object value)
{
}
然后就可以愉快的使用了
另外假如我们使用svn多人协作,这样就可能会上传SaveSlot中的图片,所以我们要忽略到SaveSlot文件夹中的png和meta文件