static bool RemoveUnusedAssetBundles(BundleInfoAsset rsd) { HashSet <string> unusedbundles = new HashSet <string>(); var a = BundleInfoManager.GetAsset(); if (a) { foreach (var v in a.BundleInfos) { unusedbundles.Add(v.name); } } foreach (var v in rsd.BundleInfos) { unusedbundles.Remove(v.name); } int i = 0; foreach (var name in unusedbundles) { i++; if (EditorUtility.DisplayCancelableProgressBar(string.Format("remove unused {0}/{1}", i, unusedbundles.Count), name, (float)i / (float)unusedbundles.Count)) { return(false); } AssetDatabase.RemoveAssetBundleName(name, true); } return(true); }
public static void LoadAsync(MonoBehaviour dependObject, string path, System.Type type, System.Action <Object> callback) { // Debug.Log("LoadAsset Aysnc:" + path); if (!patchFiles.ContainsKey(path.ToLower())) { string fullPath = path.StartsWith("Assets/") ? path : ("Assets/" + ResourceBundle.RESOURCES_DIR + "/" + AddFileExtension(path)); Statistics.AddAsset(fullPath); #if UNITY_EDITOR if (AssetManager.SimulateMode) { GetLoader().StartCoroutine(SimulateLoadThread(path, type, callback, dependObject)); return; } #endif string ab = BundleInfoManager.GetBundleNameWithFullPath(fullPath); if (!string.IsNullOrEmpty(ab)) { AssetManager.LoadAsync(GetLoader(), fullPath, type, callback); return; } } GetLoader().StartCoroutine(LoadThread(path, type, callback, dependObject)); }
static long MatchFileInfo(string path, ref List <FileInfo> olist, ref List <FileInfo> nlist, ref Dictionary <string, List <string> > buildin, ref HashSet <string> named, ref long csize, HashSet <string> obs = null) { if (buildin != null) { foreach (var b in buildin) { if (path.IndexOf(b.Key) >= 0) { b.Value.Add(path); return(0); } } } if (named.Contains(path)) { return(0); } try { string fullpath = Path.Combine(Application.dataPath.Replace("Assets", ""), path); FileInfo fs = new FileInfo(fullpath); if (!fs.Exists) { return(0); } string nfp = GetBundlesInfoName(path); var bi = BundleInfoManager.GetBundleInfoWithFullPath(nfp); if (bi == null) { nlist.Add(fs); } else if (obs != null && Path.GetDirectoryName(bi.name) != CommonAssets) { nlist.Add(fs); } else if (obs == null && Path.GetDirectoryName(bi.name) == CommonAssets) { nlist.Add(fs); } else { olist.Add(fs); } named.Add(path); long fsize = GetFileSize(path, fs.Length); csize += fsize; return(fsize); }catch (Exception ex) { Debug.Log(ex); } return(0); }
public static void NamedAssetBundleName() { int count = 0; long csize = 0; BundleInfoAsset rsd = ScriptableObject.CreateInstance <BundleInfoAsset>(); rsd.BundleInfos = new List <BundleInfo>(); { BundleInfo bi = new BundleInfo(BundleInfoManager.AssetBundleName); bi.files.Add("Assets/" + RootDir + "/" + BundleInfoManager.AssetBundleName + "/" + BundleInfoManager.AssetName); rsd.BundleInfos.Add(bi); } BundleInfoManager.InitEditor(); AssetDatabase.StartAssetEditing(); bool completed = false; do { if (null == DependedCount(Application.dataPath + "/" + RootDir, ref rsd.BundleInfos, ref count, ref csize)) { break; } Debug.LogFormat("bundle count {0}, file count {1}, files size {2} MB", rsd.BundleInfos.Count, count, csize / 1024 / 1024); completed = true; } while (false); if (completed) { RemoveAssetFromBundle(rsd); RemoveUnusedAssetBundles(rsd); CreateBundleInfoAsset(rsd); RemoveUnusedBundles(rsd); AssetDatabase.RemoveUnusedAssetBundleNames(); } Debug.Log(completed ? "completed" : "use canceled"); AssetDatabase.StopAssetEditing(); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); }
static void RemoveAssetFromBundle(BundleInfoAsset rsd) { BundleInfoAsset o = BundleInfoManager.GetAsset(); if (o == null) { return; } HashSet <string> oas = new HashSet <string>(); foreach (var b in o.BundleInfos) { foreach (var a in b.files) { oas.Add(a); } } int c = 0; int cc = oas.Count; foreach (var b in rsd.BundleInfos) { foreach (var a in b.files) { oas.Remove(a); c++; } } Debug.Log("remove asset count : " + oas.Count + ", n count : " + c + ", o count : " + cc); int i = 0; foreach (var a in oas) { ++i; EditorUtility.DisplayProgressBar(string.Format("remove unused asset {0}/{1}", i, oas.Count), a, (float)i / (float)oas.Count); SetAssetBundleName(a, ""); } }
static bool ReBuildAssetBundles(string directory, List <FileInfo> ofiles, List <FileInfo> nfiles, ref List <BundleInfo> bundles, ref int count, bool igoresize = false) { List <BundleInfo> oldlbds = new List <BundleInfo>(); ofiles.Sort(new FileComparer()); nfiles.Sort(new FileComparer()); directory = directory == CommonAssets ? CommonAssets : DataAssets; for (int i = 0; i < ofiles.Count; ++i) { var f = ofiles[i]; string filePath = f.FullName.Replace('\\', '/').Replace(Application.dataPath, "Assets"); string nfp = GetBundlesInfoName(filePath);// filePath.Replace("Assets/" + RootDir + "/", "").ToLower(); if (EditorUtility.DisplayCancelableProgressBar(string.Format("name {0}/{1}", i, ofiles.Count), filePath, (float)i / (float)ofiles.Count)) { return(false); } var bdi = BundleInfoManager.GetBundleInfoWithFullPath(nfp); var nbdi = bundles.Find((info) => { return(info.name == bdi.name); }); if (nbdi == null) { nbdi = new BundleInfo(bdi.name); bundles.Add(nbdi); } if (!oldlbds.Contains(nbdi)) { oldlbds.Add(nbdi); } count++; nbdi.files.Add(nfp); nbdi.size += f.Length; SetAssetBundleName(filePath, nbdi.name); } BundleInfo cbdi = null; // find min bundle, ready for insert for (int i = 0; i < oldlbds.Count; ++i) { var v = oldlbds[i]; if (cbdi != null) { if (v.size < cbdi.size) { cbdi = v; } continue; } if (v.size < BundleMinSize) { cbdi = v; } } for (int i = 0; i < nfiles.Count; ++i) { var f = nfiles[i]; string filePath = f.FullName.Replace('\\', '/').Replace(Application.dataPath, "Assets"); if (EditorUtility.DisplayCancelableProgressBar(string.Format("name {0} {1}/{2}", directory, i, nfiles.Count), filePath, (float)i / (float)nfiles.Count)) { return(false); } string nfp = GetBundlesInfoName(filePath); long size = GetFileSize(filePath, f.Length); if (cbdi == null) { cbdi = new BundleInfo(directory + "/" + BundleInfoManager.GetEmptyBundleName()); bundles.Add(cbdi); } if (size > BundleMinSize * 1.5 && !igoresize) { var nbdi = new BundleInfo(directory + "/" + BundleInfoManager.GetEmptyBundleName()); bundles.Add(nbdi); nbdi.files.Add(nfp); SetAssetBundleName(filePath, nbdi.name); Debug.LogFormat("file '{0}' is large {1}", filePath, size); } else { cbdi.files.Add(nfp); cbdi.size += f.Length; SetAssetBundleName(filePath, cbdi.name); if (cbdi.size > BundleMinSize && !igoresize) { cbdi = null; } } count++; } return(true); }