From ec118a38334817d58dec4d8389ff961efc3f15b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=8B=A5=E8=BE=B0?= Date: Tue, 17 Mar 2026 11:33:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20MacroFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MacroFile.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 MacroFile.md diff --git a/MacroFile.md b/MacroFile.md new file mode 100644 index 0000000..985db1f --- /dev/null +++ b/MacroFile.md @@ -0,0 +1,61 @@ +文件宏包含预设常用的文件写入,读取操作,并自动解决文件不存在的问题,通过返回布尔状态来快速判断解决。 + +原始api中,仅包含一层简单的c#文件处理封装,无法适配unity其他平台情况,这些api已经被标记为过时,但你仍然可以继续使用他们。 +新版中,添加了对多平台的支持: + +* 增加了`CrossPlatformFileHandle`文件句柄,用于处理多平台的文件写入,加载的基类。 + 使用时,通过`CrossPlatformFileHandle.ActiveHandle`来获得当前平台自动对应的文件句柄,其中包括了一组同步和一组异步的读写方法,来对目标地址进行访问。 + 下面是一个示例: + ``` + // ==== 同步读取 ==== + CrossPlatformFileHandle.ActiveFileHandle.ReadTextFromFile("absolute file path", out string content); + + // ==== 同步写入 ==== + CrossPlatformFileHandle.ActiveFileHandle.WriteTextIntoFile("absolute file path", string content); + + // ==== 异步读取 ==== + var isCompleteRead = await CrossPlatformFileHandle.ActiveFileHandle.AsyncReadTextFromFile("absolute file path", + s => + { + // 成功读取文件 + }, + s => + { + // 读取失败,原因是 s + Debug.ErrorLog(s); + }); + + // ==== 异步写入 ==== + var isCompleteWrite = await CrossPlatformFileHandle.ActiveFileHandle.AsyncWriteTextIntoFile("absolute file path", + null, // 成功写入文件,但是无需处理 + null); // 写入失败,但是无需处理 + + // 异步方法包含回调函数处理,也可通过返回布尔值判断文件写入是否成功。 + ``` + 同时,文件句柄也提供了相对路径拼接: + ``` + // 返回steamingAssets下的myFile.json文件路径 + CrossPlatformFileHandle.ActiveFileHandle.CombineStreamingAssets("myFile.json"); + ``` + +* 添加了`XericSerializerFormatter`序列化器,用于通配处理多种序列化器目标。 + 目前默认提供了 Json格式化 和 bin格式化 。 + 下面展示如何使用: + ``` + List obj = new List() { 1, 2, 3}; + // 序列化目标 + var content = JsonSerializerFormatter.formatter.Serializer(obj); + // 反序列化目标 + obj = JsonSerializerFormatter.Deserializer.Serializer(content); + ``` + + 为了方便使用,文件句柄支持填入序列化器来实现更多的格式: + ``` + // 类型太长了可以使用声名简化 + using static XericLibrary.Runtime.MacroLibrary.MacroFile.CrossPlatformFileHandle; + using static XericLibrary.Runtime.MacroLibrary.MacroFile.JsonSerializerFormatter; + + ActiveFileHandle.AsyncWriteXIntoFile( + ActiveFileHandle.CombineStreamingAssets("myFile.json"), + uriConfig, null, s => Debug.LogError(s), formatter); + ```