public void AddDependency(AssetBundleReference dependency)
 {
     if (dependency != null && m_Dependencies.Add(dependency))
     {
         dependency.Retain();
     }
 }
Exemple #2
0
        public void UnloadUnusedBundles(int tag)
        {
            if (m_AssetBundles.Count == 0)
            {
                return;
            }

            AssetBundleReference abr = null;

            Stack <string> checkQueue = StackPool <string> .Get();

            HashSet <string> checkings = HashSetPool <string> .Get();


            Action <string> checkFun = (key) =>
            {
                abr = m_AssetBundles[key];
                checkings.Remove(key);

                if (abr.isUnused())
                {
                    //check dependencies
                    if (abr.dependencies != null && abr.dependencies.Count > 0)
                    {
                        foreach (AssetBundleReference sub in abr.dependencies)
                        {
                            //只有同样tag和空tag的ref才需要重新检查。
                            if (sub.isCache && (sub.tagCount == 0 || sub.HaveTag(tag)) && !checkings.Contains(sub.name))
                            {
                                checkQueue.Push(sub.name);
                            }
                        }
                    }

                    abr.Dispose();
                    m_AssetBundles.Remove(key);
                }
            };

            foreach (string key in m_AssetBundles.Keys)
            {
                abr = m_AssetBundles[key];
                if (abr.HaveTag(tag) && abr.isCache)
                {
                    checkQueue.Push(key);
                    checkings.Add(key);
                }
            }

            //recheck unused asset bundle
            while (checkQueue.Count > 0)
            {
                checkFun(checkQueue.Pop());
            }

            StackPool <string> .Release(checkQueue);

            HashSetPool <string> .Release(checkings);
        }
Exemple #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="assetBundleName"></param>
 public void UnusedAssetBundle(string assetBundleName)
 {
     if (m_AssetBundles.ContainsKey(assetBundleName))
     {
         AssetBundleReference abr = m_AssetBundles[assetBundleName];
         abr.UnCache();
     }
 }
Exemple #4
0
 void UnusedAllAssetBundles()
 {
     foreach (var iter in m_AssetBundles)
     {
         AssetBundleReference abr = iter.Value;
         abr.UnCache();
     }
 }
 protected void AddDependency(AssetBundleReference abr)
 {
     if (abr != null)
     {
         abr.Retain();
         m_Dependencies.Add(abr);
     }
 }
Exemple #6
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);
        }
Exemple #7
0
        public override void Clean()
        {
            onComplete           = null;
            onBeforeComplete     = null;
            onAfterComplete      = null;
            info                 = null;
            type                 = null;
            assetBundleReference = null;

            result = null;
            base.Clean();
        }
Exemple #8
0
        void OnAssetBundleLoadComplete(AssetBundleReference abr)
        {
            //置空loader
            if (m_AssetBundleLoader != null)
            {
                m_AssetBundleLoader.onComplete -= OnAssetBundleLoadComplete;
                m_AssetBundleLoader             = null;
            }

            //为了保险做一下检查
            if (!m_Aborted)
            {
                assetBundleReference = abr;
                m_State = State.Loaded;
                LoadAsset();
            }
        }
Exemple #9
0
        void OnAssetBundleBeforeLoaded(AssetBundleLoader loader)
        {
            AssetBundleReference abr = loader.result;

            if (abr != null)
            {
                m_AssetBundles[abr.name] = abr;

                if (loader.cacheLoadedAsset)
                {
                    abr.Cache();
                }
                else
                {
                    abr.isCache = false;
                }
                abr.onDispose += OnAssetBundleDispose;
            }

            AssetBundleInfo info = loader.info;

            if (info != null)
            {
                if (m_LoadingAssetBundleLoaders.ContainsKey(info.fullName))
                {
                    m_LoadingAssetBundleLoaders.Remove(info.fullName);
                }
            }
            else
            {
                string key = null;
                foreach (var iter in m_LoadingAssetBundleLoaders)
                {
                    if (iter.Value == loader)
                    {
                        key = iter.Key;
                    }
                }

                if (!string.IsNullOrEmpty(key))
                {
                    m_LoadingAssetBundleLoaders.Remove(key);
                }
            }
        }
Exemple #10
0
        protected void RemoveAssetBundlesTag(int tag)
        {
            if (m_AssetBundles.Count == 0)
            {
                return;
            }

            AssetBundleReference abr = null;
            var iter = m_AssetBundles.GetEnumerator();

            while (iter.MoveNext())
            {
                abr = iter.Current.Value;
                if (abr.isCache && abr.HaveTag(tag))
                {
                    abr.RemoveTag(tag);
                }
            }
        }
        public override void Dispose(bool disposing, bool forceRemoveAll)
        {
            if (m_Disposed)
            {
                return;
            }

#if ASSETMANAGER_LOG_ON
            Debug.Log("[AssetManage]Asset dispose " + name + "," + Time.frameCount);
#endif

            base.Dispose(disposing, forceRemoveAll);

            if (disposing)
            {
                if (onDispose != null)
                {
                    onDispose(this);
                    onDispose = null;
                }

                if (asset != null)
                {
                    if (!(asset is GameObject))
                    {
                        Resources.UnloadAsset(asset);
                    }

                    asset = null;
                }

                asset = null;
                //这里通过setter调用release
                assetBundleReference = null;
                name = null;
            }
        }
        protected void OnDependencyComplete(AssetBundleReference abr)
        {
            if (abr != null && !abr.IsEmpty())
            {
                AddDependency(abr);
#if ASSETMANAGER_LOG_ON
                Debug.LogFormat("[AssetManage]DependencyComplete {0}=>{1},{2}---{3} ", info.fullName, abr.name, m_WaitDependencyCompleteCount, Time.frameCount);
#endif
            }
            else
            {
                Debug.LogError("[AssetManage]Download dependency error");
                m_DependenciesHaveError = true;
            }

            if (--m_WaitDependencyCompleteCount == 0)
            {
                m_DependenciesIsDone = true;
                if (m_State == State.Loaded && m_Result != null)
                {
                    DoAllComplete();
                }
            }
        }
Exemple #13
0
 public void OnAssetBundleLoadComlete(AssetBundleReference assetBundleReference)
 {
     m_LoadComplete            = true;
     this.assetBundleReference = assetBundleReference;
 }
Exemple #14
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);
        }
Exemple #15
0
 void OnAssetBundleDispose(AssetBundleReference abr)
 {
     m_AssetBundles.Remove(abr.name);
 }
 /// <summary>
 /// 释放到AssetBundle的引用。如果此时AssetBundle没有被其它资源引用,则会调用Unload(false)
 /// </summary>
 public void ReleaseBundleReference()
 {
     assetBundleReference = null;
 }