public static void ChargeTextures(PictureExportInfo pictureInfo, GroupNode1 groupnode) { //重新加载 var atlaspath = pictureInfo.exportPath + "/" + pictureInfo.atlasName; Sprite[] fileSprites = AssetDatabase.LoadAllAssetsAtPath(atlaspath).OfType <Sprite>().ToArray(); Texture2D[] fileTextures = LoadAllObjectFromDir <Texture2D>(pictureInfo.exportPath); // AssetDatabase.LoadAllAssetsAtPath(pictureInfo.exportPath).OfType<Texture2D>().ToArray(); Sprite[] fileSingleSprites = LoadAllObjectFromDir <Sprite>(pictureInfo.exportPath); // AssetDatabase.LoadAllAssetsAtPath(pictureInfo.exportPath).OfType<Sprite>().ToArray(); var pictureData = new List <ImgNode>(); groupnode.GetImgNodes(pictureData); foreach (var item in pictureData) { switch (item.type) { case ImgType.Image: item.sprite = Array.Find(fileSingleSprites, x => x.name == item.TextureName); break; case ImgType.AtlasImage: item.sprite = Array.Find(fileSprites, x => x.name == item.TextureName); break; case ImgType.Texture: item.texture = Array.Find(fileTextures, x => x.name == item.TextureName); break; default: break; } } }
/// <summary> /// 将一组图片保存为atlas /// </summary> /// <param name="textureArray"></param> /// <param name="pictureInfo"></param> /// <param name="atlasName"></param> /// <returns></returns> public static void SaveToAtlas(Texture2D[] textureArray, PictureExportInfo pictureInfo) { if (textureArray.Length == 0) { return; } // The output of PackTextures returns a Rect array from which we can create our sprites Rect[] rects; Texture2D atlas = new Texture2D(pictureInfo.atlassize, pictureInfo.atlassize); rects = atlas.PackTextures(textureArray, 2, pictureInfo.atlassize); List <SpriteMetaData> Sprites = new List <SpriteMetaData>(); // For each rect in the Rect Array create the sprite and assign to the SpriteMetaData for (int i = 0; i < rects.Length; i++) { // add the name and rectangle to the dictionary SpriteMetaData smd = new SpriteMetaData(); smd.name = textureArray[i].name; smd.rect = new Rect(rects[i].xMin * atlas.width, rects[i].yMin * atlas.height, rects[i].width * atlas.width, rects[i].height * atlas.height); smd.pivot = new Vector2(0.5f, 0.5f); // Center is default otherwise layers will be misaligned smd.alignment = (int)SpriteAlignment.Center; Sprites.Add(smd); } // Need to load the image first byte[] buf = atlas.EncodeToPNG(); var atlaspath = pictureInfo.exportPath + "/" + pictureInfo.atlasName; File.WriteAllBytes(Path.GetFullPath(atlaspath), buf); AssetDatabase.Refresh(); // Get our texture that we loaded TextureImporter textureImporter = AssetImporter.GetAtPath(atlaspath) as TextureImporter; // Make sure the size is the same as our atlas then create the spritesheet textureImporter.maxTextureSize = pictureInfo.atlassize; textureImporter.spritesheet = Sprites.ToArray(); textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Multiple; textureImporter.spritePivot = new Vector2(0.5f, 0.5f); textureImporter.spritePixelsPerUnit = pictureInfo.pixelsToUnitSize; AssetDatabase.ImportAsset(atlaspath, ImportAssetOptions.ForceUpdate); foreach (Texture2D tex in textureArray) { UnityEngine.Object.DestroyImmediate(tex); } }
/// <summary> /// 将图片分别保存到本地 /// </summary> /// <param name="imgType"></param> /// <param name="textureArray"></param> /// <param name="pictureInfo"></param> public static void SaveToTextures(ImgType imgType, Texture2D[] textureArray, PictureExportInfo pictureInfo) { foreach (var texture in textureArray) { byte[] buf = texture.EncodeToPNG(); var atlaspath = pictureInfo.exportPath + "/" + string.Format(pictureInfo.picNameTemp, texture.name); File.WriteAllBytes(Path.GetFullPath(atlaspath), buf); AssetDatabase.Refresh(); // Get our texture that we loaded TextureImporter textureImporter = AssetImporter.GetAtPath(atlaspath) as TextureImporter; // Make sure the size is the same as our atlas then create the spritesheet textureImporter.maxTextureSize = pictureInfo.atlassize; switch (imgType) { case ImgType.Image: textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Single; textureImporter.spritePivot = new Vector2(0.5f, 0.5f); textureImporter.spritePixelsPerUnit = pictureInfo.pixelsToUnitSize; break; case ImgType.Texture: textureImporter.textureType = TextureImporterType.Image; break; default: break; } AssetDatabase.ImportAsset(atlaspath, ImportAssetOptions.ForceUpdate); } foreach (Texture2D tex in textureArray) { UnityEngine.Object.DestroyImmediate(tex); } }
public static void InitPsdExportEnvrioment(PictureExportInfo pictureInfo0, RouleObject rouleObj0, Vector2 rootSize0) { pictureInfo = pictureInfo0; rouleObj = rouleObj0; rootSize = rootSize0; }