using System.IO; using System.Collections.Generic; using UnityEngine; using YooAsset; namespace PhxhSDK.AOT { /// /// 资源文件查询服务类 /// public class GameQueryServices : IBuildinQueryServices { public bool Query(string packageName, string fileName, string fileCRC) { // 注意:fileName包含文件格式 return StreamingAssetsHelper.FileExists(packageName, fileName); } } public class GameDeliveryServices : IDeliveryQueryServices { public bool Query(string packageName, string fileName, string fileCRC) { return false; } public string GetFilePath(string packageName, string fileName) { throw new System.NotImplementedException(); } } #if UNITY_EDITOR /// /// StreamingAssets目录下资源查询帮助类 /// public sealed class StreamingAssetsHelper { public static void Init() {} public static bool FileExists(string packageName, string fileName) { string filePath = Path.Combine(Application.streamingAssetsPath, StreamingAssetsDefine.RootFolderName, packageName, fileName); return File.Exists(filePath); } } #else /// /// StreamingAssets目录下资源查询帮助类 /// public sealed class StreamingAssetsHelper { private static bool _isInit = false; private static readonly HashSet _cacheData = new HashSet(); /// /// 初始化 /// public static void Init() { if (_isInit == false) { _isInit = true; var manifest = Resources.Load("BuildinFileManifest"); if (manifest != null) { foreach (string fileName in manifest.BuildinFiles) { _cacheData.Add(fileName); } } } } /// /// 内置文件查询方法 /// public static bool FileExists(string packageName, string fileName) { if (_isInit == false) Init(); return _cacheData.Contains(fileName); } } #endif #if UNITY_EDITOR internal class PreprocessBuild : UnityEditor.Build.IPreprocessBuildWithReport { public int callbackOrder { get { return 0; } } /// /// 在构建应用程序前处理 /// public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report) { string saveFilePath = "Assets/Resources/BuildinFileManifest.asset"; if (File.Exists(saveFilePath)) File.Delete(saveFilePath); string folderPath = $"{Application.dataPath}/StreamingAssets/{StreamingAssetsDefine.RootFolderName}"; DirectoryInfo root = new DirectoryInfo(folderPath); if (root.Exists == false) { Debug.Log($"没有发现YooAsset内置目录 : {folderPath}"); return; } var manifest = ScriptableObject.CreateInstance(); FileInfo[] files = root.GetFiles("*", SearchOption.AllDirectories); foreach (var fileInfo in files) { if (fileInfo.Extension == ".meta") continue; if (fileInfo.Name.StartsWith("PackageManifest_")) continue; manifest.BuildinFiles.Add(fileInfo.Name); } if (Directory.Exists("Assets/Resources") == false) Directory.CreateDirectory("Assets/Resources"); UnityEditor.AssetDatabase.CreateAsset(manifest, saveFilePath); UnityEditor.AssetDatabase.SaveAssets(); UnityEditor.AssetDatabase.Refresh(); Debug.Log($"一共{manifest.BuildinFiles.Count}个内置文件,内置资源清单保存成功 : {saveFilePath}"); } } #endif }