由于在开发游戏时需要反复重新游戏走一遍流程来测试游戏,但是游戏目前没有做多存档的计划,所以先整了个这么个编辑器工具 原理非常暴力,就是读取不同的存档文件
首先定义一个存档槽类,用来对 存档进行保存,读取等操作,保存时将原本的存档文件复制一份,并截图到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("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);
}
}
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文件
|