Esempio n. 1
0
        //开始异步加载AssetBundle类型的资源
        //远程下载文件到内存中成为AssetBundle镜像,再开辟内存从AssetBundle镜像中创建出指定Asset,最后卸载掉AB镜像内存,只保留Asset对象
        private static IEnumerator BeginAssetBundleLoadAsync(ResourceItem item)
        {
            var isMainifest = IsManifest(item.Path);
            var path = AssetPathConfig.GetAssetPathWWW(!isMainifest ? item.Path.ToLower() : item.Path);
            item.SetLoadState(EResItemLoadState.Loading);
            //if (item.Path.Contains("atlas")) LoggerHelper.Error("begin atlas path: " + item.Path);
            var www = new WWW(path);
            yield return www;

            if (string.IsNullOrEmpty(www.error))
            {
                item.SetLoadState(EResItemLoadState.Completed);
                //Debug.LogError("load success, path: " + www.url);

                if (!item.IsSaveAssetBundle || !IsSaveBundle(item.Path))
                {
                    Object mainAsset = null;
                    var bundle = www.assetBundle;
                    if (isMainifest)
                    {
                        //LoggerHelper.Error("is mainifest: " + item.Path);
                        mainAsset = bundle.LoadAsset("AssetBundleManifest");
                        item.SetMainAsset(mainAsset);
                    }
                    else
                    {
                        mainAsset = bundle.mainAsset;
                        if (mainAsset == null)
                        {
                            var assets = bundle.LoadAllAssets();
                            if (assets == null || assets.Length == 0)
                            {
                                Debug.LogError("load no assets, path: " + item.Path);
                                yield break;
                            }
                            mainAsset = assets[0];

                            item.SetMainAsset(mainAsset);
                            item.SetAssets(assets);
                        }
                    }

                    item.LaunchCallBack();

                    if (bundle != null)
                        bundle.Unload(false);

                    www.Dispose();
                }
                else
                {
                    item.SetAssetBundle(www.assetBundle);
                    //if (item.Path.StartsWith("atlas")) LoggerHelper.Error("after atlas path: " + item.Path);
                    item.LaunchCallBack();
                    www.Dispose();
                }
            }
            else
            {
                item.SetLoadState(EResItemLoadState.Error);
                Debug.LogError(string.Format("加载资源失败:{0}\n{1}", www.url, www.error));
                item.LaunchCallBack();
            }

            //按需清除ResourceItem缓存
            if (item.IsClearAfterLoaded)
            {
                Clear(item.Path);
                GarbageCollect();
            }
        }