예제 #1
0
        /// <summary>
        /// WWW加载图片
        /// </summary>
        /// <param name="callback"></param>
        /// <returns></returns>
        private static IEnumerator BeginImageLoadAsync(ResourceItem item, string savePath)
        {
            item.SetLoadState(EResItemLoadState.Loading);
            string path = item.Path;
            var www = new WWW(path);
            yield return www;

            //Debug.LogError("LoadImage save good: " + savePath);

            if (string.IsNullOrEmpty(www.error))
            {
                var texture2d = www.texture;
                texture2d.Compress(true);
                item.SetLoadState(EResItemLoadState.Completed);
                item.SetMainAsset(texture2d);
                item.LaunchCallBack();

                try
                {
                    File.WriteAllBytes(savePath, www.bytes);
                }
                catch (Exception e)
                {
                    Debug.LogError("LoadImage save error: " + e.ToString());
                }

                www.Dispose();
            }
            else
            {
                //Debug.LogError("pp: " + path);
                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();
            }
        }
예제 #2
0
        //开始异步加载Resources类型的资源,用来模拟AssetBundle异步加载流程
        private static IEnumerator BeginResourceLoadAsync(ResourceItem item)
        {
            var request = Resources.LoadAsync(item.Path);
            yield return request;

            var asset = request.asset;
            if (asset == null)
            {
                var assets = Resources.LoadAll(item.Path);
                if (assets == null || assets.Length == 0)
                {
                    Debug.LogError("资源不存在:\n路径:" + item.Path);
                    item.LaunchCallBack();
                    yield break;
                }
                else
                    item.SetAssets(assets);
            }
            else
                item.SetMainAsset(asset);
            //Debug.LogError("load: "+asset);
            //if (item.NeedCRC)
            //{
            //    item.CRC = FirstUtil.CRC32String(File.ReadAllBytes(string.Concat( Application.dataPath, "/Resources/", item.Path, ".prefab")));
            //}

            item.SetLoadState(EResItemLoadState.Completed);

            //按需清除ResourceItem缓存
            if (item.IsClearAfterLoaded && m_resourceItemDic.ContainsKey(item.Path))
                m_resourceItemDic.Remove(item.Path);

            item.LaunchCallBack();
        }
예제 #3
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();
            }
        }