Updated to use StreamingAssets to request BuildinCatalog.json

pull/509/head
MichaelO 2025-03-12 19:07:41 +08:00
parent c7d678282b
commit 0b4f6d6639
10 changed files with 110 additions and 28 deletions

View File

@ -15,6 +15,7 @@ namespace YooAsset.Editor
if (buildParametersContext.Parameters.BuildinFileCopyOption != EBuildinFileCopyOption.None)
{
CopyBuildinFilesToStreaming(buildParametersContext, manifestContext.Manifest);
DefaultBuildinFileSystemBuild.ExportBuildinCatalogFile();
}
}
}

View File

@ -16,6 +16,7 @@ namespace YooAsset.Editor
if (buildParameters.BuildinFileCopyOption != EBuildinFileCopyOption.None)
{
CopyBuildinFilesToStreaming(buildParametersContext, manifestContext.Manifest);
DefaultBuildinFileSystemBuild.ExportBuildinCatalogFile();
}
}
}

View File

@ -15,6 +15,7 @@ namespace YooAsset.Editor
if (buildParametersContext.Parameters.BuildinFileCopyOption != EBuildinFileCopyOption.None)
{
CopyBuildinFilesToStreaming(buildParametersContext, manifestContext.Manifest);
DefaultBuildinFileSystemBuild.ExportBuildinCatalogFile();
}
}
}

View File

@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 内置资源清单目录
/// </summary>
internal class DefaultBuildinFileCatalog : ScriptableObject
[Serializable]
internal class DefaultBuildinFileCatalog
{
[Serializable]
public class FileWrapper

View File

@ -338,8 +338,8 @@ namespace YooAsset
}
public string GetCatalogFileLoadPath()
{
string fileName = Path.GetFileNameWithoutExtension(DefaultBuildinFileSystemDefine.BuildinCatalogFileName);
return YooAssetSettingsData.GetYooResourcesLoadPath(PackageName, fileName);
string fileName = DefaultBuildinFileSystemDefine.BuildinCatalogFileName;
return PathUtility.Combine(YooAssetSettingsData.GetRequestYooDefaultBuildinRoot(), PackageName, fileName);
}
/// <summary>

View File

@ -5,22 +5,16 @@ using UnityEngine;
namespace YooAsset
{
public class DefaultBuildinFileSystemBuild : UnityEditor.Build.IPreprocessBuildWithReport
public class DefaultBuildinFileSystemBuild
{
public int callbackOrder { get { return 0; } }
/// <summary>
/// 在构建应用程序前自动生成内置资源目录文件。
/// 原理搜索StreamingAssets目录下的所有资源文件然后将这些文件信息写入文件并存储在Resources目录下。
/// 输出包裹的内置资源目录文件
/// </summary>
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
/// <exception cref="System.Exception"></exception>
public static void ExportBuildinCatalogFile()
{
YooLogger.Log("Begin to create catalog file !");
string savePath = YooAssetSettingsData.GetYooResourcesFullPath();
if (UnityEditor.AssetDatabase.DeleteAsset(savePath))
UnityEditor.AssetDatabase.Refresh();
string rootPath = YooAssetSettingsData.GetYooDefaultBuildinRoot();
DirectoryInfo rootDirectory = new DirectoryInfo(rootPath);
if (rootDirectory.Exists == false)
@ -87,7 +81,7 @@ namespace YooAsset
}
// 创建内置清单实例
var buildinFileCatalog = ScriptableObject.CreateInstance<DefaultBuildinFileCatalog>();
var buildinFileCatalog = new DefaultBuildinFileCatalog();
buildinFileCatalog.PackageName = packageName;
buildinFileCatalog.PackageVersion = packageVersion;
@ -111,6 +105,8 @@ namespace YooAsset
continue;
if (fileInfo.Name == $"{packageName}_{packageVersion}.report")
continue;
if (fileInfo.Name == DefaultBuildinFileSystemDefine.BuildinCatalogFileName)
continue;
string fileName = fileInfo.Name;
if (fileMapping.TryGetValue(fileName, out string bundleGUID))
@ -125,18 +121,13 @@ namespace YooAsset
}
// 创建输出目录
string fullPath = YooAssetSettingsData.GetYooResourcesFullPath();
string fullPath = YooAssetSettingsData.GetYooDefaultBuildinRoot();
string saveFilePath = $"{fullPath}/{packageName}/{DefaultBuildinFileSystemDefine.BuildinCatalogFileName}";
FileUtility.CreateFileDirectory(saveFilePath);
// 创建输出文件
UnityEditor.AssetDatabase.CreateAsset(buildinFileCatalog, saveFilePath);
UnityEditor.EditorUtility.SetDirty(buildinFileCatalog);
#if UNITY_2019
UnityEditor.AssetDatabase.SaveAssets();
#else
UnityEditor.AssetDatabase.SaveAssetIfDirty(buildinFileCatalog);
#endif
File.WriteAllText(saveFilePath, JsonUtility.ToJson(buildinFileCatalog, false));
UnityEditor.AssetDatabase.Refresh();
Debug.Log($"Succeed to save buildin file catalog : {saveFilePath}");
return true;

View File

@ -6,6 +6,6 @@ namespace YooAsset
/// <summary>
/// 内置清单文件名称
/// </summary>
public const string BuildinCatalogFileName = "BuildinCatalog.asset";
public const string BuildinCatalogFileName = "BuildinCatalog.json";
}
}

View File

@ -1,4 +1,5 @@
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
@ -8,9 +9,11 @@ namespace YooAsset
{
None,
LoadCatalog,
WaitForRequest,
Done,
}
private UnityWebRequest _request;
private readonly DefaultBuildinFileSystem _fileSystem;
private ESteps _steps = ESteps.None;
@ -28,12 +31,39 @@ namespace YooAsset
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
string catalogFilePath = _fileSystem.GetCatalogFileLoadPath();
if (_steps == ESteps.LoadCatalog)
{
string catalogFilePath = _fileSystem.GetCatalogFileLoadPath();
var catalog = Resources.Load<DefaultBuildinFileCatalog>(catalogFilePath);
_request = UnityWebRequest.Get(catalogFilePath);
_request.SendWebRequest();
_steps = ESteps.WaitForRequest;
return;
}
if (_steps == ESteps.WaitForRequest)
{
// 等待请求完成
if (!_request.isDone)
return;
if (_request.result != UnityWebRequest.Result.Success)
{
_request.Dispose();
_request = null;
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load catalog file: {_request.error}";
return;
}
// 解析 JSON
string jsonText = _request.downloadHandler.text;
var catalog = JsonUtility.FromJson<DefaultBuildinFileCatalog>(jsonText);
if (catalog == null)
{
_request.Dispose();
_request = null;
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load catalog file : {catalogFilePath}";
@ -42,6 +72,8 @@ namespace YooAsset
if (catalog.PackageName != _fileSystem.PackageName)
{
_request.Dispose();
_request = null;
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Catalog file package name {catalog.PackageName} cannot match the file system package name {_fileSystem.PackageName}";
@ -55,6 +87,8 @@ namespace YooAsset
}
YooLogger.Log($"Package '{_fileSystem.PackageName}' buildin catalog files count : {catalog.Wrappers.Count}");
_request.Dispose();
_request = null;
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}

View File

@ -3,6 +3,7 @@ using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace YooAsset
{
@ -12,9 +13,11 @@ namespace YooAsset
{
None,
LoadCatalog,
WaitForRequest,
Done,
}
private UnityWebRequest _request;
private readonly DefaultWebServerFileSystem _fileSystem;
private ESteps _steps = ESteps.None;
@ -36,12 +39,39 @@ namespace YooAsset
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
string catalogFilePath = _fileSystem.GetCatalogFileLoadPath();
if (_steps == ESteps.LoadCatalog)
{
string catalogFilePath = _fileSystem.GetCatalogFileLoadPath();
var catalog = Resources.Load<DefaultBuildinFileCatalog>(catalogFilePath);
_request = UnityWebRequest.Get(catalogFilePath);
_request.SendWebRequest();
_steps = ESteps.WaitForRequest;
return;
}
if (_steps == ESteps.WaitForRequest)
{
// 等待请求完成
if (!_request.isDone)
return;
if (_request.result != UnityWebRequest.Result.Success)
{
_request.Dispose();
_request = null;
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load web server catalog file: {_request.error}";
return;
}
// 解析 JSON
string jsonText = _request.downloadHandler.text;
var catalog = JsonUtility.FromJson<DefaultBuildinFileCatalog>(jsonText);
if (catalog == null)
{
_request.Dispose();
_request = null;
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load web server catalog file : {catalogFilePath}";
@ -50,6 +80,8 @@ namespace YooAsset
if (catalog.PackageName != _fileSystem.PackageName)
{
_request.Dispose();
_request = null;
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Web server catalog file package name {catalog.PackageName} cannot match the file system package name {_fileSystem.PackageName}";
@ -64,6 +96,8 @@ namespace YooAsset
}
YooLogger.Log($"Package '{_fileSystem.PackageName}' catalog files count : {catalog.Wrappers.Count}");
_request.Dispose();
_request = null;
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}

View File

@ -220,6 +220,26 @@ namespace YooAsset
else
return PathUtility.Combine(Application.streamingAssetsPath, Setting.DefaultYooFolderName);
}
internal static string GetRequestYooDefaultBuildinRoot()
{
if (string.IsNullOrEmpty(Setting.DefaultYooFolderName))
return GetRequestStreamingAssetsPath();
else
return PathUtility.Combine(GetRequestStreamingAssetsPath(), Setting.DefaultYooFolderName);
}
/// <summary>
/// 获取UnityWebRequest StreamingAssets的路径 (OSX and iOS 需要加 file://)
/// </summary>
internal static string GetRequestStreamingAssetsPath()
{
#if UNITY_STANDALONE_OSX || UNITY_IOS
return $"file://{Application.streamingAssetsPath}";
#else
return Application.streamingAssetsPath;
#endif
}
#endregion
}
}