Ejemplo n.º 1
0
        public BundleCache LoadAssetBundleSync(string name)
        {
            BundleInfo info = m_assetCfg.GetBundleInfo(name);

            if (info == null)
            {
                return(null);
            }

            //find cache
            BundleCache bundle = null;

            if (m_bundleCache.TryGetValue(name, out bundle))
            {
                bundle.Access();
                return(bundle);
            }

            //load depend
            _AddDependAbSync(info);

            if (m_bundleCache.TryGetValue(name, out bundle))
            {
                bundle.Access();
                return(bundle);
            }
            return(null);
        }
Ejemplo n.º 2
0
 public void RemoveDependBundle(BundleCache bundle)
 {
     if (m_refBundle.Contains(bundle))
     {
         m_refBundle.Remove(bundle);
         bundle.ReduceRef();
         return;
     }
 }
Ejemplo n.º 3
0
 public void OnRelease()
 {
     asset            = null;
     info             = null;
     finishedCallback = null;
     if (m_refBundle != null)
     {
         m_refBundle.ReduceRef();
         m_refBundle = null;
     }
 }
Ejemplo n.º 4
0
        public BundleCache GetBundleCached(string name)
        {
            BundleCache bundle = null;

            if (m_bundleCache.TryGetValue(name, out bundle))
            {
                bundle.Access();
                return(bundle);
            }
            return(null);
        }
Ejemplo n.º 5
0
        public void OnRelease()
        {
            finishedCallback = null;
            if (self != null)
            {
                self.ReduceRef();
            }

            _CleanRef();
            self = null;
            info = null;
        }
Ejemplo n.º 6
0
 public void AddDependBundle(BundleCache bundle)
 {
     if (m_refBundle == null)
     {
         m_refBundle = new List <BundleCache>();
     }
     if (m_refBundle.Contains(bundle))
     {
         return;
     }
     m_refBundle.Add(bundle);
     bundle.AddRef();
 }
Ejemplo n.º 7
0
        private void _AddDependAbSync(BundleInfo info)
        {
            if (info == null)
            {
                return;
            }

            //find cache
            if (info.HasDependBundle())
            {
                for (int i = 0; i < info.dependBundle.Count; ++i)
                {
                    BundleInfo dependInfo = m_assetCfg.GetBundleInfo(info.dependBundle[i]);
                    //find cache
                    BundleCache bundle = null;
                    if (!m_bundleCache.TryGetValue(dependInfo.name, out bundle))
                    {
                        //load depend
                        _AddDependAbSync(dependInfo);
                    }
                    else
                    {
                        bundle.Access();
                    }
                }
            }

            //load
            AssetBundle ab = _LoadAssetBundleSync(info.name);

            if (ab != null)
            {
                BundleCache bundle = new BundleCache();
                bundle.name   = info.name;
                bundle.info   = info;
                bundle.bundle = ab;
                bundle.Access();
                m_bundleCache.Add(info.name, bundle);
            }
            else
            {
                if (m_useDownload)
                {
                    VersionUpdate.GetIns().UpdateOneRes(info.name, null);
                }
            }
        }
Ejemplo n.º 8
0
        public void OnAssetBundleLoaded(string bundleName, BundleCache bundle)
        {
            LogUtils.Log("bundle loaded!:", bundleName);
            m_status = AssetLoadStatus.Loaded;
            if (bundle == null)
            {
                OnAssetLoaded(null);
                return;
            }

            if (m_refBundle != null)
            {
                m_refBundle.ReduceRef();
            }
            m_refBundle = bundle;
            m_refBundle.AddRef();
        }
Ejemplo n.º 9
0
        private void _AddDependAb(BundleLoadRequest request)
        {
            if (!request.info.HasDependBundle())
            {
                return;
            }

            for (int i = 0; i < request.info.dependBundle.Count; ++i)
            {
                bool       needLoad   = true;
                BundleInfo dependInfo = m_assetCfg.GetBundleInfo(request.info.dependBundle[i]);
                //find cache
                BundleCache bundle = null;
                if (m_bundleCache.TryGetValue(dependInfo.name, out bundle))
                {
                    needLoad = false;
                    request.OnAssetBundleLoadFinished(dependInfo.name, bundle);
                }
                else
                {
                    //find request
                    for (int k = 0; k < m_loadRequest.Count; ++k)
                    {
                        if (m_loadRequest[k].name.Equals(dependInfo.name))
                        {
                            needLoad = false;
                            break;
                        }
                    }
                }

                if (needLoad)
                {
                    //add depend ab
                    BundleLoadRequest depRequest = new BundleLoadRequest(dependInfo.name, dependInfo, null);

                    _AddDependAb(depRequest);

                    //add self
                    m_loadRequest.Add(depRequest);
                }
            }
        }
Ejemplo n.º 10
0
        public AssetCache LoadAssetSync(string name)
        {
            name = name.ToLower();  //unity res lower
            AssetCache asset = null;

            if (m_assetsCache.TryGetValue(name, out asset))
            {
                return(asset);
            }
            UnityEngine.Object obj   = null;
            AssetCache         cache = null;
            AssetInfo          info  = m_assetsConfig.GetAssetInfo(name);

            if (info == null)
            {
                obj = LoadAsset(name);
                if (obj != null)
                {
                    cache = new AssetCache(name, info, obj);
                    AddAssetCache(name, cache);
                    return(cache);
                }
                return(null);
            }

            BundleCache bundle = m_bundleMgr.LoadAssetBundleSync(info.dependBundle);

            if (bundle == null)
            {
                return(null);
            }

            obj = bundle.LoadAsset(AssetCache.AssetNameToBundleAssetName(info.name, info.extName));
            if (obj == null)
            {
                return(null);
            }

            cache = new AssetCache(name, info, obj);
            AddAssetCache(name, cache);
            return(cache);
        }
Ejemplo n.º 11
0
        public void LoadAssetBundleAsync(string name, Action <string, BundleCache> callback)
        {
            BundleInfo info = m_assetCfg.GetBundleInfo(name);

            if (info == null)
            {
                if (callback != null)
                {
                    callback(name, null);
                }
                return;
            }

            //find cache
            BundleCache bundle = null;

            if (m_bundleCache.TryGetValue(name, out bundle))
            {
                bundle.Access();
                callback(name, bundle);
                return;
            }

            //find request
            for (int i = 0; i < m_loadRequest.Count; ++i)
            {
                if (m_loadRequest[i].name.Equals(name))
                {
                    m_loadRequest[i].finishedCallback += callback;
                    return;
                }
            }

            //add depend ab
            BundleLoadRequest request = new BundleLoadRequest(name, info, callback);

            _AddDependAb(request);

            //add self
            m_loadRequest.Add(request);
        }
Ejemplo n.º 12
0
        public void OnUpdate()
        {
            {
                //time ctrl
                float nowTime = Time.time;
                if (nowTime - lastTime > 0.080f)
                {
                    lastTime = nowTime;
                    return;
                }

                bool idle = true;
                //load asset
                if (m_loadRequest.Count > 0)
                {
                    for (int i = 0; i < m_loadRequest.Count; ++i)
                    {
                        AssetLoadRequest request = m_loadRequest[i];
                        if (request.GetStatus() == AssetLoadStatus.Wait)
                        {
                            AssetCache asset = null;
                            if (m_assetsCache.TryGetValue(request.name, out asset))
                            {
                                idle = false;
                                request.OnAssetLoaded(asset);
                                break;//continue;
                            }

                            if (request.info == null || request.info.dependBundle == null)
                            {
                                UnityEngine.Object obj = LoadAsset(request.name);
                                if (obj != null)
                                {
                                    idle = false;
                                    AssetCache cache = new AssetCache(request.name, request.info, obj);
                                    AddAssetCache(request.name, cache);
                                    request.OnAssetLoaded(cache);
                                    break; //continue;
                                }
                                else
                                {
                                    request.OnAssetLoaded(null);
                                    continue;
                                }
                            }

                            BundleCache bundle = m_bundleMgr.GetBundleCached(request.info.dependBundle);
                            request.SetLoading();
                            if (bundle != null)
                            {
                                idle = false;
                                request.OnAssetBundleLoaded(request.info.dependBundle, bundle);
                                break; //continue;
                            }
                            m_bundleMgr.LoadAssetBundleAsync(request.info.dependBundle, request.OnAssetBundleLoaded);
                        }
                    }
                }

                //update
                for (int i = 0; i < m_loadRequest.Count; ++i)
                {
                    AssetLoadRequest request = m_loadRequest[i];
                    bool             handled = request.UpdateState();
                    if (handled)
                    {
                        idle = false;
                        break;
                    }
                }
//__end:
                //clean request
                for (int i = 0; i < m_loadRequest.Count;)
                {
                    AssetLoadRequest request = m_loadRequest[i];
                    if (request.GetStatus() == AssetLoadStatus.Finished)
                    {
                        request.OnRelease();
                        m_loadRequest.RemoveAt(i);
                    }
                    else
                    {
                        ++i;
                    }
                }

                //load bundle
                if (idle)
                {
                    //clean asset
                    _CleanAsset(false);
                    //load bundle
                    m_bundleMgr.OnTick();
                }

                lastTime = nowTime;
            }
        }
Ejemplo n.º 13
0
        public void OnTick()
        {
            bool isIdle = true;

            //load ab
            for (int i = 0; i < m_loadRequest.Count; ++i)
            {
                if (m_loadRequest[i].GetStatus() == AssetLoadStatus.Wait ||
                    m_loadRequest[i].GetStatus() == AssetLoadStatus.Downloaded)
                {
                    string      name  = m_loadRequest[i].name;
                    BundleCache cache = null;
                    if (m_bundleCache.TryGetValue(name, out cache))
                    {
                        m_loadRequest[i].OnAssetBundleLoadFinished(name, cache);
                        continue;
                    }
                    AssetBundle ab = _LoadAssetBundleSync(name);
                    if (ab == null)
                    {
                        bool isFaild = true;
                        if (m_useDownload && m_loadRequest[i].GetStatus() != AssetLoadStatus.Downloaded)
                        {
                            isFaild = !m_loadRequest[i].DownLoad();
                        }

                        if (isFaild)
                        {
                            m_loadRequest[i].OnAssetBundleLoadFinished(name, null);
                        }
                    }
                    else
                    {
                        isIdle = false;

                        cache        = new BundleCache();
                        cache.name   = name;
                        cache.info   = m_loadRequest[i].info;
                        cache.bundle = ab;
                        cache.Access();
                        m_bundleCache.Add(name, cache);
                        m_loadRequest[i].OnAssetBundleLoadFinished(name, cache);
                        break;  //do one!
                    }
                }
            }

            //check
            for (int i = 0; i < m_loadRequest.Count; ++i)
            {
                if (m_loadRequest[i].GetStatus() == AssetLoadStatus.Loading)
                {
                    isIdle = false;
                    List <string> unRefDepend = m_loadRequest[i].GetUnRefDependName();
                    if (unRefDepend != null)
                    {
                        for (int k = 0; k < unRefDepend.Count; ++k)
                        {
                            BundleCache cache = null;
                            if (m_bundleCache.TryGetValue(unRefDepend[k], out cache))
                            {
                                m_loadRequest[i].OnAssetBundleLoadFinished(unRefDepend[k], cache);
                            }
                        }
                    }
                }
            }

//__end:
            //clear finished request
            for (int i = 0; i < m_loadRequest.Count;)
            {
                BundleLoadRequest request = m_loadRequest[i];
                if (request.GetStatus() == AssetLoadStatus.Finished)
                {
                    request.OnRelease();
                    m_loadRequest.RemoveAt(i);
                }
                else
                {
                    ++i;
                }
            }

            if (isIdle)
            {
                _ReleaseAb(false);
            }
        }
Ejemplo n.º 14
0
        public void OnAssetBundleLoadFinished(string name, BundleCache bundle)
        {
            //failed
            if (bundle == null)
            {
                m_status = AssetLoadStatus.Finished;
                if (finishedCallback != null)
                {
                    finishedCallback(name, null);
                }
                return;
            }
            //self
            if (name.Equals(this.name))
            {
                if (self != null)
                {
                    self.ReduceRef();
                }

                self = bundle;
                self.AddRef();
            }
            //no depend ab
            if (!info.HasDependBundle())
            {
                m_status = AssetLoadStatus.Finished;
                if (finishedCallback != null)
                {
                    finishedCallback(this.name, self);
                }
                return;
            }
            m_status = AssetLoadStatus.Loading;
            if (name.Equals(this.name))
            {
                return;
            }
            //depend
            if (!info.IsDependBundle(name))
            {
                LogUtils.LogWarning("IsDependBundle() == false");
                return;
            }
            if (!m_refBundle.Contains(bundle))
            {
                m_refBundle.Add(bundle);
                bundle.AddRef();
            }
            m_unRefDependName.Remove(name);
            //load sucess
            if (info.GetDependBundleCount() == m_refBundle.Count && self != null)
            {
                for (int i = 0; i < m_refBundle.Count; ++i)
                {
                    self.AddDependBundle(m_refBundle[i]);
                }

                m_status = AssetLoadStatus.Finished;
                if (finishedCallback != null)
                {
                    finishedCallback(this.name, self);
                }

                _CleanRef();
            }
        }