public AssetBundle GetAssetBundle()
 {
     if (assetBundle == null && downloadHandler != null)
     {
         assetBundle = downloadHandler.assetBundle;
         downloadHandler.Dispose();
         downloadHandler = null;
     }
     return(assetBundle);
 }
 /// <summary>
 /// Get the asset bundle object managed by this resource.  This call may force the bundle to load if not already loaded.
 /// </summary>
 /// <returns>The asset bundle.</returns>
 public AssetBundle GetAssetBundle()
 {
     if (m_AssetBundle == null && m_downloadHandler != null)
     {
         m_AssetBundle = m_downloadHandler.assetBundle;
         m_downloadHandler.Dispose();
         m_downloadHandler = null;
     }
     return(m_AssetBundle);
 }
        private void WebRequestOperationCompleted(AsyncOperation op)
        {
            UnityWebRequestAsyncOperation remoteReq = op as UnityWebRequestAsyncOperation;
            var webReq = remoteReq.webRequest;

            if (string.IsNullOrEmpty(webReq.error))
            {
                downloadHandler = webReq.downloadHandler as DownloadHandlerAssetBundle;
                provideHandle.Complete(this, true, null);
            }
            else
            {
                downloadHandler = webReq.downloadHandler as DownloadHandlerAssetBundle;
                downloadHandler.Dispose();
                downloadHandler = null;
                if (retries++ < options.RetryCount)
                {
                    UDebug.LogFormat("Web request {0} failed with error '{1}', retrying ({2}/{3})...", webReq.url, webReq.error, retries, options.RetryCount);
                    BeginOperation();
                }
                else
                {
                    var exception = new Exception(string.Format("RemoteAssetBundleProvider unable to load from url {0}, result='{1}'.", webReq.url, webReq.error));
                    CompletedEvent?.Invoke(null, false, exception);
                }
            }
            webReq.Dispose();
        }
Example #4
0
        protected IEnumerator LoadBundle(string name, Action <string, AssetBundle> callback, uint crc = 0)
        {
            var path = PathTool.DataPath + name;

            path = path.ToLower();
            using (var request = UnityWebRequestAssetBundle.GetAssetBundle(path))
            {
                var handler = new DownloadHandlerAssetBundle(request.url, crc);             //1
                request.downloadHandler = handler;                                          //1
                yield return(request.SendWebRequest());

                if (request.isNetworkError)
                {
                    MessageBox.Error("加载AssetBundle失败:" + path);
                    callback(path, null);
                }
                else
                {
                    callback(path, handler.assetBundle);                //1
                    //callback(path, (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle);                //2
                    //callback(path, AssetBundle.LoadFromMemory(request.downloadHandler.data));                             //3
                }
                handler.Dispose();
                request.Abort();
                request.Dispose();
            }
        }
Example #5
0
        private IEnumerator LoadCoroutine <T>(ResourceInfoBase info, HTFAction <float> loadingAction, HTFAction <T> loadDoneAction, bool isPrefab = false, Transform parent = null, bool isUI = false) where T : UnityEngine.Object
        {
            if (_isLoading)
            {
                yield return(_loadWait);
            }

            _isLoading = true;

            UnityEngine.Object asset = null;

            if (Mode == ResourceMode.Resource)
            {
                ResourceRequest request = Resources.LoadAsync <T>(info.ResourcePath);
                while (!request.isDone)
                {
                    loadingAction?.Invoke(request.progress);
                    yield return(null);
                }
                asset = request.asset;
                if (!asset)
                {
                    GlobalTools.LogError("加载资源失败:Resources文件夹中不存在 " + typeof(T) + " 资源 " + info.ResourcePath);
                }
                else
                {
                    if (isPrefab)
                    {
                        asset = ClonePrefab(asset as GameObject, parent, isUI);
                    }
                }
            }
            else
            {
#if UNITY_EDITOR
                loadingAction?.Invoke(1);
                yield return(null);

                asset = AssetDatabase.LoadAssetAtPath <T>(info.AssetPath);
                if (!asset)
                {
                    GlobalTools.LogError("加载资源失败:路径中不存在资源 " + info.AssetPath);
                }
                else
                {
                    if (isPrefab)
                    {
                        asset = ClonePrefab(asset as GameObject, parent, isUI);
                    }
                }
#else
                if (_assetBundles.ContainsKey(info.AssetBundleName))
                {
                    loadingAction?.Invoke(1);
                    yield return(null);

                    asset = _assetBundles[info.AssetBundleName].LoadAsset <T>(info.AssetPath);
                    if (!asset)
                    {
                        GlobalTools.LogError("加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath);
                    }
                    else
                    {
                        if (isPrefab)
                        {
                            asset = ClonePrefab(asset as GameObject, parent, isUI);
                        }
                    }
                }
                else
                {
                    UnityWebRequest            request = UnityWebRequest.Get(_assetBundlePath + info.AssetBundleName);
                    DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(request.url, 0);
                    request.downloadHandler = handler;
                    request.SendWebRequest();
                    while (!request.isDone)
                    {
                        loadingAction?.Invoke(request.downloadProgress);
                        yield return(null);
                    }
                    if (!request.isNetworkError && !request.isHttpError)
                    {
                        if (handler.assetBundle)
                        {
                            asset = handler.assetBundle.LoadAsset <T>(info.AssetPath);
                            if (!asset)
                            {
                                GlobalTools.LogError("加载资源失败:AB包 " + info.AssetBundleName + " 中不存在资源 " + info.AssetPath);
                            }
                            else
                            {
                                if (isPrefab)
                                {
                                    asset = ClonePrefab(asset as GameObject, parent, isUI);
                                }
                            }

                            if (IsCacheAssetBundle)
                            {
                                if (!_assetBundles.ContainsKey(info.AssetBundleName))
                                {
                                    _assetBundles.Add(info.AssetBundleName, handler.assetBundle);
                                }
                            }
                            else
                            {
                                handler.assetBundle.Unload(false);
                            }
                        }
                        else
                        {
                            GlobalTools.LogError("请求:" + request.url + " 未下载到AB包!");
                        }
                    }
                    else
                    {
                        GlobalTools.LogError("请求:" + request.url + " 遇到网络错误:" + request.error);
                    }
                    request.Dispose();
                    handler.Dispose();
                }
#endif
            }

            if (asset)
            {
                DataSetInfo dataSet = info as DataSetInfo;
                if (dataSet != null && dataSet.Data != null)
                {
                    (asset as DataSet).Fill(dataSet.Data);
                }

                loadDoneAction?.Invoke(asset as T);
            }
            asset = null;

            _isLoading = false;
        }