/// <summary> /// 保存UI预制 /// 自动添加引用图集依赖 /// </summary> /// <param name="instance"></param> static void SaveUIPrefab(GameObject instance) { string prefabPath = AssetDatabase.GetAssetPath(PrefabUtility.GetCorrespondingObjectFromSource(instance)); if (!IsUIPrefab(prefabPath)) { return; } GameObject go = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(instance) as GameObject; Image[] imgs = go.GetComponentsInChildren <Image>(true); Dictionary <string, SpriteAtlas> saDict = new Dictionary <string, SpriteAtlas>(); List <SpriteAtlas> saList = new List <SpriteAtlas>(); string imgPath; string spriteAtlasPath; SpriteAtlas sa; foreach (Image img in imgs) { imgPath = AssetDatabase.GetAssetPath(img.sprite); if (imgPath.IndexOf("/UIAtlas/") == -1) { continue; } imgPath = imgPath.Substring(0, imgPath.LastIndexOf("/")); spriteAtlasPath = imgPath.Replace("/ArtRes/UIAtlas/", "/BundleRes/UIAtlas/") + ".spriteatlas"; if (!saDict.TryGetValue(spriteAtlasPath, out sa)) { sa = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(spriteAtlasPath); if (sa != null) { saDict.Add(spriteAtlasPath, sa); saList.Add(sa); } else { ToolsHelper.Warning("SpriteAtlas未找到:" + spriteAtlasPath); } } } SpriteAtlasList compAtlas = go.GetComponent <SpriteAtlasList>(); if (saList.Count > 0) { if (compAtlas == null) { compAtlas = go.AddComponent <SpriteAtlasList>(); } compAtlas.AtlasList = saList.ToArray(); } else { if (compAtlas != null) { Component.DestroyImmediate(compAtlas, true); } } PrefabUtility.ResetToPrefabState(instance); }
/// <summary> /// 保存文件 /// </summary> /// <param name="path">保存路径</param> /// <param name="content">文件内容</param> /// <param name="iscover">存在是否进行覆盖,默认true</param> public static void SaveFile(string path, string content, bool iscover = true, bool isLog = true) { FileInfo info = new FileInfo(path); if (!iscover && info.Exists) //不覆盖 { if (isLog) ToolsHelper.Warning($"文件已存在,不进行覆盖操作!! {path}"); return; } CheckCreateDirectory(info.DirectoryName); FileStream fs = new FileStream(path, FileMode.Create); StreamWriter sWriter = new StreamWriter(fs, Encoding.GetEncoding("UTF-8")); sWriter.WriteLine(content); sWriter.Flush(); sWriter.Close(); fs.Close(); Log($"成功生成文件 {path}",false); }