/// <summary> /// 获取资源 /// </summary> public T GetAsset <T>(string abPath, string assetName) where T : UnityEngine.Object { T asset = null; AssetBundleTask task = GetTask(abPath, false); task?.Load((ab) => { asset = ab?.LoadAsset <T>(assetName); }); return(asset); }
/// <summary> /// 加载AB包 /// </summary> public AssetBundle LoadAssetBundle(string abPath) { AssetBundle assetbundle = null; AssetBundleTask task = GetTask(abPath, false); task?.Load((ab) => { assetbundle = ab; }); return(assetbundle); }
/// <summary> /// 卸载 /// </summary> public void Unload(string abPath, bool unloadAllLoadedObjects) { string abKey = GetAbKey(abPath); AssetBundleTask task = this[abKey]; if (task != null) { task.Unload(unloadAllLoadedObjects); _taskDic?.Remove(abKey); } }
/// <summary> /// 获取任务 /// </summary> private AssetBundleTask GetTask(string abPath, bool async) { string abKey = GetAbKey(abPath); AssetBundleTask task = this[abKey]; if (task == null) { task = new AssetBundleTask(abPath, async); this[abKey] = task; } return(task); }
/// <summary> /// 异步获取资源 /// </summary> public void GetAssetAsync <T>(string abPath, string assetName, Action <T> callback, Action <float> progressCallback = null) where T : UnityEngine.Object { AssetBundleTask task = GetTask(abPath, true); task?.Load((ab) => { Task.CreateTask(GetAssetAsync(ab, assetName, (asset) => { callback?.Invoke(asset as T); }, (progress) => { progressCallback?.Invoke(progress * 0.5f + 0.5f); })); }, (progress) => { progressCallback?.Invoke(progress * 0.5f); }); }
private AssetBundleTask this[string key] { get { AssetBundleTask task = null; if (!string.IsNullOrEmpty(key)) { _taskDic?.TryGetValue(key, out task); } return(task); } set { if (!string.IsNullOrEmpty(key)) { _taskDic[key] = value; } } }
/// <summary> /// 异步加载AB包 /// </summary> public void LoadAssetBundleAsync(string abPath, Action <AssetBundle> callback, Action <float> progressCallback) { AssetBundleTask task = GetTask(abPath, true); task?.Load(callback, progressCallback); }