public override T LoadAsset <T>(string path) { //已加载过 if (AssetDic.ContainsKey(path)) { AssetDic[path].Retain(); return(AssetDic[path].obj as T); } string abName = GetAbName(path); if (!IsLoadBundle(abName)) { //依赖 LoadAssetBundleDepends(abName); BundleInfo.CreateInfo(abName, LoadAssetBundle(abName)); } else { bundleDic[abName].Retain(); } T t = null; if (bundleDic[abName].bundle != null) { t = bundleDic[abName].bundle.LoadAsset <T>(path); } AssetInfo.CreateInfo(path, t); return(t); }
IEnumerator LoadSceneCoroutine(string path, Action <AssetLoadInfo> action) { string abName = GetAbName(path); if (!IsLoadBundle(abName)) { //这里用同步加载了,有的场景引用的东西太多了,用异步加载太慢了 LoadAssetBundleDepends(abName); BundleInfo.CreateInfo(abName, LoadAssetBundle(abName)); } else { bundleDic[abName].Retain(); } //加载场景 yield return(SceneManager.LoadSceneAsync(path, LoadSceneMode.Single)); if (lastSceneName != "") { UnLoadScene(lastSceneName); } lastSceneName = abName; if (action != null) { AssetLoadInfo info = new AssetLoadInfo(); info.path = path; action.Invoke(info); } }
private IEnumerator LoadAssetBundleDependsAsync(string abName) { string[] depends = Manifest.GetAllDependencies(abName); for (int i = 0; i < depends.Length; i++) { string dependName = depends[i]; if (IsLoadBundle(dependName)) { //Debug.Log("LoadAssetCoroutineHelper IsLoadBundle=" + dependName); bundleDic[dependName].Retain(); continue; } //先添加进容器,防止重复调用 BundleInfo.CreateInfo(dependName, null); //Debug.Log("LoadAssetCoroutineHelper dependName=" + dependName); var dependRequest = AssetBundle.LoadFromFileAsync(GetLoadPath(dependName)); yield return(dependRequest); bundleDic[dependName].bundle = dependRequest.assetBundle; } }
private void LoadAssetBundleDepends(string abName) { //比如加载A,包含引用B1和B2,B1和B2都引用了C,但是GetAllDependencies(A)方法只会算一个C的计数 //而如果之后再单独加载B1和B2时,如果不加载依赖会导致依赖计数不对 //所以这里使用了GetDirectDependencies string[] depends = Manifest.GetDirectDependencies(abName); for (int i = 0; i < depends.Length; i++) { string dependName = depends[i]; if (!IsLoadBundle(dependName)) { BundleInfo.CreateInfo(dependName, LoadAssetBundle(dependName)); } else { bundleDic[dependName].Retain(); } //递归添加所有依赖,如果用GetAllDependencies会导致计数不对 LoadAssetBundleDepends(dependName); } }
IEnumerator LoadAssetCoroutineHelper <T>(string path) where T : Object { //Debug.Log("LoadAssetCoroutineHelper begin"); if (IsLoadAsset(path)) { //如果重复对同样的资源进行异步加载,需要等待 if (prepareLoadDic.ContainsKey(path)) { while (prepareLoadDic.ContainsKey(path)) { yield return(null); } if (AssetDic.ContainsKey(path)) { AssetDic[path].Retain(); yield break; } //这里有个极特殊情况,最开始没考虑到,同时异步加载两个资源时,目前的写法是加载第二个资源时进行等待 //但是有可能第一个资源加载完之后立刻卸载了,导致第二个资源等待完之后全都没了,所以需要触发重新加载 else { yield return(LoadAssetCoroutineHelper <T>(path)); } } else { AssetDic[path].Retain(); yield break; } } AddPrepareLoadDic(path); //先添加进容器 AssetInfo.CreateInfo(path, null); string abName = GetAbName(path); if (!IsLoadBundle(abName)) { //先添加进容器,防止重复调用 BundleInfo.CreateInfo(abName, null); yield return(LoadAssetBundleDependsAsync(abName)); //Debug.Log("LoadAssetCoroutineHelper abName=" + abName); var bundleRequest = AssetBundle.LoadFromFileAsync(GetLoadPath(abName)); yield return(bundleRequest); bundleDic[abName].bundle = bundleRequest.assetBundle; } else { //Debug.Log("LoadAssetCoroutineHelper IsLoadAbName=" + abName); bundleDic[abName].Retain(); } AssetBundle bundle = bundleDic[abName].bundle; //Debug.Log("LoadAssetCoroutineHelper path=" + path); AssetBundleRequest request = bundle.LoadAssetAsync <T>(path); yield return(request); AssetInfo.SetObject(path, request.asset); //Debug.Log("LoadAssetCoroutineHelper over"); //移除加载中列表 RemovePrepareLoadDic(path); //在异步加载时进来一个卸载,都加载完后在这里统一删除 if (unLoadCacheDic.ContainsKey(path)) { Debug.Log("unLoadCacheDic=" + path); RemoveUnLoadCacheDic(path); UnLoadAsset(path); } }