public static void GenAsset_OneFile(string path) { try { if (!path.EndsWith(".png") || path.EndsWith(".tag")) { Debug.LogError("--->> 目前仅支持.png/.tga"); return; } string fileName = FileUtility.GetNameFromFullPath(path, ""); string genPath = getAssetPath(fileName); string dir = FileUtility.GetFolderFromFullPath(genPath); FileUtility.EnsureDirectory(dir); SprAtlas asset = null; if (File.Exists(genPath)) { File.Delete(genPath); } asset = ScriptableObject.CreateInstance <SprAtlas>(); AssetDatabase.CreateAsset(asset, genPath); asset.file_name = fileName; var all = AssetDatabase.LoadAllAssetsAtPath(path); List <Sprite> sprs = new List <Sprite>(); foreach (var a in all) { var t = a as Texture; if (t != null) { asset.texture = t; } var sp = a as Sprite; if (sp) { sprs.Add(sp); } } asset.sprites = sprs.ToArray(); EditorUtility.SetDirty(asset); //AssetDatabase.SaveAssets(); Debug.Log("<color=yellow>--->></color> 生成asset完成 !!\n" + genPath); } catch (System.Exception ex) { Debug.Log("Gen sprite atlas exception!\t" + ex.Message + "\t" + path); } }
void __SetSprite(object refer_, Image image_, string real_url_, string spriteName, bool nativeSize_) { SprAtlas atlas = m_assetCache.LoadSync(real_url_, refer_) as SprAtlas; if (atlas == null) { //如果找不到资源,已经报错了,这里不需要报 return; } if (string.IsNullOrEmpty(spriteName)) { //传空时,取图集名称 spriteName = atlas.file_name; } Sprite sprite = atlas.GetSprite(spriteName); if (sprite == null) { Log.Error("图集缺少图片:" + real_url_ + ", " + spriteName); return; } image_.sprite = null; image_.sprite = sprite; if (nativeSize_) { image_.SetNativeSize(); } Vector4 border = sprite.border; if (border.x > 0 || border.y > 0 || border.z > 0 || border.w > 0) { //九宫格 image_.type = Image.Type.Sliced; } else { image_.type = Image.Type.Simple; } }
//-------∽-★-∽------∽-★-∽--------∽-★-∽lua相关∽-★-∽--------∽-★-∽------∽-★-∽--------// //生成图集配置 public static bool GenSpriteMsg() { bool ret = true; string msg_path = "Assets/Resources/LuaScript/data/sprite_msg.lua"; if (File.Exists(msg_path)) { File.Delete(msg_path); } Dictionary <string, string> sprName2path = new Dictionary <string, string>(); WalkDir(GEN_PATH_ATLAS, null, (f) => { if (f.EndsWith(".asset")) { SprAtlas atlas = AssetDatabase.LoadAssetAtPath <SprAtlas>(f); string name = FileUtility.GetNameFromFullPath(f, ""); foreach (Sprite sprite in atlas.sprites) { if (sprName2path.ContainsKey(sprite.name)) { ret = false; Debug.LogError(String.Format("图片重复 {0} {1} -> {2}", sprite, sprName2path[sprite.name], atlas.file_name)); break; } else { sprName2path[sprite.name] = atlas.file_name; } } } }); if (!ret) { return(ret); } StringBuilder sb = new StringBuilder(65535); sb.Append("return {\n"); foreach (var kvp in sprName2path) { sb.AppendFormat("[\"{0}\"] = \"{1}\",\n", kvp.Key, kvp.Value); } sb.Append("}\n"); FileStream luaFile = new FileStream(msg_path, FileMode.Create); var bytes = System.Text.Encoding.UTF8.GetBytes(sb.ToString()); luaFile.Write(bytes, 0, bytes.Length); luaFile.Flush(); luaFile.Close(); AssetDatabase.SaveAssets(); Debug.Log("sprite_msg生成完毕: " + msg_path); return(ret); }