AssetBundle LoadAssetBundle(string bundleName) { var bundleCrc = CRC32.GetCRC32(bundleName); AssetBundleItem item = null; if (cacheAssetBundleItemDic.TryGetValue(bundleCrc, out item)) { item.RefCount += 1; return(item.Bundle); } AssetBundle assetBundle = null; var fullPath = PathDefine.ABLoadPath + bundleName; if (File.Exists(fullPath)) { assetBundle = AssetBundle.LoadFromFile(fullPath); } if (assetBundle == null) { Debug.LogErrorFormat("load bundle error {0}", bundleName); return(null); } item = assetBundleItemPool.Spawn(true); item.Bundle = assetBundle; item.RefCount++; cacheAssetBundleItemDic.Add(bundleCrc, item); return(assetBundle); }
private void DrawAssetBundleItem(AssetBundleItem assetBundleItem) { EditorGUILayout.BeginHorizontal(); { string title = assetBundleItem.Name; if (assetBundleItem.AssetBundle.Packed) { title = "[Packed] " + title; } float emptySpace = position.width; if (EditorGUILayout.Toggle(m_SelectedAssetBundle == assetBundleItem.AssetBundle, GUILayout.Width(emptySpace - 12f))) { ChangeSelectedAssetBundle(assetBundleItem.AssetBundle); } else if (m_SelectedAssetBundle == assetBundleItem.AssetBundle) { ChangeSelectedAssetBundle(null); } GUILayout.Space(-emptySpace + 24f); GUI.DrawTexture(new Rect(32f + 14f * assetBundleItem.Depth, 20f * m_CurrentAssetBundleRowOnDraw + 1f, 16f, 16f), assetBundleItem.Icon); EditorGUILayout.LabelField(string.Empty, GUILayout.Width(26f + 14f * assetBundleItem.Depth), GUILayout.Height(18f)); EditorGUILayout.LabelField(title); } EditorGUILayout.EndHorizontal(); m_CurrentAssetBundleRowOnDraw++; }
/// <summary> /// 通过资源名字加载ab包,但是加载过的ab包需要保存起来 /// </summary> /// <param name="name">资源名</param> /// <returns></returns> private AssetBundle LoadAB(string abName) { uint nameCrc = Crc32.GetCRC32(abName); AssetBundleItem abItem = null; if (!m_crcABItemDic.TryGetValue(nameCrc, out abItem)) { AssetBundle tempAB = null; string fullPath = PathConfig.AssetBundleTargetPath + "/" + abName; tempAB = AssetBundle.LoadFromFile(fullPath); //加载ab包 if (tempAB == null) { Debug.LogError("没有对应名字的AssetBundle"); return(null); } abItem = m_abItemPool.Create(true); abItem.AB = tempAB; abItem.RefCount++; m_crcABItemDic.Add(nameCrc, abItem); } else { abItem.RefCount++; } return(abItem.AB); }
/// <summary> /// 加载单个assetbundle根据名字 /// </summary> /// <param name="abName"></param> /// <returns></returns> private AssetBundle LoadAssetBundle(string abName) { AssetBundleItem item = null; uint crc = CRC32.GetCRC32(abName); if (!m_AssetBundleItemDic.TryGetValue(crc, out item)) { AssetBundle assetBundle = null; string fullPath = ABLoadPath + abName; assetBundle = AssetBundle.LoadFromFile(fullPath); if (assetBundle == null) { Debug.LogError(" Load AssetBundle Error : " + fullPath); } item = m_AssetBundleItemPool.Spawn(true); item.assetBundle = assetBundle; item.RefCount++; m_AssetBundleItemDic.Add(crc, item); } else { item.RefCount++; } return(item.assetBundle); }
/// <summary> /// 加载ab包 /// </summary> /// <param name="abName"></param> /// <returns></returns> public AssetBundle LoadAssetBundle(string abName) { uint crc = Crc.GetCRC32(abName); AssetBundleItem item = null; if (!m_AssetBundleItemDic.TryGetValue(crc, out item)) { string path = ABLoadpath + abName; AssetBundle ab = null; //if (File.Exists(path))//移动端不支持File访问 ab = AssetBundle.LoadFromFile(path); if (ab == null) { Debug.LogError("ab包加载失败,请检查" + path); return(null); } item = m_AssetBundleItemPool.Spawn(); item.assetBundle = ab; item.refCount++; m_AssetBundleItemDic.Add(crc, item); } else { item.refCount++; } return(item.assetBundle); }
private static void _updateGroup(AssetBundleItem assetBundleItem, string groupStr) { assetBundleItem.AssetBundle.ClearResourceGroup(); if (string.IsNullOrWhiteSpace(groupStr)) { return; } var newGroup = groupStr.Split(','); foreach (string group in newGroup) { if (string.IsNullOrWhiteSpace(@group)) { Debug.LogError( $"Resource Group Name cannot be Null Or WhiteSpace. Asset Bundle Path:{assetBundleItem.FromRootPath}"); continue; } if (!assetBundleItem.AssetBundle.HasResourceGroup(@group)) { assetBundleItem.AssetBundle.AddResourceGroup(@group); } } }
/// <summary> /// 加载单个 assetbundle,根据名字 /// </summary> /// <param name="name"></param> /// <returns></returns> private AssetBundle LoadAssetBundle(string name) { AssetBundleItem item = null; uint crc = CRC32.GetCRC32(name); if (!assetBundleItemDic.TryGetValue(crc, out item)) { // 加载 ab AssetBundle assetBundle = null; string fullPath = ABLoadPath + name; assetBundle = AssetBundle.LoadFromFile(fullPath); if (assetBundle == null) { Debug.LogError(" Load AssetBundle Error:" + fullPath); } // 给 AssetBundleItem 赋值 item = assetBundleItemPool.Spawn(true); item.assetBundle = assetBundle; item.RefCount++; assetBundleItemDic.Add(crc, item); } else { item.RefCount++; // 已经加载过了,此次加载就不需要重复加载 ab,而是把引用计数 +1 } return(item.assetBundle); }
/// <summary> /// 加载AB资源 /// </summary> private AssetBundle LoadAssetBundle(string name) { AssetBundleItem item = null; uint crc = CRC32.GetCRC32(name); if (!abItemDict.TryGetValue(crc, out item)) { AssetBundle assetBundle = null; string fullPath = ConstConfig.ASSETBUNDLEPATH + "/" + name; //if (!File.Exists(fullPath)) //{ // Debug.LogError("Can't find Asset, path:" + fullPath); // return assetBundle; //} assetBundle = AssetBundle.LoadFromFile(fullPath); if (assetBundle == null) { Debug.LogError("Load AssetBundle error :" + fullPath); } item = abItemClassPool.Spawn(); item.m_AssetBundle = assetBundle; item.m_RefCount++; abItemDict.Add(crc, item); } else { item.m_RefCount++; } return(item.m_AssetBundle); }
/// <summary> /// 加载单个assetbundle根据名字 /// </summary> /// <param name="name"></param> /// <returns></returns> private AssetBundle LoadAssetBundle(string name) { AssetBundleItem item = null; uint crc = Crc32.GetCrc32(name); if (!m_AssetBundleItemDic.TryGetValue(crc, out item)) { AssetBundle assetBundle = null; string hotABPath = HotPatchManager.Instance.ComputeABPath(name); //如果有热更的,走热更,没有就走本地 string fullPath = string.IsNullOrEmpty(hotABPath) ? ABLoadPath + name : hotABPath; byte[] bytes = AES.AESFileByteDecrypt(fullPath, "Ocean"); assetBundle = AssetBundle.LoadFromMemory(bytes); if (assetBundle == null) { Debug.LogError(" Load AssetBundle Error:" + fullPath); } item = m_AssetBundleItemPool.Spawn(true); item.assetBundle = assetBundle; item.RefCount++; m_AssetBundleItemDic.Add(crc, item); } else { item.RefCount++; } return(item.assetBundle); }
private void GenerateResourceFile() { StringBuilder sb = new StringBuilder(); sb.Append("Id,BundleName,BundleFullName,Size,Md5\r"); for (int i = 0; i < abItemList.Count; ++i) { AssetBundleItem item = abItemList[i]; sb.Append((i + 1).ToString()); sb.Append(","); sb.Append(item.bundleName); sb.Append(","); sb.Append(item.bundleFullName); sb.Append(","); sb.Append(item.size.ToString()); sb.Append(","); sb.Append(item.md5); if (i < abItemList.Count - 1) { sb.Append('\r'); } } string resourceFilePath = GetAssetBundlePath(curBuildTarget) + resourceFile; IOTools.WriteFile(resourceFilePath, sb.ToString()); }
/// <summary> /// 加载AB包 /// </summary> /// <param name="name">streamingAssetsPath ABname</param> /// <returns></returns> public AssetBundle LoadAssetBundle(string name) { AssetBundleItem item = null; string path = BundleTargetPath + name; uint crc = Crc32.GetCrc32(path); if (!m_AssetBundleItemDic.TryGetValue(crc, out item)) { AssetBundle ab = null; ab = AssetBundle.LoadFromFile(path); if (ab == null) { Debug.LogError("AssetBundleManager.LoadAssetBundle AB包为null path:" + path); } else { item = m_AssetBundleItemPool.Spawn(true); item.assetBundle = ab; item.refCount++; m_AssetBundleItemDic.Add(crc, item); } } else { item.refCount++; } return(item.assetBundle); }
/// <summary> /// 加载单个assetbundle根据名字 /// </summary> /// <returns>AssetBundle</returns> private AssetBundle LoadAssetBundle(string name) { AssetBundleItem item = null; uint crc = CRC32.GetCRC32(name); if (!_assetBundleItemDic.TryGetValue(crc, out item)) { AssetBundle assetBundle = null; string fullPath = Application.streamingAssetsPath + "/" + name; if (File.Exists(fullPath)) { assetBundle = AssetBundle.LoadFromFile(fullPath); } if (assetBundle == null) { Debug.LogError("Load AssetBundle Error Path:" + fullPath); } item = _assetBundleItemPool.Spawn(true); item.assetbundle = assetBundle; item.refCount++; _assetBundleItemDic.Add(crc, item); } else { item.refCount++; } return(item.assetbundle); }
/** アセットバンドルアイテム。登録。 */ public void Regist(string a_assetbundle_name, AssetBundleItem a_assetbundle_item) { if (this.list.ContainsKey(a_assetbundle_name) == false) { this.list.Add(a_assetbundle_name, a_assetbundle_item); } else { Tool.Assert(false); } }
public static void ComputeFileCRC() { var platform = GetPlatformFolderForAssetBundles(EditorUserBuildSettings.activeBuildTarget); var root = System.IO.Path.GetFullPath(Application.dataPath + "/../AssetBundles/" + platform + "/"); var platformBundle = System.IO.Path.Combine(root, platform); var assetBundle = AssetBundle.LoadFromFile(platformBundle); if (null == assetBundle) { Debug.LogErrorFormat("[compute_file_crc]:failed load main assetbundle:[0] failed ...", platformBundle); return; } AssetBundleManifest assetBundleManifest = assetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); if (null == assetBundleManifest) { Debug.LogErrorFormat("load assetbundlemanifest failed ..."); return; } var assetBundles = assetBundleManifest.GetAllAssetBundles(); var listItems = assetBundles.ToList(); listItems.Add(platform); assetBundles = listItems.ToArray(); List <AssetBundleItem> assetBundleItems = new List <AssetBundleItem>(assetBundles.Length); for (int i = 0; i < assetBundles.Length; ++i) { var path = System.IO.Path.Combine(root, assetBundles[i]); var fileMd5 = Function.GetMD5HashFromFile(path); if (string.IsNullOrEmpty(fileMd5)) { continue; } AssetBundleItem assetBundleItem = new AssetBundleItem(); assetBundleItem.key = assetBundles[i]; assetBundleItem.md5 = fileMd5; assetBundleItems.Add(assetBundleItem); Debug.LogFormat("[assetbundle]:{0}\t\t[md5]:[{1}]", assetBundles[i], fileMd5); } var storePath = @"Assets/Resources/Data/";; var bundleMd5List = Scriptablity.Create <AssetBundleList>(storePath, "AssetBundleMd5List"); bundleMd5List.assetBundleItems = assetBundleItems.ToArray(); bundleMd5List.baseAssetBundles = new string[] { platform, "table", }; EditorUtility.SetDirty(bundleMd5List); AssetDatabase.SaveAssets(); assetBundle.Unload(true); }
/** constructor */ public ResultType() { //assetbundle_item this.assetbundle_item = null; //errorstring this.errorstring = null; //responseheader this.responseheader = null; }
AssetBundleItem GetCacheBundle(uint bundleCrc) { AssetBundleItem item = null; if (cacheAssetBundleItemDic.TryGetValue(bundleCrc, out item)) { item.RefCount += 1; return(item); } return(null); }
public void AddItem(string name, AssetBundle assetBundle) { AssetBundleItem item = GetItem(name); if (item != null) { throw new GameFrameworkException("AssetBundle item is already exist."); } item = new AssetBundleItem(name, assetBundle, this); m_Items.Add(item); m_Items.Sort(AssetBundleItemComparer); }
private void AddManifestAssetBundle(string dirPath) { string fileName = GetFolderName(curBuildTarget); string filePath = dirPath + fileName; AssetBundleItem item = new AssetBundleItem(); item.bundleName = fileName; item.bundleFullName = fileName; item.size = UtilTools.getFileSize(filePath); item.md5 = UtilTools.md5file(filePath); string oldMd5 = resourceData.GetMd5ByBundleName(item.bundleName); isChange = oldMd5 != item.md5; abItemList.Add(item); }
/** constructor */ public Main_AssetBundle() { this.is_busy = false; this.is_cancel = false; this.is_shutdown = false; //request this.request_assetbundle_name = null; //result this.result_progress = 0.0f; this.result_errorstring = null; this.result_type = ResultType.None; this.result_assetbundle_item = null; }
/// <summary> /// 异步加载AB /// </summary> /// <typeparam name="T"></typeparam> /// <param name="abName"></param> /// <param name="assetName"></param> /// <param name="cb"></param> /// <returns></returns> IEnumerator LoadAssetAsync <T>(string abName, string assetName, System.Action <T> cb) where T : Object { //AB是否已加载 if (!abLoadedDic.ContainsKey(abName)) {//如果还没加载 //先加载依赖项 foreach (string item in manifest.GetAllDependencies(abName)) { yield return(LoadAssetAsync <T>(item, null, null)); } //加载AB AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(PathHelper.AssetBundlePath + "/" + abName); while (!request.isDone) { yield return(new WaitForSeconds(0.15f)); } AssetBundleItem abItem = new AssetBundleItem(abName, request.assetBundle, manifest.GetAllDependencies(abName)); abLoadedDic.Add(abName, abItem); } else { //AB内存已释放 if (abLoadedDic[abName].V_AssetBundle == null) { //先加载依赖项 foreach (string item in manifest.GetAllDependencies(abName)) { yield return(LoadAssetAsync <T>(item, null, null)); } //加载AB AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(PathHelper.AssetBundlePath + "/" + abName); yield return(request); if (request.assetBundle != null) { abLoadedDic[abName].V_AssetBundle = request.assetBundle; } } } if (!string.IsNullOrEmpty(assetName) && cb != null) { StartCoroutine(abLoadedDic[abName].LoadAssetAsync <T>(assetName, cb)); } }
private void UnLoadAssetBundle(string name) { AssetBundleItem item = null; uint crc = Crc32.GetCrc32(name); if (m_AssetBundleItemDic.TryGetValue(crc, out item) && item != null) { item.Release(); if (item.RefCount <= 0 && item.assetBundle != null) { item.assetBundle.Unload(true); item.Reset(); m_AssetBundleItemPool.Recycle(item); m_AssetBundleItemDic.Remove(crc); } } }
public void UnLoadAssetBundle(string ABName) { uint crc = Crc32.GetCrc32(ABName); if (ABItemDic.ContainsKey(crc)) { AssetBundleItem abItem = ABItemDic[crc]; abItem.refCount--; if (abItem.refCount <= 0 && abItem.assetBundle != null) { abItem.assetBundle.Unload(true); abItem.Reset(); ABItemPool.Recyle(abItem); ABItemDic.Remove(crc); } } }
/// <summary> /// 按资源名 卸载资源 /// </summary> private void UnLoadAssetBundle(string name) { AssetBundleItem item = null; uint crc = CRC32.GetCRC32(name); if (abItemDict.TryGetValue(crc, out item) && item != null) { item.m_RefCount--; if (item.m_RefCount <= 0 && item.m_AssetBundle != null) { item.m_AssetBundle.Unload(true); item.Reset(); abItemClassPool.UnSpawn(item); abItemDict.Remove(crc); } } }
void UnloadAssetBundle(string bundleName) { var bundleCrc = CRC32.GetCRC32(bundleName); AssetBundleItem item = null; if (cacheAssetBundleItemDic.TryGetValue(bundleCrc, out item)) { item.RefCount--; if (item.RefCount <= 0) { item.Bundle.Unload(true); item.Reset(); assetBundleItemPool.Recycle(item); cacheAssetBundleItemDic.Remove(bundleCrc); } } }
/// <summary> /// 卸载 AB /// </summary> /// <param name="name"></param> private void UnLoadAssetBundle(string name) { AssetBundleItem item = null; uint crc = CRC32.GetCRC32(name); if (assetBundleItemDic.TryGetValue(crc, out item) && item != null) { item.RefCount--; if (item.RefCount <= 0 && item.assetBundle != null) // 确保没有被引用了 { item.assetBundle.Unload(true); // 卸载 item.Rest(); // 还原 类对象池 assetBundleItemPool.Recycle(item); assetBundleItemDic.Remove(crc); } } }
private void UnLoadAssetBundle(string abName) { AssetBundleItem item = null; uint crc = CRC32.GetCRC32(abName); if (m_AssetBundleItemDic.TryGetValue(crc, out item) || item != null) { item.RefCount--; if (item.RefCount <= 0 && item.assetBundle != null) { item.assetBundle.Unload(true); item.Rest(); m_AssetBundleItemPool.Recycle(item); m_AssetBundleItemDic.Remove(crc); } } }
public void UnLoadAssetBundle(string name) { AssetBundleItem item = null; string crc = CRC32.GetCRC32(name).ToString(); if (m_AssetBundleItemDic.TryGetValue(crc, out item) && item != null) { item.RefCount--; if (item.RefCount <= 0 && item.assetBundle != null) { item.assetBundle.Unload(true); item.Reset(); m_AssetBundleItemPool.Recycle(item); m_AssetBundleItemDic.Remove(crc); } } }
/// <summary> /// 卸载AB包 /// </summary> /// <param name="name"></param> private void UnloadAssetBundle(string name) { string path = BundleTargetPath + "/" + name; uint crc = Crc32.GetCrc32(path); AssetBundleItem item = null; if (m_AssetBundleItemDic.TryGetValue(crc, out item)) { item.refCount--; if (item.refCount <= 0 && item.assetBundle != null) { item.refCount = 0; item.assetBundle.Unload(true); m_AssetBundleItemPool.Recycle(item); m_AssetBundleItemDic.Remove(crc); } } }
/// <summary> /// 加载AB /// </summary> /// <param name="abName"></param> /// <returns></returns> bool LoadAssetBundle(string abName) { //AB是否已加载 if (!abLoadedDic.ContainsKey(abName)) {//如果还没加载 //先加载依赖项 foreach (string item in manifest.GetAllDependencies(abName)) { if (!LoadAssetBundle(item)) { return(false); } } //加载AB AssetBundle ab = AssetBundle.LoadFromFile(PathHelper.AssetBundlePath + "/" + abName); AssetBundleItem abItem = new AssetBundleItem(abName, ab, manifest.GetAllDependencies(abName)); abLoadedDic.Add(abName, abItem); return(true); } else { //AB内存已释放 if (abLoadedDic[abName].V_AssetBundle == null) { //先加载依赖项 foreach (string item in manifest.GetAllDependencies(abName)) { if (!LoadAssetBundle(item)) { return(false); } } //加载AB AssetBundle ab = AssetBundle.LoadFromFile(PathHelper.AssetBundlePath + "/" + abName); abLoadedDic[abName].V_AssetBundle = ab; return(true); } else { //AB内存未释放 return(true); } } }
/// <summary> /// 根据ab包名卸载ab包 /// </summary> /// <param name="abName"></param> public void UnLoadAssetBundle(string abName) { uint crc = Crc.GetCRC32(abName); AssetBundleItem item = null; if (m_AssetBundleItemDic.TryGetValue(crc, out item) || item != null) { item.refCount--; if (item.refCount <= 0 && item.assetBundle != null) { Debug.Log(abName); //没有引用 item.assetBundle.Unload(true); item.Rest(); m_AssetBundleItemPool.Recycle(item); m_AssetBundleItemDic.Remove(crc); } } }