行业资讯
06-02-YooAsset源码-Unity内置文件系统实现
源码-内置文件系统实现篇章06-源码深度-文件系统与下载模块阅读时间约 25 分钟前置知识了解 IFileSystem 接口一、引言本章将深入解析 YooAsset 内置文件系统DefaultBuildinFileSystem、DefaultCacheFileSystem、DefaultDeliveryFileSystem的源码实现。这些内置文件系统是 YooAsset 的默认实现覆盖了主流平台的需求。深入理解这些实现有助于开发者在遇到跨平台问题时进行调试和优化。YooAsset 的内置文件系统针对不同平台的特点进行了优化本章将详细解析这些优化策略。二、StreamingAssetsFileSystem 实现2.1 StreamingAssetsFileSystem 概述StreamingAssetsFileSystem 是基于 Unity StreamingAssets 的文件系统特点用于访问打包在游戏包体内的资源不同平台的访问方式不同资源是只读的实现位置YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs2.2 跨平台实现public class DefaultBuildinFileSystem : FileSystemBase { public override bool Exists(string filePath) { string fullPath Path.Combine(RootDirectory, filePath); #if UNITY_EDITOR return File.Exists(fullPath); #elif UNITY_ANDROID return AndroidExists(fullPath); #elif UNITY_IOS return File.Exists(fullPath); #elif UNITY_WEBGL return WebGLExists(fullPath); #else return File.Exists(fullPath); #endif } }跨平台条件编译详解使用 #if 条件编译处理不同平台的差异。Editor 平台详解Editor 平台下直接使用 File.Exists 检查文件。Android 平台详解Android 平台下需要使用 UnityWebRequest 访问 jar 协议。iOS 平台详解iOS 平台下直接使用 File.Exists 检查文件。WebGL 平台详解WebGL 平台下需要使用 UnityWebRequest 访问 HTTP 协议。2.3 Android 平台实现private bool AndroidExists(string filePath) { using (UnityWebRequest request UnityWebRequest.Head(filePath)) { request.SendWebRequest(); while (!request.isDone) { } return request.result UnityWebRequest.Result.Success; } }UnityWebRequest.Head 详解使用 HEAD 请求检查文件是否存在HEAD 请求不下载文件内容。同步等待详解使用 while 循环同步等待请求完成在实际代码中应使用异步方式。2.4 WebGL 平台实现private bool WebGLExists(string filePath) { using (UnityWebRequest request UnityWebRequest.Head(filePath)) { var asyncOp request.SendWebRequest(); while (!asyncOp.isDone) { } return request.result UnityWebRequest.Result.Success; } }WebGL 限制详解WebGL 平台不支持本地文件访问所有文件都需要通过 HTTP 访问。Service Worker 缓存详解WebGL 平台可以使用 Service Worker 实现本地缓存。三、PersistentFileSystem 实现3.1 PersistentFileSystem 概述PersistentFileSystem 是基于 Unity persistentDataPath 的文件系统特点用于持久化存储已下载的资源跨平台兼容性好支持读写操作实现位置YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs3.2 跨平台路径public class DefaultCacheFileSystem : FileSystemBase { public override void OnCreate(string packageName, string rootDirectory) { base.OnCreate(packageName, rootDirectory); // 确保缓存目录存在 if (!Directory.Exists(RootDirectory)) Directory.CreateDirectory(RootDirectory); } private string GetFullPath(string filePath) { return Path.Combine(RootDirectory, filePath); } }持久化路径详解iOSApplication.persistentDataPathApplication/Documents/AndroidApplication.persistentDataPath/storage/emulated/0/Android/data/package/filesWindows%USERPROFILE%/AppData/LocalLow/company/product目录创建详解在 OnCreate 时确保缓存目录存在。3.3 读写实现public override byte[] ReadFile(string filePath) { string fullPath GetFullPath(filePath); if (File.Exists(fullPath)) return File.ReadAllBytes(fullPath); return null; } public override bool WriteFile(string filePath, byte[] fileData) { string fullPath GetFullPath(filePath); string directory Path.GetDirectoryName(fullPath); if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); File.WriteAllBytes(fullPath, fileData); return true; }读取实现详解使用 File.ReadAllBytes 读取整个文件到内存。写入实现详解使用 File.WriteAllBytes 写入文件。先创建目录再写入文件。3.4 性能优化public override byte[] ReadFile(string filePath) { string fullPath GetFullPath(filePath); if (File.Exists(fullPath)) { // 使用 FileShare.Read 允许其他进程读取 using (FileStream fs new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (BinaryReader reader new BinaryReader(fs)) { return reader.ReadBytes((int)fs.Length); } } return null; }流式读取详解使用 FileStream 流式读取文件避免大文件占用过多内存。FileShare 详解使用 FileShare.Read 允许其他进程同时读取文件。四、WebFileSystem 实现4.1 WebFileSystem 概述WebFileSystem 是基于 Web 平台的文件系统特点用于 WebGL 平台不支持本地文件访问通过 HTTP 访问远程资源实现位置YooAsset/Runtime/FileSystem/DefaultDeliveryFileSystem/DefaultDeliveryFileSystem.cs4.2 HTTP 访问实现public class DefaultDeliveryFileSystem : FileSystemBase { public override byte[] ReadFile(string filePath) { string url GetFileURL(filePath); using (UnityWebRequest request UnityWebRequest.Get(url)) { var asyncOp request.SendWebRequest(); while (!asyncOp.isDone) { } if (request.result UnityWebRequest.Result.Success) return request.downloadHandler.data; return null; } } private string GetFileURL(string filePath) { return Path.Combine(RootDirectory, filePath).Replace(\\, /); } }HTTP GET 详解使用 UnityWebRequest.Get 发起 HTTP GET 请求。URL 构造详解将 RootDirectory 和 filePath 组合成完整的 URL。4.3 HTTP 缓存public class WebCacheFileSystem : DefaultDeliveryFileSystem { private Dictionarystring, byte[] _memoryCache new Dictionarystring, byte[](); public override byte[] ReadFile(string filePath) { if (_memoryCache.TryGetValue(filePath, out var data)) return data; data base.ReadFile(filePath); if (data ! null) _memoryCache[filePath] data; return data; } }内存缓存详解使用 Dictionary 实现内存缓存避免重复 HTTP 请求。缓存限制详解内存缓存有大小限制需要 LRU 策略管理。4.4 IndexedDB 缓存WebGLpublic class IndexedDBCache { public void Save(string key, byte[] data); public byte[] Load(string key); public void Delete(string key); }IndexedDB 详解IndexedDB 是浏览器提供的本地数据库可用于持久化缓存。应用场景详解WebGL 平台下使用 IndexedDB 实现持久化缓存。五、RawFileFileSystem 实现5.1 RawFileFileSystem 概述RawFileFileSystem 是基于 Resources 目录的文件系统特点用于访问 Resources 目录下的资源资源会被打包到安装包不需要打 AssetBundle实现位置YooAsset/Runtime/FileSystem/RawFileSystem/RawFileSystem.cs5.2 Resources 加载实现public class RawFileSystem : FileSystemBase { public override byte[] ReadFile(string filePath) { TextAsset textAsset Resources.LoadTextAsset(filePath); if (textAsset ! null) return textAsset.bytes; return null; } public override string ReadFileText(string filePath) { TextAsset textAsset Resources.LoadTextAsset(filePath); if (textAsset ! null) return textAsset.text; return null; } }Resources.Load 详解使用 Resources.Load 加载 Resources 目录下的资源。TextAsset 详解TextAsset 是 Unity 的文本资源类型可以获取字节或文本。5.3 RawFile 的应用配置文件将 JSON、XML 等配置文件放在 Resources 目录。小型资源将小图片、音频片段等放在 Resources 目录。本地化文件将本地化字符串文件放在 Resources 目录。六、文件系统错误处理6.1 错误类型文件系统可能遇到以下错误文件不存在读取失败写入失败权限不足磁盘空间不足6.2 错误处理实现public override byte[] ReadFile(string filePath) { string fullPath GetFullPath(filePath); if (!File.Exists(fullPath)) { Debug.LogWarning($File not found: {fullPath}); return null; } try { return File.ReadAllBytes(fullPath); } catch (Exception ex) { Debug.LogError($Failed to read file {fullPath}: {ex.Message}); return null; } }文件不存在处理详解返回 null调用方根据 null 处理。异常捕获详解使用 try-catch 捕获读取异常。6.3 重试机制public override byte[] ReadFileWithRetry(string filePath, int maxRetries 3) { for (int i 0; i maxRetries; i) { try { return ReadFile(filePath); } catch (Exception ex) { if (i maxRetries - 1) throw; Thread.Sleep(100); } } return null; }重试策略详解失败时重试一定次数。退避策略详解每次重试前等待一段时间。七、总结本章深入解析了 YooAsset 内置文件系统的实现包括StreamingAssetsFileSystem基于 StreamingAssets 的文件系统跨平台实现PersistentFileSystem基于 persistentDataPath 的持久化文件系统WebFileSystem基于 HTTP 的 Web 平台文件系统RawFileFileSystem基于 Resources 目录的文件系统错误处理错误类型、错误处理实现、重试机制通过深入理解这些实现开发者可以更好地使用和优化 YooAsset 的文件系统。下一篇下载系统DownloadSystem
郑州网站建设
网页设计
企业官网