diff --git a/Assets/YooAsset/Runtime/Services.meta b/Assets/YooAsset/Runtime/Services.meta new file mode 100644 index 0000000..4f521cc --- /dev/null +++ b/Assets/YooAsset/Runtime/Services.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: bf4cc5f446778bc428a69fdcd183e447 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/Services/DefaultLocationServices.cs b/Assets/YooAsset/Runtime/Services/DefaultLocationServices.cs new file mode 100644 index 0000000..b50e14f --- /dev/null +++ b/Assets/YooAsset/Runtime/Services/DefaultLocationServices.cs @@ -0,0 +1,77 @@ +using System.IO; + +namespace YooAsset +{ + public class DefaultLocationServices : ILocationServices + { + private readonly string _resourceRoot; + + public DefaultLocationServices(string resourceRoot) + { + if (string.IsNullOrEmpty(resourceRoot) == false) + _resourceRoot = PathHelper.GetRegularPath(resourceRoot); + } + public string ConvertLocationToAssetPath(YooAssets.EPlayMode playMode, string location) + { + if (playMode == YooAssets.EPlayMode.EditorPlayMode) + { + string filePath = CombineAssetPath(_resourceRoot, location); + return FindDatabaseAssetPath(filePath); + } + else + { + return CombineAssetPath(_resourceRoot, location); + } + } + + /// + /// 合并资源路径 + /// + private static string CombineAssetPath(string root, string location) + { + if (string.IsNullOrEmpty(root)) + return location; + else + return $"{root}/{location}"; + } + + /// + /// 获取AssetDatabase的加载路径 + /// + private static string FindDatabaseAssetPath(string filePath) + { +#if UNITY_EDITOR + if (File.Exists(filePath)) + return filePath; + + // AssetDatabase加载资源需要提供文件后缀格式,然而资源定位地址并没有文件格式信息。 + // 所以我们通过查找该文件所在文件夹内同名的首个文件来确定AssetDatabase的加载路径。 + // 注意:AssetDatabase.FindAssets() 返回文件内包括递归文件夹内所有资源的GUID + string fileName = Path.GetFileName(filePath); + string directory = PathHelper.GetDirectory(filePath); + string[] guids = UnityEditor.AssetDatabase.FindAssets(string.Empty, new[] { directory }); + for (int i = 0; i < guids.Length; i++) + { + string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(guids[i]); + + if (UnityEditor.AssetDatabase.IsValidFolder(assetPath)) + continue; + + string assetDirectory = PathHelper.GetDirectory(assetPath); + if (assetDirectory != directory) + continue; + + string assetName = Path.GetFileNameWithoutExtension(assetPath); + if (assetName == fileName) + return assetPath; + } + + // 没有找到同名的资源文件 + YooLogger.Warning($"Not found asset : {filePath}"); + return filePath; +#else + throw new System.NotImplementedException(); +#endif + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/DefaultLocationServices.cs.meta b/Assets/YooAsset/Runtime/Services/DefaultLocationServices.cs.meta new file mode 100644 index 0000000..0d76172 --- /dev/null +++ b/Assets/YooAsset/Runtime/Services/DefaultLocationServices.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8d996937ba73c9b4bb942b8ba6f43398 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/AssetSystem/IBundleServices.cs b/Assets/YooAsset/Runtime/Services/IBundleServices.cs similarity index 100% rename from Assets/YooAsset/Runtime/AssetSystem/IBundleServices.cs rename to Assets/YooAsset/Runtime/Services/IBundleServices.cs diff --git a/Assets/YooAsset/Runtime/AssetSystem/IBundleServices.cs.meta b/Assets/YooAsset/Runtime/Services/IBundleServices.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/AssetSystem/IBundleServices.cs.meta rename to Assets/YooAsset/Runtime/Services/IBundleServices.cs.meta diff --git a/Assets/YooAsset/Runtime/AssetSystem/IDecryptionServices.cs b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs similarity index 100% rename from Assets/YooAsset/Runtime/AssetSystem/IDecryptionServices.cs rename to Assets/YooAsset/Runtime/Services/IDecryptionServices.cs diff --git a/Assets/YooAsset/Runtime/AssetSystem/IDecryptionServices.cs.meta b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/AssetSystem/IDecryptionServices.cs.meta rename to Assets/YooAsset/Runtime/Services/IDecryptionServices.cs.meta diff --git a/Assets/YooAsset/Runtime/Services/ILocationServices.cs b/Assets/YooAsset/Runtime/Services/ILocationServices.cs new file mode 100644 index 0000000..f3c9c11 --- /dev/null +++ b/Assets/YooAsset/Runtime/Services/ILocationServices.cs @@ -0,0 +1,11 @@ + +namespace YooAsset +{ + public interface ILocationServices + { + /// + /// 定位地址转换为资源路径 + /// + string ConvertLocationToAssetPath(YooAssets.EPlayMode playMode, string location); + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Services/ILocationServices.cs.meta b/Assets/YooAsset/Runtime/Services/ILocationServices.cs.meta new file mode 100644 index 0000000..31308ea --- /dev/null +++ b/Assets/YooAsset/Runtime/Services/ILocationServices.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cfc81e18e5b5f6f4b821c7427b34d349 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/Utility/YooHelper.cs b/Assets/YooAsset/Runtime/Utility/YooHelper.cs index 5a57692..f7f8a56 100644 --- a/Assets/YooAsset/Runtime/Utility/YooHelper.cs +++ b/Assets/YooAsset/Runtime/Utility/YooHelper.cs @@ -71,56 +71,6 @@ namespace YooAsset return StringUtility.Format("file:///{0}", path); #elif UNITY_WEBGL return path; -#endif - } - - /// - /// 合并资源路径 - /// - public static string CombineAssetPath(string root, string location) - { - if (string.IsNullOrEmpty(root)) - return location; - else - return $"{root}/{location}"; - } - - /// - /// 获取AssetDatabase的加载路径 - /// - public static string FindDatabaseAssetPath(string filePath) - { -#if UNITY_EDITOR - if (File.Exists(filePath)) - return filePath; - - // AssetDatabase加载资源需要提供文件后缀格式,然而资源定位地址并没有文件格式信息。 - // 所以我们通过查找该文件所在文件夹内同名的首个文件来确定AssetDatabase的加载路径。 - // 注意:AssetDatabase.FindAssets() 返回文件内包括递归文件夹内所有资源的GUID - string fileName = Path.GetFileName(filePath); - string directory = GetDirectory(filePath); - string[] guids = UnityEditor.AssetDatabase.FindAssets(string.Empty, new[] { directory }); - for (int i = 0; i < guids.Length; i++) - { - string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(guids[i]); - - if (UnityEditor.AssetDatabase.IsValidFolder(assetPath)) - continue; - - string assetDirectory = GetDirectory(assetPath); - if (assetDirectory != directory) - continue; - - string assetName = Path.GetFileNameWithoutExtension(assetPath); - if (assetName == fileName) - return assetPath; - } - - // 没有找到同名的资源文件 - YooLogger.Warning($"Not found asset : {filePath}"); - return filePath; -#else - throw new System.NotImplementedException(); #endif } } diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs index 45e4beb..a4eac82 100644 --- a/Assets/YooAsset/Runtime/YooAssets.cs +++ b/Assets/YooAsset/Runtime/YooAssets.cs @@ -10,7 +10,7 @@ namespace YooAsset /// /// 运行模式 /// - private enum EPlayMode + public enum EPlayMode { /// /// 编辑器下模拟运行模式 @@ -28,16 +28,18 @@ namespace YooAsset HostPlayMode, } + /// + /// 初始化参数 + /// public abstract class CreateParameters { /// - /// 资源定位的根路径 - /// 例如:Assets/MyResource + /// 资源定位服务接口 /// - public string LocationRoot; + public ILocationServices LocationServices = null; /// - /// 文件解密接口 + /// 文件解密服务接口 /// public IDecryptionServices DecryptionServices = null; @@ -100,9 +102,9 @@ namespace YooAsset private static bool _isInitialize = false; - private static string _locationRoot; private static EPlayMode _playMode; private static IBundleServices _bundleServices; + private static ILocationServices _locationServices; private static EditorPlayModeImpl _editorPlayModeImpl; private static OfflinePlayModeImpl _offlinePlayModeImpl; private static HostPlayModeImpl _hostPlayModeImpl; @@ -119,6 +121,11 @@ namespace YooAsset if (parameters == null) throw new Exception($"YooAsset create parameters is null."); + if (parameters.LocationServices == null) + throw new Exception($"{nameof(IBundleServices)} is null."); + else + _locationServices = parameters.LocationServices; + #if !UNITY_EDITOR if (parameters is EditorPlayModeParameters) throw new Exception($"Editor play mode only support unity editor."); @@ -149,9 +156,6 @@ namespace YooAsset YooLogger.Warning($"{nameof(parameters.OperationSystemMaxTimeSlice)} minimum value is 33 milliseconds"); } - if (string.IsNullOrEmpty(parameters.LocationRoot) == false) - _locationRoot = PathHelper.GetRegularPath(parameters.LocationRoot); - if (parameters.AutoReleaseInterval > 0) _releaseCD = parameters.AutoReleaseInterval; @@ -241,7 +245,7 @@ namespace YooAsset throw new NotImplementedException(); } } - + /// /// 向网络端请求并更新补丁清单 /// @@ -307,7 +311,7 @@ namespace YooAsset /// public static BundleInfo GetBundleInfo(string location) { - string assetPath = ConvertLocationToAssetPath(location); + string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location); string bundleName = _bundleServices.GetBundleName(assetPath); return _bundleServices.GetBundleInfo(bundleName); } @@ -350,7 +354,7 @@ namespace YooAsset /// 优先级 public static SceneOperationHandle LoadSceneAsync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single, bool activateOnLoad = true, int priority = 100) { - string scenePath = ConvertLocationToAssetPath(location); + string scenePath = _locationServices.ConvertLocationToAssetPath(_playMode, location); var handle = AssetSystem.LoadSceneAsync(scenePath, sceneMode, activateOnLoad, priority); return handle; } @@ -362,7 +366,7 @@ namespace YooAsset /// public static RawFileOperation LoadRawFileAsync(string location, string savePath) { - string assetPath = ConvertLocationToAssetPath(location); + string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location); return AssetSystem.LoadRawFileAsync(assetPath, savePath); } @@ -451,7 +455,7 @@ namespace YooAsset private static AssetOperationHandle LoadAssetInternal(string location, System.Type assetType, bool waitForAsyncComplete) { - string assetPath = ConvertLocationToAssetPath(location); + string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location); var handle = AssetSystem.LoadAssetAsync(assetPath, assetType); if (waitForAsyncComplete) handle.WaitForAsyncComplete(); @@ -459,7 +463,7 @@ namespace YooAsset } private static SubAssetsOperationHandle LoadSubAssetsInternal(string location, System.Type assetType, bool waitForAsyncComplete) { - string assetPath = ConvertLocationToAssetPath(location); + string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location); var handle = AssetSystem.LoadSubAssetsAsync(assetPath, assetType); if (waitForAsyncComplete) handle.WaitForAsyncComplete(); @@ -539,7 +543,7 @@ namespace YooAsset List assetPaths = new List(locations.Length); foreach (var location in locations) { - string assetPath = ConvertLocationToAssetPath(location); + string assetPath = _locationServices.ConvertLocationToAssetPath(_playMode, location); assetPaths.Add(assetPath); } return _hostPlayModeImpl.CreateDownloaderByPaths(assetPaths, downloadingMaxNumber, failedTryAgain); @@ -642,22 +646,6 @@ namespace YooAsset } } } - - /// - /// 定位地址转换为资源路径 - /// - private static string ConvertLocationToAssetPath(string location) - { - if (_playMode == EPlayMode.EditorPlayMode) - { - string filePath = PathHelper.CombineAssetPath(_locationRoot, location); - return PathHelper.FindDatabaseAssetPath(filePath); - } - else - { - return PathHelper.CombineAssetPath(_locationRoot, location); - } - } #endregion } } \ No newline at end of file