/// <summary>
        /// 初始化资源配置信息管理器,默认为UI和Effect特效资源
        /// </summary>
        public void Init(string strBaseResDir, string strBaseResWWWDir)
        {
            Resources.UnloadUnusedAssets();
            string path1 = ResourceManager.GetFullPath("data/ui.config", false);

            if (!this.InitUIResourceData(path1))
            {
                AssetLogger.Error(string.Format("false == InitUI(strUIConfig):{0}", path1));
            }

            /*string path2 = ResourceManager.GetFullPath("data/effect.config", false);
             * if (!this.InitEffectResourceData(path2))
             * {
             *  AssetLogger.Error(string.Format("false == InitEffect(strEffectConfig):{0}", path2));
             * }*/
            string path3 = ResourceManager.GetFullPath("data/atlas.config", false);

            if (!this.InitAtlasResourceData(path3))
            {
                AssetLogger.Error(string.Format("false == InitAtlas(strAtlasConfig):{0}", path3));
            }

            /*
             * string path4 = ResourceManager.GetFullPath("data/model.config", false);
             * if (!this.InitModelResourceData(path4))
             * {
             *  AssetLogger.Error(string.Format("false == InitModel(strModelConfig):{0}", path4));
             * }
             * */
        }
 public void RemoveAssetRequest(LocalAssetRequest request)
 {
     if (!this.m_listAssetRequest.Remove(request))
     {
         AssetLogger.Error("false == m_listAssetRequest.Remove(assetRequest):");
     }
 }
 /// <summary>
 /// 打印出错资源的信息
 /// </summary>
 public void DebugError()
 {
     foreach (var current in this.m_listErrorAsset)
     {
         AssetLogger.Error(current.URL + "has some error:" + current.Error);
     }
 }
示例#4
0
 public void DelRef(GameAssetRequest assetRequest)
 {
     if (!this.m_listAssetRequest.Remove(assetRequest))
     {
         AssetLogger.Error("false == m_listAssetRequest.Remove(assetRequest):");
     }
 }
        /// <summary>
        /// 加载模型资源
        /// </summary>
        /// <param name="path">Data/Model/HeroModel/{0}</param>
        /// <param name="callBackFun"></param>
        /// <param name="assetPRIType"></param>
        /// <returns></returns>
        public IAssetRequest LoadModel(string path, AssetRequestFinishedEventHandler callBackFun, AssetPRI assetPRIType)
        {
            IAssetRequest result;

            if (string.IsNullOrEmpty(path))
            {
                AssetLogger.Error("string.IsNullOrEmpty(path) == true");
                result = null;
            }
            else
            {
                string                 name                   = LocalResourceManager.ChangePathToFilenameWithoutExtension(path);
                ResourceData           resourceData           = null;
                List <ResourceData>    dependes               = null;
                CollectDepResourceData collectDepResourceData = this.m_DicModelResourceData.GetCollectDepResourceData(name, out resourceData, out dependes);
                if (null != collectDepResourceData)
                {
                    result = this.CreateAssetRequest(resourceData, dependes, callBackFun, assetPRIType);
                }
                else
                {
                    AssetLogger.Error(string.Format("null == collectDepResourceData:{0}", path));
                    result = null;
                }
            }
            return(result);
        }
 /// <summary>
 /// 设置引用资源的数目,初始化depAssetResource[]数组
 /// </summary>
 /// <param name="length"></param>
 public void SetDepSize(int length)
 {
     if (length > 0)
     {
         if (this.m_depAssetResources != null)
         {
             AssetLogger.Error("this.m_depAssetResource != null");
         }
         this.m_depAssetResources = new LocalAssetResource[length];
     }
 }
 /// <summary>
 /// 设置当前资源,初始化assetResource
 /// </summary>
 /// <param name="assetSO"></param>
 public void SetAsset(IAssetResource assetSO)
 {
     if (this.m_assetResource != null)
     {
         AssetLogger.Error(string.Format("null != m_assetResource:{0}", this.GetAssetResourceStates()));
         return;
     }
     if (assetSO != null)
     {
         this.m_assetResource = assetSO as LocalAssetResource;
         this.m_assetResource.AddRef();
     }
 }
 /// <summary>
 /// 释放已经加载的资源
 /// </summary>
 /// <param name="value"></param>
 protected void Dispose(bool value)
 {
     //如果已经释放了,就说明也不做
     if (this.m_isDispose)
     {
         return;
     }
     if (value)
     {
         LocalAssetResource.s_hashSetAssetURL.Remove(this.URL);
         string text = Path.GetFileName(this.URL.ToLower());      //取得指定url路径的文件名+后缀名
         if (!LocalAssetResource.s_hashSetAssetName.Remove(text)) //移除失败
         {
             AssetLogger.Error("s_hashSetAssetName.Remove(strAssetName) == false: " + text);
         }
         if (this.m_AssetBunle != null)
         {
             if (LocalAssetResource.m_dicAllAssetResource.ContainsKey(this.m_url))
             {
                 LocalAssetResource.m_dicAllAssetResource.Remove(this.m_url);
             }
             Debug.Log("Assetbundle卸载完全");
             this.m_AssetBunle.Unload(true);                             //卸载assetbundle上所有的资源
             this.m_AssetBunle = null;
             this.m_MainAsset  = null;                                   //设置MainAsset为null,过段时间会被回收
             AssetLogger.Debug(string.Format("Unload:[{0}]", this.URL)); //卸载成功
         }
         else
         {
             if (this.m_MainAsset != null)
             {
                 if (this.m_MainAsset is GameObject || this.m_MainAsset is Component)
                 {
                     UnityEngine.Object.DestroyImmediate(this.m_MainAsset, true);
                 }
             }
             else
             {
                 Resources.UnloadAsset(this.m_MainAsset);
             }
             this.m_MainAsset = null;
         }
         if (this.m_www != null)
         {
             this.m_www.Dispose();//释放www的资源
             this.m_www = null;
         }
         this.m_bCancel = true;
     }
     this.m_isDispose = true;//设置释放成功
 }
        public IAssetRequest CreateAssetRequest(string path, AssetRequestFinishedEventHandler callback, AssetPRI assetPRI, EnumAssetType assetType)
        {
            if (string.IsNullOrEmpty(path))
            {
                AssetLogger.Error("string.IsNullOrEmpty(path) == true");
                return(null);
            }
            string arg  = Path.GetDirectoryName(path).ToLower();
            string text = LocalResourceManager.ChangePathToFilenameWithoutExtension(path);

            path = string.Format("data/{0}/{1}.ab", arg, text);
            ResourceData resourceData = CollectDepResourceDataMap.RefResource(text, path, 0, assetType);

            return(this.CreateAssetRequest(resourceData, null, callback, assetPRI));
        }
 /// <summary>
 /// 初始化模型资源,从Json读取数据转成CollectDepResourceDataMap类型实例,并初始化
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 private bool InitModelResourceData(string path)
 {
     using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
     {
         JsonReader reader = new JsonReader(streamReader);
         this.m_DicModelResourceData = JsonMapper.ToObject <CollectDepResourceDataMap>(reader);
     }
     if (this.m_DicModelResourceData == null)
     {
         AssetLogger.Error("null == m_dicModelResourceData");
         this.m_DicModelResourceData = new CollectDepResourceDataMap();
         return(false);
     }
     CollectDepResourceDataMap.AddResourceDatas(this.m_DicModelResourceData.mDicResourceData);
     this.m_DicModelResourceData.mDicResourceData.Clear();
     return(true);
 }
        public IAssetRequest LoadScene(string path, AssetRequestFinishedEventHandler callBackFun, AssetPRI assetPRIType)
        {
            IAssetRequest result;

            if (string.IsNullOrEmpty(path))
            {
                AssetLogger.Error("string.IsNullOrEmpty(path) == true");
                result = null;
            }
            else
            {
                string directoryName = Path.GetDirectoryName(path);
                string text          = LocalResourceManager.ChangePathToFilenameWithoutExtension(path);
                path = string.Format("data/{0}/{1}.unity3d", directoryName, text);
                ResourceData resourceData = CollectDepResourceDataMap.RefResource(text, path, 0, EnumAssetType.eAssetType_Scene);
                result = this.CreateAssetRequest(resourceData, null, callBackFun, assetPRIType);
            }
            return(result);
        }
        /// <summary>
        /// 加载UI资源
        /// </summary>
        /// <param name="path"></param>
        /// <param name="callback"></param>
        /// <param name="assetPRI"></param>
        /// <returns></returns>
        public IAssetRequest LoadUI(string path, AssetRequestFinishedEventHandler callback, AssetPRI assetPRI)
        {
            if (string.IsNullOrEmpty(path))
            {
                AssetLogger.Error("string.IsNullOrEmpty(path) == true");
                return(null);
            }
            string                 text                   = LocalResourceManager.ChangePathToFilenameWithoutExtension(path);//路径转成文件名不带后缀
            ResourceData           resourceData           = null;
            List <ResourceData>    dependes               = null;
            CollectDepResourceData collectDepResourceData = this.m_DicUIResourceData.GetCollectDepResourceData(text, out resourceData, out dependes);

            if (collectDepResourceData != null)
            {
                return(this.CreateAssetRequest(resourceData, dependes, callback, assetPRI));//创建请求资源实例
            }
            AssetLogger.Error(string.Format("null == collectDepResourceData:{0}", text));
            return(null);
        }
        public IAssetRequest LoadEffect(string path, AssetRequestFinishedEventHandler callback, AssetPRI assetPRI)
        {
            if (string.IsNullOrEmpty(path))
            {
                AssetLogger.Error("string.IsNullOrEmpty(path) == true");
                return(null);
            }
            string                 name                   = LocalResourceManager.ChangePathToFilenameWithoutExtension(path);
            ResourceData           resourceData           = null;
            List <ResourceData>    list                   = null;
            CollectDepResourceData collectDepResourceData = this.m_DicEffectReourceData.GetCollectDepResourceData(name, out resourceData, out list);

            if (collectDepResourceData != null)
            {
                return(this.CreateAssetRequest(resourceData, list, callback, assetPRI));
            }
            AssetLogger.Error(string.Format("null == collectDepResourceData:{0}", path));
            return(null);
        }
 /// <summary>
 /// 主要初始化url,resourceData,isfinished=false
 /// </summary>
 /// <param name="data"></param>
 public LocalAssetResource(ResourceData data)
 {
     if (data == null)
     {
         AssetLogger.Error("null == resourceData");
     }
     this.m_resourceData = data;
     if (this.m_resourceData != null)
     {
         if (this.m_resourceData.mPath.EndsWith(".unity3d"))
         {
             this.m_url = ResourceManager.GetFullPath(this.m_resourceData.mPath, true);
         }
         else
         {
             this.m_url = ResourceManager.GetFullPath(this.m_resourceData.mPath, false);
         }
     }
     this.m_bCancel = false;
 }
        public void DetectComplete()
        {
            if (!this.m_HasCallBack && this.DetectAllHasFinished())
            {
                if (this.m_assetResource != null)
                {
                    AssetLogger.Debug("Asset: DetectComplete:" + this.m_assetResource.URL);
                }
                else
                {
                    AssetLogger.Error("Asset: DetectComplete: null == m_assetResource");
                }
                this.m_HasCallBack = true; //设置加载完成
                this.DebugError();         //打印出出错的资源
                this.LoadMainAsset();

                /*Debug.Log("Main:" + this.m_assetResource.MainAsset);
                 * for (int j = 0; j < this.m_depAssetResources.Length; j++)
                 * {
                 *  Debug.Log("dep:" + this.m_depAssetResources[j].MainAsset);
                 * }
                 */
                try
                {
                    for (int i = 0; i < this.m_listAssetRequest.Count; i++)
                    {
                        LocalAssetRequest request = this.m_listAssetRequest[i];
                        request.OnAssetRequestFinishedHandler(this.m_assetResource);//执行资源加载完成之后的委托
                    }
                }
                catch (Exception e)
                {
                    AssetLogger.Fatal(e.ToString());
                }
                this.UnloadAssetBundle();
            }
        }
 /// <summary>
 /// 开始加载assetbundle,通过本地url从内存中加载
 /// </summary>
 public void BeginDownload()
 {
     this.m_dataBeginLoadTime = DateTime.Now;
     try
     {
         AssetLogger.Debug("BeginDownLoad:" + this.URL);
         this.m_isBeginDownload = true;
         if (LocalAssetResource.s_hashSetAssetURL.Contains(this.URL))//已经加载过了
         {
             AssetLogger.Error("s_hashSet.Contains(m_strAssetUrl) == true: " + this.URL);
             this.m_isDone = true;
         }
         else
         {
             string text = Path.GetFileName(this.URL).ToLower();                                              //取得要加载的资源名(小写)
             if (!LocalAssetResource.s_hashSetAssetName.Add(text))                                            //添加资源名到资源名集合中
             {
                 AssetLogger.Error(string.Format("s_hashSetAssetName.Add(strAssetName) == false:{0}", text)); //添加失败
             }
             if (!this.m_url.EndsWith(".unity3d"))
             {
                 string text2 = this.URL;
                 if (this.URL.IndexOf("file:///") == 0)
                 {
                     text2 = this.URL.Substring(8);
                 }
                 FileInfo fileInfo = new FileInfo(text2);
                 if (fileInfo.Exists)
                 {
                     /*FileStream fileStream = fileInfo.OpenRead();//打开文件流
                      * Debug.Log("doload"+fileStream.Length);
                      * byte[] array = new byte[fileStream.Length];
                      * fileStream.Read(array, 0, (int)fileStream.Length);//读取资源
                      * fileStream.Close();
                      * this.m_AssetBunle = AssetBundle.CreateFromMemoryImmediate(array);//从内存中创建assetbundleCreateRequest,这个比较占内存,虽然是异步的
                      */
                     this.m_AssetBunle = AssetBundle.CreateFromFile(text2);
                     if (!LocalAssetResource.m_dicAllAssetResource.ContainsKey(this.m_url))
                     {
                         LocalAssetResource.m_dicAllAssetResource.Add(this.m_url, this);
                         if (LocalAssetResource.m_dicAllAssetResource.Count == 200)
                         {
                             AssetLogger.Debug(string.Format("AssetBundle count Exceed 200, it is dangerous!", new object[0]));
                         }
                     }
                     this.m_isDone = true;
                 }
                 else
                 {
                     //不存在文件资源,报错
                     AssetLogger.Error(string.Format("fileInfo.Exists == false:{0}", text2));
                     this.m_isDone = true;
                 }
             }
             else
             {
                 //如果平台不是windows,那么就用www来加载资源
                 AssetLogger.Debug("Scene: BeginDownLoad:" + this.URL);
                 this.m_www = new WWW(this.URL);
             }
             LocalAssetResource.s_hashSetAssetURL.Add(this.URL);//加载完成之后,把资源的url添加到url集合中
         }
     }
     catch (Exception e)
     {
         AssetLogger.Fatal(e.ToString());
         this.m_isDone = true;
     }
 }
        public void OnUpdate()
        {
            if (!this.UnLoadNotUsedResource() && this.m_allUnLoadFinishedEventHandler != null)
            {
                GC.Collect();
                Resources.UnloadUnusedAssets();
                Action <bool> allUnLoadFinishedEventHandler = this.m_allUnLoadFinishedEventHandler;
                this.m_allUnLoadFinishedEventHandler = null;
                allUnLoadFinishedEventHandler(true);
            }
            LinkedListNode <LocalAssetResource> next;

            for (LinkedListNode <LocalAssetResource> linkedListNode = this.m_linkedListAssetResourceInLoading.First; linkedListNode != null; linkedListNode = next)
            {
                next = linkedListNode.Next;
                LocalAssetResource value = linkedListNode.Value;
                if (value.IsDone)//已经加载完成
                {
                    Debug.Log(value.URL + "已经下载过了");
                    if (!string.IsNullOrEmpty(value.Error))//如果加载没错的话
                    {
                        this.m_bLoadError = true;
                        AssetLogger.Error(value.Error);
                    }
                    if (this.m_allLoadFinishedEventHandler != null)
                    {
                        this.m_current += value.Size;
                    }
                    this.m_linkedListAssetResourceInLoading.Remove(linkedListNode);
                    value.OnLoaded();
                }
                else //还没有加载
                {
                    if (value.Canceled)//如果已经取消就从链表中移除
                    {
                        Debug.Log("取消下载:" + value.URL);
                        this.m_linkedListAssetResourceInLoading.Remove(linkedListNode);
                    }
                    else
                    {
                        if (!value.Started)
                        {
                            Debug.Log(value.URL + "开始下载");
                            value.BeginDownload();
                        }
                    }
                }
            }
            //如果平台是手机的话,就处理1个,不是的话处理5个
            int dealCount = Application.isMobilePlatform ? 1 : 10;

            if (this.m_linkedListNeedToLoad.Count > 0 && this.m_linkedListAssetResourceInLoading.Count < dealCount)//保持一次处理5个asset
            {
                Debug.Log(this.m_linkedListNeedToLoad.Count);
                LocalAssetResource value2 = this.m_linkedListNeedToLoad.First.Value;
                this.m_linkedListNeedToLoad.RemoveFirst();
                Debug.Log("AddLast:" + value2.URL);
                this.m_linkedListAssetResourceInLoading.AddLast(value2);
            }
            //如果没有加载任务了,就表示完成执行完成回调函数
            if (this.m_linkedListNeedToLoad.Count == 0 && this.m_linkedListAssetResourceInLoading.Count == 0 && this.m_allLoadFinishedEventHandler != null)
            {
                Action <bool> allLoadFinishedEventHandler = this.m_allLoadFinishedEventHandler;
                this.m_allLoadFinishedEventHandler = null;
                try
                {
                    allLoadFinishedEventHandler(!this.m_bLoadError);
                }
                catch (Exception e)
                {
                    AssetLogger.Fatal(e.ToString());
                }
                this.m_total   = 0;
                this.m_current = 0;
            }
        }