public static void CreateAtlasAsset() { var selection = Selection.objects; string path = string.Empty; StringBuilder sb = new StringBuilder(); foreach (Object s in selection) { if (s is DefaultAsset && (path = AssetDatabase.GetAssetPath(s)) != null && Directory.Exists(path)) { // var import = AssetImporter.GetAtPath(path); var ragName = s.name.ToLower() + "_atlas.asset"; string atlas_path = Path.Combine(path, ragName); sb.Append("Crate atlas Asset :"); sb.Append(ragName); sb.Append("\r\n"); var allchildren = EditorUtils.getAllChildFiles(path, @"\.meta$|\.manifest$|\.DS_Store$|\.u$", null, false); int count = 0; List <int> names = new List <int>(); List <Sprite> allSprites = new List <Sprite>(); foreach (var f in allchildren) { count++; TextureImporter ti = AssetImporter.GetAtPath(f) as TextureImporter; if (ti != null) { Object[] objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(f); foreach (var item in objs) { if (item is Sprite) { names.Add(LuaHelper.StringToHash(item.name)); allSprites.Add((Sprite)item); } } EditorUtility.DisplayProgressBar("Processing...", "生成中... (" + count + " / " + allchildren.Count + ")", count / allchildren.Count); } } EditorUtility.ClearProgressBar(); //生成或者替换资源 AtlasAsset atlas = AssetDatabase.LoadAssetAtPath <AtlasAsset>(atlas_path); if (atlas == null) { atlas = AtlasAsset.CreateInstance <AtlasAsset>(); AssetDatabase.CreateAsset(atlas, atlas_path); } atlas.names = names; atlas.sprites = allSprites; EditorUtility.SetDirty(atlas); var import = AssetImporter.GetAtPath(atlas_path); import.assetBundleName = Path.GetFileNameWithoutExtension(ragName) + Common.CHECK_ASSETBUNDLE_SUFFIX; sb.AppendFormat("build {0} success count = {1} ", ragName, names.Count); AssetDatabase.SaveAssets(); } } sb.AppendLine("\r\nall completed"); Debug.Log(sb.ToString()); }
public static void GenerateAssetBundlesMd5Mapping(string[] allAssets) { string info = "Generate AssetBundles Md5Mapping "; EditorUtility.DisplayProgressBar("GenerateAssetBundlesMd5Mapping", info, 0); string speciallyPath = "Assets/Config/Lan/"; string luaPath = "Assets/Lua/"; AssetImporter import = null; float i = 0; float allLen = allAssets.Length; string name = ""; string nameMd5 = ""; //name mapping StringBuilder nameSb = new StringBuilder(); //asset map StringBuilder sb = new StringBuilder(); sb.AppendLine("return {"); foreach (string path in allAssets) { import = AssetImporter.GetAtPath(path); string line = string.Empty; if (import != null && string.IsNullOrEmpty(import.assetBundleName) == false) { string abName = import.assetBundleName; name = CUtils.GetAssetName(path).ToLower(); if (!string.IsNullOrEmpty(import.assetBundleVariant)) { abName = import.assetBundleName + "." + import.assetBundleVariant; // line = "{\"" + import.assetBundleName + "\" = { size = \"" + name + "\", path = \"" + path + "\"}},"; } line = "[\"" + abName + "\"] = { size = " + GetAssetbundleSize(abName) + ", path = \"" + path + "\"},"; sb.AppendLine(line); nameSb.AppendFormat("{0}={1}\r\n", CUtils.GetRightFileName(name), name); if (name.Contains(" ")) { Debug.LogWarning(name + " contains space"); } } else if (import != null && path.Contains(speciallyPath)) { name = CUtils.GetAssetName(path).ToLower(); string md5name = CUtils.GetRightFileName(name) + Common.CHECK_ASSETBUNDLE_SUFFIX; line = "[\"" + md5name + "\"] = { size = " + GetAssetbundleSize(md5name) + ", path = \"" + path + "\"},"; sb.AppendLine(line); nameSb.AppendFormat("{0}={1}\r\n", md5name, name); } else if (import != null && path.Contains(luaPath)) { string luaname = path.Replace(luaPath, "").Replace("\\", ".").Replace("/", "."); string luacname = luaname.Replace(".lua", "").Replace(".", "+"); string luaMd5Name = CUtils.GetRightFileName(luacname); line = "[\"" + luaMd5Name + "\"] = { size = " + GetAssetbundleSize(luaMd5Name + ".bytes") + ", path = \"" + path + "\"},"; sb.AppendLine(line); nameSb.AppendFormat("{0}={1}\r\n", luaMd5Name, luaname); } EditorUtility.DisplayProgressBar("Generate AssetBundles Md5Mapping", info + "=>" + i.ToString() + "/" + allLen.ToString(), i / allLen); i++; } string[] special = new string[] { CUtils.platform, Common.CONFIG_CSV_NAME, Common.CRC32_FILELIST_NAME, Common.CRC32_VER_FILENAME }; foreach (string p in special) { name = EditorUtils.GetAssetBundleName(p); nameMd5 = CUtils.GetRightFileName(name); string line = "[\"" + nameMd5 + "\"] ={ size = 0, path = \"" + p + "\" },"; sb.AppendLine(line); nameSb.AppendFormat("{0}={1}\r\n", CUtils.GetRightFileName(name), name); } sb.AppendLine("}"); string tmpPath = Path.Combine(Application.dataPath, EditorUtils.TmpPath); EditorUtils.CheckDirectory(tmpPath); EditorUtility.DisplayProgressBar("Generate AssetBundles Md5Mapping", "write file to Assets/" + EditorUtils.TmpPath + "Md5Mapping.txt", 0.99f); string outPath = Path.Combine(tmpPath, "md5_asset_mapping.txt"); Debug.Log("write to path=" + outPath); using (StreamWriter sr = new StreamWriter(outPath, false)) { sr.Write(sb.ToString()); } outPath = Path.Combine(tmpPath, "md5_name_mapping.txt"); Debug.Log("write to path=" + outPath); using (StreamWriter sr = new StreamWriter(outPath, false)) { sr.Write(nameSb.ToString()); } EditorUtility.ClearProgressBar(); Debug.Log(info + " Complete! Assets/" + EditorUtils.TmpPath + "md5_asset_mapping.txt"); }
public static void CreateAtlasAsset() { var selection = Selection.objects; string path = string.Empty; StringBuilder sb = new StringBuilder(); string tagName = string.Empty; foreach (Object s in selection) { if (s is DefaultAsset && (path = AssetDatabase.GetAssetPath(s)) != null && Directory.Exists(path)) { var ragName = s.name.ToLower() + "_atlas.spriteatlas"; string atlas_path = Path.Combine(path, ragName); tagName = s.name.ToLower() + "_atlas"; sb.Append("Crate atlas Asset :"); sb.Append(ragName); sb.Append("\r\n"); var allchildren = EditorUtils.getAllChildFiles(path, @"\.meta$|\.manifest$|\.DS_Store$|\.u$", null, false); int count = 0; List <int> names = new List <int>(); List <Sprite> allSprites = new List <Sprite>(); foreach (var f in allchildren) { count++; TextureImporter ti = AssetImporter.GetAtPath(f) as TextureImporter; if (ti != null) { if (ti.textureType != TextureImporterType.Sprite) { ti.textureType = TextureImporterType.Sprite; } Object[] objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(f); foreach (var item in objs) { if (item is Sprite) { sb.AppendLine(item.name); names.Add(UnityEngine.Animator.StringToHash(item.name)); allSprites.Add((Sprite)item); } } // ti.spritePackingTag = tagName; // ti.assetBundleName = tagName + Common.CHECK_ASSETBUNDLE_SUFFIX; EditorUtility.DisplayProgressBar("Processing...", "生成中... (" + count + " / " + allchildren.Count + ")", count / allchildren.Count); } else { Debug.LogWarningFormat("{0} is not Texture ", f); } } EditorUtility.ClearProgressBar(); //生成或者替换资源 var atlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlas_path); if (atlas == null) { atlas = new SpriteAtlas(); AssetDatabase.CreateAsset(atlas, atlas_path); SpriteAtlasPackingSettings packSet = new SpriteAtlasPackingSettings() { blockOffset = 1, enableRotation = false, enableTightPacking = false, padding = 2, }; atlas.SetPackingSettings(packSet); SpriteAtlasTextureSettings textureSet = new SpriteAtlasTextureSettings() { readable = false, generateMipMaps = false, sRGB = false, filterMode = FilterMode.Bilinear, }; atlas.SetTextureSettings(textureSet); } SpriteAtlasExtensions.Add(atlas, allSprites.ToArray()); EditorUtility.SetDirty(atlas); var groupAssets = new List <string>(); groupAssets.Add(atlas_path); var atlasGroup = AASEditorUtility.FindGroup(tagName, AASEditorUtility.DefaltGroupSchema[0]); AASEditorUtility.SetGroupAddress(AASEditorUtility.LoadAASSetting(), atlasGroup, groupAssets); sb.AppendFormat("build {0} success count = {1} ", ragName, names.Count); AssetDatabase.SaveAssets(); } } sb.AppendLine("\r\nall completed"); Debug.Log(sb.ToString()); EditorUtils.WriteToTmpFile(tagName + ".txt", sb.ToString()); }
/// <summary> /// 设置assetbundleName /// </summary> /// <param name="s"></param> public static void SetAssetBundleName(Object s, bool replace = false, bool variant = false, string variantName = null) { string abPath = AssetDatabase.GetAssetPath(s); AssetImporter import = AssetImporter.GetAtPath(abPath); string folder = EditorUtils.GetLabelsByPath(abPath); string objName = s.name.ToLower(); if (replace) { objName = HugulaEditorSetting.instance.GetAssetBundleNameByReplaceIgnore(objName); // Debug.LogFormat ("{0} replace {1}", s.name, objName); } bool isScene = abPath.EndsWith(".unity"); string name = CUtils.GetRightFileName(objName); var suffix = Common.CHECK_ASSETBUNDLE_SUFFIX; // if (variant) // variant // { // suffix = ""; // } if (string.IsNullOrEmpty(folder)) { import.assetBundleName = name + suffix; } else { import.assetBundleName = string.Format("{0}/{1}{2}", folder, name, suffix); } if (variant) { string m_variant; if (abPath.IndexOf("_high") == 0) { m_variant = "high"; } else if (abPath.IndexOf("_medium") == 0) { m_variant = "medium"; } else if (abPath.IndexOf("_low") == 0) { m_variant = "low"; } else { DirectoryInfo filePath = new DirectoryInfo(abPath); m_variant = variantName != null ? variantName : filePath.Parent.Name.ToLower(); } import.assetBundleVariant = m_variant; HugulaSetting.instance.AddVariant(m_variant); if (m_variant.Equals(Common.ASSETBUNDLE_SUFFIX)) { Debug.LogWarningFormat("{0} variant folder name Equals {1}", abPath, Common.ASSETBUNDLE_SUFFIX); } if (m_variant == Common.DOT_BYTES.Replace(".", "")) { Debug.LogWarningFormat("{0} variant folder name Equals {1}", abPath, Common.DOT_BYTES); } } if (s.name.Contains(" ")) { Debug.LogWarning(s.name + " contains space"); } Debug.Log(import.assetBundleName + ",variant=" + import.assetBundleVariant); // if (s is GameObject) // { // GameObject tar = s as GameObject; // if (tar.transform.parent == null) // { // ReferenceCount refe = LuaHelper.AddComponent(tar, typeof(ReferenceCount)) as ReferenceCount; // if (refe != null) // { // if (string.IsNullOrEmpty(import.assetBundleVariant)) // refe.assetbundle = import.assetBundleName; // else // refe.assetbundle = import.assetBundleName + "." + import.assetBundleVariant; // EditorUtility.SetDirty(s); // } // } // } // else if (isScene) //如果是场景需要添加引用计数脚本 // { //UnityEngine.SceneAsset // var sce = s; // as SceneAsset; // Debug.Log(sce); // AssetDatabase.OpenAsset(sce); // GameObject gobj = GameObject.Find(sce.name); // if (gobj == null) gobj = new GameObject(sce.name); // ReferenceCount refe = LuaHelper.AddComponent(gobj, typeof(ReferenceCount)) as ReferenceCount; // if (refe != null) // { // if (string.IsNullOrEmpty(import.assetBundleVariant)) // refe.assetbundle = import.assetBundleName; // else // refe.assetbundle = import.assetBundleName + "." + import.assetBundleVariant; // EditorUtility.SetDirty(sce); // } // var refers = GameObject.FindObjectsOfType<ReferenceCount>(); // foreach (var rf in refers) // { // if (rf != refe) // { // Debug.LogWarningFormat("you should not add ReferenceCount in {0}", EditorUtils.GetGameObjectPathInScene(rf.transform, string.Empty)); // } // } // } }
public static void GenerateAllAtlasMapping() { StringBuilder sb = new StringBuilder(); List <int> allSprites = new List <int>(); List <string> atlasNames = new List <string>(); var files = AssetDatabase.GetAllAssetPaths().Where(p => p.EndsWith(".spriteatlas") ).ToArray(); for (int i = 0; i < files.Length; i++) { var o = AssetDatabase.LoadAssetAtPath <UnityEngine.U2D.SpriteAtlas>(files[i]); var ti = AssetImporter.GetAtPath(files[i]); var key = System.IO.Path.GetFileNameWithoutExtension(files[i]); if (o != null) { // var assetBundleName = ti.assetBundleName; EditorUtility.DisplayProgressBar("Processing...", "生成中... (" + i + " / " + files.Length + ")", (float)i / (float)files.Length); var sps = new UnityEngine.Sprite[o.spriteCount]; int len = o.GetSprites(sps); for (int j = 0; j < len; j++) { string name = sps[j].name.Replace("(Clone)", ""); int id = UnityEngine.Animator.StringToHash(name); allSprites.Add(id); atlasNames.Add(key); sb.AppendFormat("{0}({1}) {2}\r\n", name, id, key); } } } EditorUtility.ClearProgressBar(); string mapping_root_name = Hugula.Atlas.AtlasManager.ATLAS_MAPPING_ROOT_NAME; string atlas_path = Path.Combine(AtlasConfig.ATLAS_MAPPING_ROOT, mapping_root_name + ".asset"); //生成或者替换资源 var atlas = AssetDatabase.LoadAssetAtPath <MappingAsset>(atlas_path); if (atlas == null) { atlas = MappingAsset.CreateInstance <MappingAsset>(); AssetDatabase.CreateAsset(atlas, atlas_path); } atlas.names = allSprites.ToArray(); atlas.keys = atlasNames.ToArray(); EditorUtility.SetDirty(atlas); //aas var groupAssets = new List <string>(); groupAssets.Add(atlas_path); var atlasGroup = AASEditorUtility.FindGroup(mapping_root_name, AASEditorUtility.DefaltGroupSchema[0]); AASEditorUtility.SetGroupAddress(AASEditorUtility.LoadAASSetting(), atlasGroup, groupAssets); AssetDatabase.SaveAssets(); Debug.LogFormat(" save {0} count = {1} ", atlas_path, atlasNames.Count); EditorUtils.WriteToTmpFile(mapping_root_name + ".txt", sb.ToString()); }