Exemplo n.º 1
0
 public void AddDependency(AssetBundleReference dependency)
 {
     if (dependency != null && m_Dependencies.Add(dependency))
     {
         dependency.Retain();
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 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();
     }
 }
Exemplo n.º 4
0
 void UnusedAllAssetBundles()
 {
     foreach (var iter in m_AssetBundles)
     {
         AssetBundleReference abr = iter.Value;
         abr.UnCache();
     }
 }
Exemplo n.º 5
0
 protected void AddDependency(AssetBundleReference abr)
 {
     if (abr != null)
     {
         abr.Retain();
         m_Dependencies.Add(abr);
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 7
0
        public override void Clean()
        {
            onComplete           = null;
            onBeforeComplete     = null;
            onAfterComplete      = null;
            info                 = null;
            type                 = null;
            assetBundleReference = null;

            result = null;
            base.Clean();
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 11
0
        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;
            }
        }
Exemplo n.º 12
0
        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();
                }
            }
        }
Exemplo n.º 13
0
 public void OnAssetBundleLoadComlete(AssetBundleReference assetBundleReference)
 {
     m_LoadComplete            = true;
     this.assetBundleReference = assetBundleReference;
 }
Exemplo n.º 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);
        }
Exemplo n.º 15
0
 void OnAssetBundleDispose(AssetBundleReference abr)
 {
     m_AssetBundles.Remove(abr.name);
 }
Exemplo n.º 16
0
 /// <summary>
 /// 释放到AssetBundle的引用。如果此时AssetBundle没有被其它资源引用,则会调用Unload(false)
 /// </summary>
 public void ReleaseBundleReference()
 {
     assetBundleReference = null;
 }