Exemplo n.º 1
0
        /// <summary>
        /// load asset bundle from file
        /// </summary>
        /// <param name="path"></param>
        /// <param name="tag"></param>
        /// <param name="cacheLoadedAsset"></param>
        /// <returns>AssetBundleReference retainted.ref count add one after load.</returns>
        public AssetBundleReference LoadAssetBundleSync(string path, int tag, bool cacheLoadedAsset = true)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            AssetBundleReference abr = null;

            if (m_AssetBundles.ContainsKey(path))
            {
#if ASSETMANAGER_LOG_ON
                Debug.LogFormat("[AssetManage]LoadAssetBundleSync bundle is loaded {0},{1}", path, Time.frameCount);
#endif
                abr = m_AssetBundles[path];
                //refresh
                abr.AddTag(tag);

                if (cacheLoadedAsset)
                {
                    abr.Cache();
                }
            }
            else
            {
                if (m_LoadingAssetBundleLoaders.ContainsKey(path))
                {
                    Debug.LogErrorFormat("[AssetManage]LoadAssetBundleSync async loader is active {0},{1}", path, Time.frameCount);
                    //TODO Stop async
                    return(null);
                }
                else
                {
#if ASSETMANAGER_LOG_ON
                    Debug.LogFormat("[AssetManage]LoadAssetBundleSync create new loader {0},{1}", path, Time.frameCount);
#endif
                    AssetBundleSyncLoader loader = m_LoaderManager.CreateAssetBundleSyncLoader(path);
                    if (loader != null)
                    {
                        loader.state = Loader.State.Inited;
                        if (loader.cacheLoadedAsset == false && cacheLoadedAsset)
                        {
                            loader.cacheLoadedAsset = true;
                        }

                        loader.Start();
                        abr = loader.result;
                        //must retain . will be destory by loader clean
                        abr.Retain();
                        OnAssetBundleBeforeLoaded(loader);
                        OnAssetBundleAfterLoaded(loader);
                    }
                }
            }

            return(abr);
        }
Exemplo n.º 2
0
        /// <summary>
        /// async load asset bundle
        /// 同一个资源只有一个正在加载的loader
        /// </summary>
        /// <param name="path"></param>
        /// <param name="tag"></param>
        /// <param name="cacheLoadedAsset"></param>
        /// <param name="completeHandle"></param>
        /// <returns></returns>
        public AssetBundleLoader LoadAssetBundle(string path, int tag, bool cacheLoadedAsset, Action <AssetBundleReference> completeHandle = null, bool autoStart = true)
        {
            AssetBundleLoader loader = null;

            if (string.IsNullOrEmpty(path))
            {
                if (completeHandle != null)
                {
                    completeHandle(null);
                }
                return(loader);
            }

            if (m_AssetBundles.ContainsKey(path))
            {
#if ASSETMANAGER_LOG_ON
                Debug.Log("[AssetManage]LoadAssetBundle asset bundle is loaded " + path + "," + Time.frameCount);
#endif
                //asset bundle is loaded
                AssetBundleReference abr = m_AssetBundles[path];

                //refresh
                abr.AddTag(tag);

                if (cacheLoadedAsset)
                {
                    abr.Cache();
                }

                loader           = m_LoaderManager.CreateAssetBundleAsyncLoader(path);
                loader.forceDone = true;
                loader.result    = abr;

                //call complete callback
                if (completeHandle != null)
                {
                    loader.onComplete += completeHandle;
                }

                loader.IncreaseLoadingRequest();

                loader.onAfterComplete += OnAssetBundleAfterLoaded;
                loader.state            = Loader.State.Completed;
                if (autoStart)
                {
                    m_LoaderManager.ActiveLoader(loader);
                }
            }
            else
            {
                if (m_LoadingAssetBundleLoaders.ContainsKey(path))
                {
#if ASSETMANAGER_LOG_ON
                    Debug.Log("[AssetManage]LoadAssetBundle using loading loader " + path + "," + Time.frameCount);
#endif
                    loader = m_LoadingAssetBundleLoaders[path];
                }
                else
                {
#if ASSETMANAGER_LOG_ON
                    Debug.Log("[AssetManage]LoadAssetBundle create new loader " + path + "," + Time.frameCount);
#endif
                    loader = m_LoaderManager.CreateAssetBundleAsyncLoader(path);
                    if (loader != null)
                    {
                        m_LoadingAssetBundleLoaders[path] = loader;
                    }
                    else
                    {
                        if (completeHandle != null)
                        {
                            completeHandle(null);
                        }
                        return(null);
                    }
                }

                loader.AddParamTag(tag);

                if (loader.cacheLoadedAsset == false && cacheLoadedAsset)
                {
                    loader.cacheLoadedAsset = true;
                }

                if (completeHandle != null)
                {
                    loader.onComplete += completeHandle;
                }

                if (loader.state == Loader.State.Idle)
                {
                    loader.onBeforeComplete += OnAssetBundleBeforeLoaded;
                    loader.onAfterComplete  += OnAssetBundleAfterLoaded;
                    loader.state             = Loader.State.Inited;
                    if (autoStart)
                    {
                        m_LoaderManager.ActiveLoader(loader);
                    }
                }
            }
            return(loader);
        }