/// <summary> /// AssetBundle同步加载; /// </summary> /// <param name="path"></param> /// <returns></returns> public AssetBundle LoadFromFile(string path) { if (string.IsNullOrEmpty(path)) { return(null); } var assetBundlePath = AssetBundleHelper.GetAssetBundlePath(path); if (assetBundlePath == null) { return(null); } var assetBundleName = AssetBundleHelper.GetAssetBundleFileName(path); var assetBundle = LoadSync(assetBundlePath); if (assetBundle == null) { return(null); } //返回AssetBundleName; var dependentAssetBundle = Manifest.GetAllDependencies(assetBundleName); foreach (var tempAssetBundle in dependentAssetBundle) { if (tempAssetBundle == AssetBundleHelper.GetAssetBundleFileName(AssetBundleHelper.ShaderAssetBundleName) || tempAssetBundle == AssetBundleHelper.GetAssetBundleFileName(AssetBundleHelper.LuaAssetBundleName)) { continue; } var tempPtah = AssetBundleHelper.GetAssetBundlePathByName(tempAssetBundle); LoadSync(tempPtah); } return(assetBundle); }
/// <summary> /// AssetBundle 镜像卸载方法[Unload(false)]; /// 使用资源为一般初始化就全局保存不在销毁的资源,如:Shader; /// </summary> /// <param name="path"></param> public void UnloadMirroring(string path) { if (string.IsNullOrEmpty(path)) { return; } var assetBundlePath = AssetBundleHelper.GetAssetBundlePath(path); if (assetBundlePath != null) { UnloadAsset(assetBundlePath, false); } }
/// <summary> /// AssetBundle异步加载; /// </summary> /// <param name="path"></param> /// <param name="action"></param> /// <param name="progress"></param> /// <returns></returns> public IEnumerator LoadFromFileAsync(string path, Action <AssetBundle> action, Action <float> progress) { if (string.IsNullOrEmpty(path)) { yield break; } var assetBundlePath = AssetBundleHelper.GetAssetBundlePath(path); if (assetBundlePath == null) { yield break; } var assetBundleName = AssetBundleHelper.GetAssetBundleFileName(path); //先加载依赖的AssetBundle; var dependentAssetBundle = Manifest.GetAllDependencies(assetBundleName); var count = dependentAssetBundle.Length; var precent = ResourceMgr.singleton.LOAD_BUNDLE_PRECENT; var unit = precent / (count + 1); int index = 0; foreach (var tempAssetBundle in dependentAssetBundle) { var dp = 0f; if (tempAssetBundle == AssetBundleHelper.GetAssetBundleFileName(AssetBundleHelper.ShaderAssetBundleName) || tempAssetBundle == AssetBundleHelper.GetAssetBundleFileName(AssetBundleHelper.LuaAssetBundleName)) { continue; } var tempPtah = AssetBundleHelper.GetAssetBundlePathByName(tempAssetBundle); var itor = LoadAsync(tempPtah, null, (value) => { dp = value; }); while (itor.MoveNext()) { progress?.Invoke(unit * (index + dp)); yield return(CoroutineMgr.WaitForEndOfFrame); } index++; } //加载目标AssetBundle; var p = 0f; var itorTarget = LoadAsync(assetBundlePath, action, (value) => { p = value; }); while (itorTarget.MoveNext()) { progress?.Invoke(unit * (count + p)); yield return(CoroutineMgr.WaitForEndOfFrame); } }
/// <summary> /// 通用资源AssetBundle卸载方法[Unload(true)]; /// </summary> /// <param name="path"></param> /// <param name="asset"></param> public void UnloadAsset(string path, UnityObject asset) { if (string.IsNullOrEmpty(path)) { return; } if (asset != null && _assetBundleRefDict.TryGetValue(path, out var list)) { for (var i = 0; i < list.Count; i++) { if ((UnityObject)(list[i].Target) == asset) { list.RemoveAt(i); //需要UnloadAsset? //Resources.UnloadAsset(asset); break; } } } var assetBundleName = AssetBundleHelper.GetAssetBundleFileName(path); var dependentAssetBundle = Manifest.GetAllDependencies(assetBundleName); foreach (var tempAssetBundle in dependentAssetBundle) { if (tempAssetBundle == AssetBundleHelper.GetAssetBundleFileName(AssetBundleHelper.ShaderAssetBundleName)) { continue; } var tempPtah = AssetBundleHelper.GetAssetBundlePathByName(tempAssetBundle); UnloadAsset(tempPtah, true); } var assetBundlePath = AssetBundleHelper.GetAssetBundlePath(path); if (assetBundlePath != null) { UnloadAsset(assetBundlePath, true); } }