Example #1
0
        public void Dump()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("Dump Bundle Cache. Count = {0}\n", m_caches.Count);
            var iter = m_caches.GetEnumerator();

            while (iter.MoveNext())
            {
                AssetBundleInfo item = iter.Current.Value;
                sb.AppendFormat("{0} count:{1} persistent:{2}\n", iter.Current.Key, item.ReferencedCount,
                                item.Persistent);
            }

            iter.Dispose();

            Logger.Log(sb.ToString());
        }
Example #2
0
        // ------------------------------------------------------------------------------------------
        // 从AssetBundle里加载资源
        private void LoadBundleAsset(LoaderGroup group, AssetBundleInfo info, string name, Type type,
                                     GroupLoadedCallback onLoaded,
                                     bool async = true, LoadPriority priority = LoadPriority.Normal)
        {
            if (info == null || string.IsNullOrEmpty(name))
            {
                if (onLoaded != null)
                {
                    onLoaded(group, null);
                }

                return;
            }

            if (!async)
            {
                // 同步,直接加载
                //Logger.Log(string.Format("-->LoadBundleAsset: {0}", name));

                Object asset = info.LoadAsset(name, type);
                if (onLoaded != null)
                {
                    onLoaded(group, asset);
                }
            }
            else
            {
                // 异步,创建一个加载器后加载
                BundleAssetLoadParam param = new BundleAssetLoadParam {
                    Bundle = info.Bundle,
                    Type   = type
                };

                m_task.AddLoadTask(group, Loader.LoaderType.BundleAsset, name, param, (group1, data1) => {
                    // 加载回调
                    if (onLoaded != null)
                    {
                        onLoaded(group1, data1);
                    }
                }, true, priority, true);
            }
        }
Example #3
0
        // 加载AssetBundle(先从persistentData读,没有找到则从streamingAssets读)
        public void LoadBundle(string path, LoadedHandler onLoaded,
                               bool async            = true, bool persistent = false, bool manifest = true,
                               LoadPriority priority = LoadPriority.Normal)
        {
            path = path.ToLower();
            LoadAssetBundle(null, path, (group, data) => {
                AssetBundle ab = null;

                AssetBundleInfo info = data as AssetBundleInfo;
                if (info != null)
                {
                    ab = info.Bundle;
                }

                if (onLoaded != null)
                {
                    onLoaded(ab);
                }
            }, async, persistent, manifest, priority);
        }
Example #4
0
        /*private void InitVersionData(bool start = true)
         * {
         *  Clear();
         *
         *  m_origns.Clear();
         *  for (int i = 0; i < VersionData.Inst.Items.Length; ++i)
         *  {
         *      VersionData.VersionItem item = VersionData.Inst.Items[i];
         *      m_origns.Add(item.Name, item.Md5);
         *  }
         *
         *  if (start)
         *  {
         *      LoadManifest();
         *  }
         * }*/

        /*public void SetPatchData(JSONClass list, bool clear = false)
         * {
         *  if (clear)
         *  {
         *      Clear();
         *  }
         *
         *  m_patchs.Clear();
         *  if (list != null)
         *  {
         *      foreach (KeyValuePair<string, JSONNode> item in list)
         *      {
         *          m_patchs.Add(item.Key, item.Value["md5"]);
         *      }
         *  }
         *
         *  LoadManifest();
         * }*/

        /*private void LoadVersion()
         * {
         *  Logger.Log("LoadVersion");
         *  if (ConstantData.EnableMd5Name)
         *  {
         *      string pathPatch = string.Format("{0}/version_patch", Application.persistentDataPath);
         *      bool hasPatch = false;
         *
         *      if (ConstantData.EnablePatch)
         *      {
         *          hasPatch = File.Exists(pathPatch);
         *      }
         *
         *      InitVersionData(!hasPatch);
         *
         *      if (hasPatch)
         *      {
         *          LoadStream(pathPatch, (data) =>
         *          {
         *              byte[] bytes = data as byte[];
         *              if (bytes == null)
         *              {
         *                  Logger.LogError("Load patch version Failed!");
         *                  return;
         *              }
         *
         *              string text = Encoding.UTF8.GetString(bytes);
         *              JSONClass json = JSONNode.Parse(text) as JSONClass;
         *              if (json == null)
         *              {
         *                  Logger.LogError("Load patch version Failed!");
         *                  return;
         *              }
         *
         *              JSONClass jsonList = null;
         *              if (string.Equals(json["version"], LogicConstantData.Version))
         *              {
         *                  jsonList = json["list"] as JSONClass;
         *              }
         *
         *              m_urlPatch = json["url"];
         *              SetPatchData(jsonList);
         *          }, false, false, true);
         *      }
         *  }
         *  else
         *  {
         *      LoadManifest();
         *  }
         * }*/

        // 加载资源清单
        private void LoadManifest()
        {
            Logger.Log("LoadManifest");

            if (m_manifest != null)
            {
                Object.DestroyImmediate(m_manifest, true);
                m_manifest = null;
            }

            if (m_mapping != null)
            {
                Object.DestroyImmediate(m_mapping, true);
                m_mapping = null;
            }

            // 加载AssetBundle依赖文件
            LoadAssetBundle(null, ConstantData.AssetbundleManifest, (group, data) => {
                AssetBundleInfo ab = data as AssetBundleInfo;
                if (ab != null)
                {
                    m_manifest = ab.LoadAsset <AssetBundleManifest>("AssetBundleManifest");
                }
            }, false, false, false);

            // 加载资源到AssetBundle映射表
            string name = ConstantData.AssetbundleMapping;

            LoadAssetFromBundle(null, name, name, typeof(AssetBundleMapping), (group, data) => {
                m_mapping = data as AssetBundleMapping;
                if (m_mapping != null)
                {
                    m_mapping.Init();
                }
            }, false);

            Logger.Log("LoadManifest End");
        }
Example #5
0
        /// <summary>
        /// 清除AssetBundle缓存
        /// </summary>
        /// <param name="onlyRefZero">只清除引用计算为0的</param>
        /// <param name="onlyTimeout">只清除缓存时间已到的</param>
        /// <param name="includePersistent">是否包含常驻资源</param>
        public void Clear(bool onlyRefZero = true, bool onlyTimeout = true, bool includePersistent = false)
        {
            string[] keys = new string[m_caches.Count];
            m_caches.Keys.CopyTo(keys, 0);

            for (int i = 0; i < keys.Length; ++i)
            {
                string          key  = keys[i];
                AssetBundleInfo item = m_caches[key];
                if (onlyRefZero)
                {
                    // 只清除引用计数为0的
                    if (item.IsCanRemove && (!onlyTimeout || item.IsTimeOut))
                    {
                        UnloadAssetBundleInfo(key);
                    }
                }
                else if (includePersistent || item.IsCanRemove)
                {
                    UnloadAssetBundleInfo(key);
                }
            }
        }
Example #6
0
        // 加载AssetBundle(先从persistentData读,没有找到则从streamingAssets读,带后缀)
        private void LoadAssetBundle(LoaderGroup group, string path, GroupLoadedCallback onLoaded,
                                     bool async            = true, bool persistent = false, bool manifest = true,
                                     LoadPriority priority = LoadPriority.Normal)
        {
            path = path.ToLower();

            if (async && group == null)
            {
                group = m_task.PopGroup(priority);
            }

            if (manifest)
            {
                if (!HasBundle(path))
                {
                    // Manifest里没有这个AssetBundle,说明是一个错误的路径
                    Logger.LogError(string.Format("ab不存在:{0}", path));
                    if (onLoaded != null)
                    {
                        if (!async)
                        {
                            onLoaded(group, null);
                        }
                        else
                        {
                            m_task.AddAsyncCallback(onLoaded, group, null);
                        }
                    }

                    return;
                }

                // 加载依赖
                LoadDependencies(group, path, async, persistent);
            }

            // 检查是否有缓存
            if (m_cache.CheckAssetBundleInfo(group, path, onLoaded, persistent, async))
            {
                return;
            }

            // 添加加载任务
            m_task.AddLoadTask(group, Loader.LoaderType.Bundle, path, null, (group1, data) => {
                AssetBundle ab       = data as AssetBundle;
                AssetBundleInfo info = null;

                if (ab != null)
                {
                    info = m_cache.SetAssetBundle(path, ab);
#if UNITY_EDITOR
                    RefreshShader(ab);
#endif
                }

                // 加载回调
                if (onLoaded != null)
                {
                    onLoaded(group1, info);
                }
            }, async, priority);
        }