在unity中实现如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class FileManager : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
getSavePath();
}
//保存关卡数据
public void saveLeavelData(string name, string content) {
string fullPath = getSavePath() + name;
if (!File.Exists(fullPath)) {
File.Create(fullPath).Dispose();
}
Debug.Log(fullPath);
if (content != null && content.Length > 0) {
File.WriteAllText(fullPath, content);
}
}
//获取关卡数据
public string getLevelData(string name) {
string fullPath = getSavePath() + name;
if (File.Exists(fullPath))
{
string content = File.ReadAllText(fullPath);
return content;
}
return null;
}
//获取存储路径
string getSavePath() {
string savePath = Application.persistentDataPath + "/MathBlock/";
string path = Path.GetFullPath(savePath);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
}
注:创建文件后要调Dispose(),不然会报错
网友评论