/// <summary> /// 通过http请求一个字符串 /// 请求的可以是一个文本,也会以字符串形式返回 /// </summary> /// <param name="_url"></param> /// <param name="_callback"></param> public void GetString(string _url, Action <string> _callback) { if (false == HKRequestTools.IsCheckUrl(_url)) { return; } httpClient.GetString(new Uri(_url), response => { if (null != _callback) { if (true == response.IsSuccessStatusCode) { if (null != response.Data) { _callback(response.Data); } } else { this.Error(StringCacheFactory.GetFree().Add("Download fail url = ") .Add(_url).Add("\terror code = ").Add(response.StatusCode)); _callback(null); } } }); }
/// <summary> /// 检测并返回一个Audio Item /// </summary> /// <param name="_name">音效名字</param> /// <param name="_category">音效类别</param> /// <returns></returns> public AudioItem CheckAudioItem(string _name, string _category) { if (true == string.IsNullOrEmpty(_name)) { return(null); } if (true == AudioController.IsValidAudioID(_name)) { return(AudioController.GetAudioItem(_name)); } AudioClip audioClip = null; // 加载音频资源 if (null != loadResEvent) { var audioObject = loadResEvent(_name, "audio"); if (null != audioObject && true == audioObject is AudioClip) { audioClip = (AudioClip)audioObject; } } if (null == audioClip) { this.Warr(StringCacheFactory.GetFree().Add("无法加载音频 = ").Add(_name)); return(null); } var audio_category = AudioController.GetCategory(_category); if (null != audio_category) { AudioItem audioItem = AudioController.AddToCategory(audio_category, audioClip, _name); return(audioItem); } return(null); }
/// <summary> /// 将一个数组,以AssetBundle的形式写入到本地目录中 /// </summary> /// <param name="_url"></param> /// <param name="_bytes"></param> public static void WriteAssetBundleToLcoal(string _url, byte[] _bytes) { if (true == string.IsNullOrEmpty(_url)) { logger.Error("写入时,原始路径为空,无法写入"); return; } if (null == _bytes || _bytes.Length <= 0) { logger.Error(StringCacheFactory.GetFree().Add("写入本地时,原始数据为null = ") .Add(_url)); return; } // 写入本地 如果有重复则会覆盖 string savePath = Path.GetFileName(_url); if (false == string.IsNullOrEmpty(DownloadBaseUrl)) { savePath = _url.Replace(DownloadBaseUrl, ""); } // 调用File系统,将字节写入到本地文件中,路径保持和网络格式路径格式相同 App.Make <IFileSystemManager>().Disk().Write(savePath, _bytes); }
/// <summary> /// 被销毁时会调用到这里 /// </summary> private void OnDestroy() { if (false == IsDestroyTag && false == HKCommonDefine.IsApplicationQuit) // 如果为false,说是被动销毁 { this.Error(StringCacheFactory.GetFree().Add("非法销毁 = ") .Add(gameObject.name) .Add(" parent name = ").Add(transform.parent.name).Add(" group name = ").Add(parentGroupName)); } }
/// <summary> /// debug信息打印 /// debug主要是在调试时打印 /// 生产环境中,不会打印出来 /// debug信息调试完成后,应该自觉屏蔽 /// </summary> /// <param name="_object"></param> /// <param name="_message"></param> /// <param name="_params">附加参数,只有在Dev环境中才会存在,在上线时dev的都会被屏蔽掉,所以不会有new object[]的额外开销</param> public static void Debug(this object _object, string _message, params object[] _params) { var strApp = StringCacheFactory.GetFree().Add(_message); if (_params.Length > 0) { for (int index = 0; index < _params.Length; index++) { strApp.Add(_params[index]); } } _object.LogMessage(LogLevels.Debug, strApp.Release()); }
/// <summary> /// Release /// </summary> /// <returns></returns> public string Release() { if (null != cacheList && cacheList.Count > 0) { for (int index = 0; index < cacheList.Count; index++) { if (index > 0 && false == string.IsNullOrEmpty(split)) { sp.Append(split); } var str = cacheList[index]; sp.Append(str); } string result = sp.ToString(); StringCacheFactory.Recover(this); return(result); } return(""); }
/// <summary> /// 资源下载失败的处理 /// 要向资源错误原因中添加一条错误记录 /// </summary> /// <param name="_item"></param> /// <param name="_errorCode"></param> private void DownloadFailExecute(HKDownloadTaskItem _item, int _errorCode) { if (null == errorInfoCache) { errorInfoCache = StringCacheFactory.GetFree(); errorInfoCache.SetSplit("\n"); } if (_item.CurrentSurplusRetryCount > 0) { RetryExecute(_item); errorInfoCache.Add(_item.Url).Add(" 下载失败,启动第").Add(_item.RetryCount - _item.CurrentSurplusRetryCount) .Add("次重试"); } else { errorInfoCache.Add(_item.Url).Add(" 下载失败 = ").Add("_").Add(_errorCode); // 使用完后,就回收到内存池中 HKDownloadTaskItem.downloadItemCache.Recover(_item); } // 检测当前剩余下载对象,并继续下载 CheckQueueComplete(); }