2023-06-14 18:52:59 +08:00
|
|
|
|
using System.IO;
|
2023-05-25 16:38:33 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
2023-06-14 18:52:59 +08:00
|
|
|
|
using YooAsset;
|
2023-05-25 16:38:33 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-06-14 18:52:59 +08:00
|
|
|
|
/// 内置文件查询服务类
|
2023-05-25 16:38:33 +08:00
|
|
|
|
/// </summary>
|
2023-06-14 18:52:59 +08:00
|
|
|
|
public class GameQueryServices : IQueryServices
|
2023-05-25 16:38:33 +08:00
|
|
|
|
{
|
2023-06-14 18:52:59 +08:00
|
|
|
|
public bool QueryStreamingAssets(string fileName)
|
2023-05-25 16:38:33 +08:00
|
|
|
|
{
|
2023-06-14 18:52:59 +08:00
|
|
|
|
// 注意:fileName包含文件格式
|
|
|
|
|
|
return StreamingAssetsHelper.FileExists(fileName);
|
2023-05-25 16:38:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-09 12:02:00 +08:00
|
|
|
|
/// <summary>
|
2023-06-14 18:52:59 +08:00
|
|
|
|
/// StreamingAssets目录下资源查询帮助类
|
2023-02-09 12:02:00 +08:00
|
|
|
|
/// </summary>
|
2023-06-14 18:52:59 +08:00
|
|
|
|
public sealed class StreamingAssetsHelper
|
2023-02-09 12:02:00 +08:00
|
|
|
|
{
|
2023-06-14 18:52:59 +08:00
|
|
|
|
private static bool _isInit = false;
|
|
|
|
|
|
private static readonly HashSet<string> _cacheData = new HashSet<string>();
|
2023-02-24 16:52:50 +08:00
|
|
|
|
|
2023-06-16 14:10:33 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
public static void Init() { _isInit = true; }
|
|
|
|
|
|
public static bool FileExists(string fileName)
|
|
|
|
|
|
{
|
|
|
|
|
|
return File.Exists(System.IO.Path.Combine(Application.streamingAssetsPath, "BuildinFiles", fileName));
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
2023-06-14 18:52:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void Init()
|
2023-02-24 16:52:50 +08:00
|
|
|
|
{
|
2023-06-14 18:52:59 +08:00
|
|
|
|
if (_isInit == false)
|
2023-02-24 16:52:50 +08:00
|
|
|
|
{
|
2023-06-14 18:52:59 +08:00
|
|
|
|
_isInit = true;
|
|
|
|
|
|
var manifest = Resources.Load<BuildinFileManifest>("BuildinFileManifest");
|
|
|
|
|
|
foreach (string fileName in manifest.BuildinFiles)
|
2023-02-24 16:52:50 +08:00
|
|
|
|
{
|
2023-06-14 18:52:59 +08:00
|
|
|
|
_cacheData.Add(fileName);
|
2023-02-24 16:52:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-06-14 18:52:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 内置文件查询方法
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool FileExists(string fileName)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_isInit == false)
|
|
|
|
|
|
Init();
|
|
|
|
|
|
|
|
|
|
|
|
return _cacheData.Contains(fileName);
|
2023-02-24 16:52:50 +08:00
|
|
|
|
}
|
2023-06-16 14:10:33 +08:00
|
|
|
|
#endif
|
2023-02-22 15:29:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-14 18:52:59 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
internal class PreprocessBuild : UnityEditor.Build.IPreprocessBuildWithReport
|
2023-02-22 15:29:00 +08:00
|
|
|
|
{
|
2023-06-14 18:52:59 +08:00
|
|
|
|
public int callbackOrder { get { return 0; } }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在构建应用程序前处理
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
|
2023-02-22 15:29:00 +08:00
|
|
|
|
{
|
2023-06-14 18:52:59 +08:00
|
|
|
|
var manifest = ScriptableObject.CreateInstance<BuildinFileManifest>();
|
|
|
|
|
|
|
|
|
|
|
|
string folderPath = $"{Application.dataPath}/StreamingAssets/BuildinFiles";
|
|
|
|
|
|
DirectoryInfo root = new DirectoryInfo(folderPath);
|
|
|
|
|
|
FileInfo[] files = root.GetFiles();
|
|
|
|
|
|
foreach (var fileInfo in files)
|
2023-02-22 15:29:00 +08:00
|
|
|
|
{
|
2023-06-14 18:52:59 +08:00
|
|
|
|
if (fileInfo.Extension == ".meta")
|
|
|
|
|
|
continue;
|
|
|
|
|
|
if (fileInfo.Name.StartsWith("PackageManifest_"))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
manifest.BuildinFiles.Add(fileInfo.Name);
|
2023-02-22 15:29:00 +08:00
|
|
|
|
}
|
2023-06-14 18:52:59 +08:00
|
|
|
|
|
|
|
|
|
|
string saveFilePath = "Assets/Resources/BuildinFileManifest.asset";
|
|
|
|
|
|
if (File.Exists(saveFilePath))
|
|
|
|
|
|
File.Delete(saveFilePath);
|
|
|
|
|
|
if (Directory.Exists("Assets/Resources") == false)
|
|
|
|
|
|
Directory.CreateDirectory("Assets/Resources");
|
|
|
|
|
|
UnityEditor.AssetDatabase.CreateAsset(manifest, saveFilePath);
|
|
|
|
|
|
UnityEditor.AssetDatabase.SaveAssets();
|
|
|
|
|
|
UnityEditor.AssetDatabase.Refresh();
|
|
|
|
|
|
Debug.Log($"内置资源清单保存成功 : {saveFilePath}");
|
2023-02-22 15:29:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-06-14 18:52:59 +08:00
|
|
|
|
#endif
|