private void _loadAssetBundleAndAsset(string _bundle_name, string _asset_name, Action <ResData, UnityEngine.Object> _action, bool async = true) { ResConfig config = GetResConfig(_bundle_name); if (config == null) { return; } ResData data = GetResData(config, true); data.LoadAssetBundle(_asset_name, _action, async); }
public ResData GetResData(ResConfig _config, bool _create = false) { if (m_res_datas.ContainsKey(_config.BundleName)) { return(m_res_datas[_config.BundleName]); } if (_create) { ResData data = new ResData(_config); m_res_datas[_config.BundleName] = data; return(data); } return(null); }
public void UnInit() { m_state = ResDataState.Init; if (m_bundle != null) { m_bundle.Unload(false); } m_bundle = null; m_bundle_async = true; m_loaded_dependencies_count = 0; m_reference_count = 0; m_request_assets.Clear(); ResManager.Instance.RemoveResData(m_res_config.BundleName); m_res_config = null; }
private IEnumerator _downloadResList() { string url = string.Format("{0}/res_list.assetbundle", m_res_server_url); UnityWebRequest www = UnityWebRequest.GetAssetBundle(url); yield return(www.SendWebRequest()); if (www.error != null) { Debug.LogErrorFormat("下载最新资源列表失败 {0} {1}", url, www.error); if (m_complete != null) { m_complete(); } yield break; } AssetBundle ab = DownloadHandlerAssetBundle.GetContent(www); m_server_res = ab.LoadAsset <TextAsset>("res_list"); ResManager.Deserialize(m_server_res.bytes, m_server_res_config, null); foreach (var res in m_server_res_config) { ResConfig config = ResManager.Instance.GetResConfig(res.Key); if (config == null || config.Md5.CompareTo(res.Value.Md5) != 0) { m_need_download_res.Add(res.Value); } } if (m_need_download_res.Count == 0 && m_complete != null) { m_complete(); } else { StartCoroutine(_downloadRes()); } }
public static void BuildResList(Dictionary <string, ResConfig> _res_list) { AssetBundle ab = AssetBundle.LoadFromFile(string.Format("{0}/StreamingAssets", Application.streamingAssetsPath)); AssetBundleManifest manifest = ab.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); string[] bundles = manifest.GetAllAssetBundles(); if (_res_list == null) { _res_list = new Dictionary <string, ResConfig>(); } for (int i = 0; i < bundles.Length; i++) { ResConfig config = new ResConfig(); config.BundleName = bundles[i]; config.Md5 = manifest.GetAssetBundleHash(bundles[i]).ToString(); string[] depen = manifest.GetAllDependencies(bundles[i]); config.Dependencies.AddRange(depen); config.Size = GetAssetBundleSize(string.Format("{0}/{1}", Application.streamingAssetsPath, bundles[i])); if (config.Size == 0) { Debug.LogErrorFormat("AB包 {0}的大小居然为0 请检查!", bundles[i]); } string[] paths = BuildRule.GetAssetPathByBundle(config.BundleName); //string[] paths = AssetDatabase.GetAssetPathsFromAssetBundle( config.BundleName ); for (int j = 0; j < paths.Length; j++) { config.Assets.Add(paths[j]); } if (_res_list.ContainsKey(config.BundleName)) //打包选中才有这种情况 { _res_list[config.BundleName] = config; } else { _res_list.Add(config.BundleName, config); } } ab.Unload(true); SaveResList(ResListPath, _res_list); AssetDatabase.Refresh(); BuildPipeline.BuildAssetBundles(Path, new AssetBundleBuild[] { new AssetBundleBuild() { assetBundleName = "res_list.assetbundle", assetNames = new[] { ResListPath } } }, Options, TargetPlatform); if (File.Exists(string.Format("{0}/StreamingAssets", Application.streamingAssetsPath))) { File.Delete(string.Format("{0}/StreamingAssets", Application.streamingAssetsPath)); } var alls = GetFiles(new DirectoryInfo(Path)); foreach (var file in alls) { if (file.EndsWith(".manifest")) { File.Delete(file); } } File.Delete(Application.persistentDataPath + "/res_list"); AssetDatabase.Refresh(); }
public static void Deserialize(byte[] _bytes, Dictionary <string, ResConfig> _res_config, Dictionary <string, string> _res_path) { try { using (MemoryStream memory_stream = new MemoryStream(_bytes)) { using (StreamReader reader = new StreamReader(memory_stream, Encoding.UTF8)) { if (!Equals(Encoding.UTF8, reader.CurrentEncoding)) { Debug.LogErrorFormat("res_list文件 {0} 编码不是UTF-8!", reader.CurrentEncoding.EncodingName); return; } if (reader.EndOfStream) { return; } Int32 line_count = 0; while (!reader.EndOfStream) { String line = reader.ReadLine(); if (String.IsNullOrEmpty(line)) { continue; } line_count++; String[] parts = line.Split('\t'); ResConfig config = new ResConfig(); config.BundleName = parts[0]; config.Size = Int64.Parse(parts[1], NumberStyles.None); config.Version = uint.Parse(parts[2]); config.Md5 = parts[3]; string[] depen = parts[4].Split(','); for (int i = 0; i < depen.Length; i++) { if (depen[i] != string.Empty) { config.Dependencies.Add(depen[i]); } } string[] asset = parts[5].Split(','); for (int i = 0; i < asset.Length; i++) { if (asset[i] != string.Empty) { config.Assets.Add(asset[i]); if (_res_path == null) { continue; } if (_res_path.ContainsKey(asset[i])) { Debug.LogErrorFormat("Asset:{0} 被打入到了多个Bundle {1}", asset[i], config.BundleName); } else { _res_path.Add(asset[i], config.BundleName); } } } _res_config.Add(config.BundleName, config); } } } } catch (Exception e) { Debug.LogErrorFormat("解析 res_list 文件失败!\tMessage: {0}\nStackTrace: {1} {2}", e.Message, e.StackTrace, e.ToString()); } }
public ResData(ResConfig _config) { m_state = ResDataState.Init; m_res_config = _config; }