1
This commit is contained in:
71
unity/Assets/Script/SaveSystem/SaveSystem.cs
Normal file
71
unity/Assets/Script/SaveSystem/SaveSystem.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
public static class SaveSystem
|
||||
{
|
||||
public static bool IsHaveFile(string FilePath)
|
||||
{
|
||||
if (File.Exists(FilePath))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
public static void CreateFile(string FilePath, bool IfHaveFileIsCreate = false)
|
||||
{
|
||||
if (!File.Exists(FilePath))
|
||||
{
|
||||
File.CreateText(FilePath);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SaveDate(string fileName, object date)
|
||||
{
|
||||
var json = JsonUtility.ToJson(date);
|
||||
|
||||
string path = Path.Combine(Application.persistentDataPath, fileName);
|
||||
|
||||
if (!IsHaveFile(path))
|
||||
{
|
||||
//Debug.Log("文件不存在");
|
||||
CreateFile(path);
|
||||
}
|
||||
try
|
||||
{
|
||||
File.WriteAllText(path, json);
|
||||
Debug.Log("存档成功");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.Log("存档创建失败");
|
||||
SaveDate(fileName,date);
|
||||
}
|
||||
}
|
||||
public static T LoadDate<T>(string fileName)
|
||||
{
|
||||
|
||||
var path = Path.Combine(Application.persistentDataPath, fileName);
|
||||
|
||||
if (IsHaveFile(path))
|
||||
{
|
||||
var json = File.ReadAllText(path);
|
||||
|
||||
T date = JsonUtility.FromJson<T>(json);
|
||||
Debug.Log("读档成功}");
|
||||
return date;
|
||||
}
|
||||
return default;
|
||||
}
|
||||
public static void DeleteDate(string fileName)
|
||||
{
|
||||
var path = Path.Combine(Application.persistentDataPath, fileName);
|
||||
if (!IsHaveFile(path))
|
||||
{
|
||||
return;//不存在
|
||||
}
|
||||
File.Delete(path);
|
||||
Debug.Log("删除成功");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user