| | |
| | | using Cysharp.Threading.Tasks; |
| | | using System.Threading; |
| | | using ProjSG.Resource; |
| | | using YooAsset; |
| | | |
| | | |
| | | |
| | | |
| | |
| | | |
| | | public void Init() |
| | | { |
| | | |
| | | |
| | | } |
| | | |
| | | public void Release() |
| | |
| | | } |
| | | #endif |
| | | |
| | | //needExt 是否需要函数内部添加后缀 |
| | | [System.Obsolete("US2: Use LoadAssetAsync<T>(directory, name, needExt) returning UniTask<T> instead.")] |
| | | public T LoadAsset<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object |
| | | { |
| | | directory = directory.Replace("\\", "/"); |
| | | name = name.Replace("\\", "/"); |
| | | // 特殊处理 因为有一层图集的关系 directory要传入的应该是atlas的名字 |
| | | if (typeof(T) == typeof(Sprite)) |
| | | { |
| | | return LoadSprite(directory, name) as T; |
| | | } |
| | | else if (typeof(T) == typeof(SkeletonDataAsset)) |
| | | { |
| | | //文件目录调整,name中包含了路径 |
| | | if (name.Contains("/")) |
| | | { |
| | | directory += name.Substring(0, name.LastIndexOf("/")); |
| | | name = name.Substring(name.LastIndexOf("/") + 1); |
| | | } |
| | | // // needExt 是否需要函数内部添加后缀 |
| | | // [System.Obsolete("US2: Use LoadAssetAsync<T>(directory, name, needExt) returning UniTask<T> instead.")] |
| | | // public T LoadAsset<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object |
| | | // { |
| | | // directory = directory.Replace("\\", "/"); |
| | | // name = name.Replace("\\", "/"); |
| | | // // 特殊处理 因为有一层图集的关系 directory要传入的应该是atlas的名字 |
| | | // if (typeof(T) == typeof(Sprite)) |
| | | // { |
| | | // return LoadSprite(directory, name) as T; |
| | | // } |
| | | // else if (typeof(T) == typeof(SkeletonDataAsset)) |
| | | // { |
| | | // //文件目录调整,name中包含了路径 |
| | | // if (name.Contains("/")) |
| | | // { |
| | | // directory += name.Substring(0, name.LastIndexOf("/")); |
| | | // name = name.Substring(name.LastIndexOf("/") + 1); |
| | | // } |
| | | |
| | | } |
| | | // } |
| | | |
| | | return LoadAssetInternal<T>(directory, name, needExt); |
| | | } |
| | | // return LoadAssetInternal<T>(directory, name, needExt); |
| | | // } |
| | | |
| | | //needExt 是否需要函数内部添加后缀 |
| | | private T LoadAssetInternal<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object |
| | | { |
| | | T asset = null; |
| | | var path = ($"Assets/ResourcesOut/{directory}/{name}" + (needExt ? GetExtension(typeof(T)) : "")).Replace("//", "/").Trim().Replace("\\", "/"); |
| | | // //needExt 是否需要函数内部添加后缀 |
| | | // private T LoadAssetInternal<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object |
| | | // { |
| | | // // 已禁用同步加载,强制业务全部走异步API。 |
| | | // throw new NotSupportedException("同步资源加载已禁用,请使用异步接口"); |
| | | // } |
| | | |
| | | if (!AssetSource.isUseAssetBundle) |
| | | { |
| | | #if UNITY_EDITOR |
| | | asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path); |
| | | #endif |
| | | } |
| | | else |
| | | { |
| | | // US1: Route through YooAssetService sync wrapper (transitional) |
| | | #pragma warning disable CS0612, CS0618 |
| | | asset = YooAssetService.Instance.LoadAssetSync<T>(path); |
| | | #pragma warning restore CS0612, CS0618 |
| | | } |
| | | // [System.Obsolete("US2: Use LoadConfigAsync returning UniTask<string[]> instead.")] |
| | | // public string[] LoadConfig(string name) |
| | | // { |
| | | // string path = string.Empty; |
| | | // #if UNITY_EDITOR |
| | | // if (!AssetSource.isUseAssetBundle) |
| | | // { |
| | | // path = ResourcesPath.CONFIG_FODLER + "/" + name + ".txt"; |
| | | // } |
| | | // else |
| | | // #endif |
| | | // { |
| | | // path = AssetVersionUtility.GetAssetFilePath($"config/{name}.txt"); |
| | | // } |
| | | |
| | | if (asset == null) |
| | | { |
| | | Debug.LogErrorFormat("LoadAsset() => 加载不到资源: {0}", path); |
| | | } |
| | | // return File.ReadAllLines(path); |
| | | // } |
| | | |
| | | return asset; |
| | | } |
| | | |
| | | [System.Obsolete("US2: Use LoadConfigAsync returning UniTask<string[]> instead.")] |
| | | public string[] LoadConfig(string name) |
| | | { |
| | | string path = string.Empty; |
| | | #if UNITY_EDITOR |
| | | if (!AssetSource.isUseAssetBundle) |
| | | { |
| | | path = ResourcesPath.CONFIG_FODLER + "/" + name + ".txt"; |
| | | } |
| | | else |
| | | #endif |
| | | { |
| | | path = AssetVersionUtility.GetAssetFilePath($"config/{name}.txt"); |
| | | } |
| | | |
| | | return File.ReadAllLines(path); |
| | | } |
| | | |
| | | private Sprite LoadSprite(string atlasName, string spriteName) |
| | | { |
| | | if (!AssetSource.isUseAssetBundle) |
| | | { |
| | | #pragma warning disable CS0618 // Obsolete — legacy sync fallback |
| | | SpriteAtlas atlas = LoadAsset<SpriteAtlas>("Sprite", atlasName.Replace("Sprite/", "")); |
| | | #pragma warning restore CS0618 |
| | | if (null == atlas) |
| | | { |
| | | return null; |
| | | } |
| | | return atlas.GetSprite(spriteName); |
| | | } |
| | | else |
| | | return LoadAssetInternal<Sprite>(atlasName, spriteName); |
| | | } |
| | | |
| | | //needExt 是否需要函数内部添加后缀 |
| | | [System.Obsolete("US2: Use LoadAssetAsync<T>(directory, name, needExt) returning UniTask<T> instead.")] |
| | | public void LoadAssetAsync<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack, bool needExt = true) where T : UnityEngine.Object |
| | | { |
| | | directory = directory.Replace("\\", "/"); |
| | | name = name.Replace("\\", "/"); |
| | | |
| | | // 特殊处理 因为有一层图集的关系 directory要传入的应该是atlas的名字 |
| | | if (typeof(T) == typeof(Sprite)) |
| | | { |
| | | LoadSpriteAsync<T>(directory, name, callBack); |
| | | return; |
| | | } |
| | | |
| | | LoadAssetAsyncInternal<T>(directory, name, callBack, needExt); |
| | | } |
| | | |
| | | public async UniTask<T> LoadAssetAsync<T>(string directory, string name) where T : UnityEngine.Object |
| | | { |
| | |
| | | |
| | | public async UniTask<T> LoadAssetAsync<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object |
| | | { |
| | | var tcs = new UniTaskCompletionSource<T>(); |
| | | LoadAssetAsync<T>(directory, name, (isLoaded, asset) => { |
| | | if (isLoaded) |
| | | { |
| | | tcs.TrySetResult(asset as T); |
| | | } |
| | | else |
| | | { |
| | | tcs.TrySetException(new Exception($"Failed to load asset: {directory}/{name}")); |
| | | } |
| | | }, needExt); |
| | | return await tcs.Task; |
| | | // 新版直接走 YooAsset/异步加载 |
| | | // 这里假设有 YooAssetService.Instance.LoadAssetAsync<T>(...) 可用 |
| | | string path = directory.EndsWith("/") ? directory + name : directory + "/" + name; |
| | | return await YooAssetService.Instance.LoadAssetAsync<T>(path); |
| | | } |
| | | |
| | | private void LoadSpriteAsync<T>(string atlasName, string spriteName, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object |
| | |
| | | } |
| | | } |
| | | |
| | | [System.Obsolete("US1: Use YooAssetService.ReleaseHandle or UnloadUnusedAssetsAsync instead.")] |
| | | public void UnloadAsset(string assetBundleName, string assetName) |
| | | public void UnloadAsset(string directory, string assetName) |
| | | { |
| | | // US1: AssetBundleUtility unload no longer effective since assets loaded via YooAsset. |
| | | // Proper unload handled via YooAssetService handle-based release. |
| | | } |
| | | |
| | | [System.Obsolete("US1: Use YooAssetService.UnloadUnusedAssetsAsync instead.")] |
| | | public void UnloadAssetBundle(string assetBundleName, bool unloadAllLoadedObjects, bool includeDependenice) |
| | | { |
| | | // US1: AssetBundleUtility unload no longer effective since assets loaded via YooAsset. |
| | | string path = directory.EndsWith("/") ? directory + assetName : directory + "/" + assetName; |
| | | YooAssetService.Instance.UnloadAsset(path); |
| | | } |
| | | |
| | | public string GetAssetFilePath(string _assetKey) |
| | |
| | | /// </summary> |
| | | public async UniTask<string[]> LoadConfigAsync(string name, CancellationToken ct = default) |
| | | { |
| | | // AB 模式(含 WebGL): 使用 YooAsset RawFile 加载(配置文件在 YooAsset 沙盒中) |
| | | // AB 模式(含 WebGL): 使用 YooAsset 加载配置文件 |
| | | if (AssetSource.isUseAssetBundle) |
| | | { |
| | | var location = $"Assets/ResourcesOut/Config/{name}.txt"; |
| | | try |
| | | { |
| | | var location = $"Assets/ResourcesOut/Config/{name}.txt"; |
| | | #if UNITY_WEBGL |
| | | // WebGL: WebServerFileSystem 不支持 LoadRawFileAsync,改用 LoadAssetAsync<TextAsset> |
| | | // .txt 文件在 Unity 中以 TextAsset 形式导入,WebGL 支持此加载方式 |
| | | var asset = await ProjSG.Resource.YooAssetService.Instance.LoadAssetAsync( |
| | | location, typeof(UnityEngine.TextAsset), 0, ct) as UnityEngine.TextAsset; |
| | | if (asset != null && !string.IsNullOrEmpty(asset.text)) |
| | | return asset.text.Split(new[] { "\r\n", "\n" }, System.StringSplitOptions.None); |
| | | #else |
| | | var text = await ProjSG.Resource.YooAssetService.Instance.LoadRawFileTextAsync(location, ct); |
| | | if (!string.IsNullOrEmpty(text)) |
| | | { |
| | | return text.Split(new[] { "\r\n", "\n" }, System.StringSplitOptions.None); |
| | | } |
| | | #endif |
| | | } |
| | | catch (System.Exception ex) |
| | | { |