public bool AddImage(Texture2D img, string name, Borders borders) { AtlasNode node = _root.Insert(img, borders); if (node == null) { return(false); } _rectangleMap.Add(name, node.Rectangle); try { Image.SetPixels((int)node.Rectangle.x, (int)node.Rectangle.y, (int)node.Rectangle.width, (int)node.Rectangle.height, img.GetPixels()); } catch (Exception exceptionMessage) { Debug.Log(exceptionMessage); InstallerAtlasProperties.SetReadable(img); Image.SetPixels((int)node.Rectangle.x, (int)node.Rectangle.y, (int)node.Rectangle.width, (int)node.Rectangle.height, img.GetPixels()); } if (borders.IsRepeat) { Color color = new Color(); for (int b = 0; b < borders.RepeatSize; b++) { for (int i = 0; i < img.width; i++) { color = img.GetPixel(i, 0); Image.SetPixel(i + (int)node.Rectangle.x, (int)node.Rectangle.y - b - 1, color); color = img.GetPixel(i, img.height - 1); Image.SetPixel(i + (int)node.Rectangle.x, (int)node.Rectangle.y + (int)node.Rectangle.height + b, color); } } for (int b = 0; b < borders.RepeatSize; b++) { for (int i = 0; i < img.height; i++) { color = img.GetPixel(0, i); Image.SetPixel((int)node.Rectangle.x - b - 1, (int)node.Rectangle.y + i, color); color = img.GetPixel(img.width - 1, i); Image.SetPixel((int)node.Rectangle.x + (int)node.Rectangle.width + b, (int)node.Rectangle.y + i, color); } } } if (Image == null) { Debug.LogError("Image null!"); } return(true); }
// generate sprite atlas from texture atlas private void GenerateSprites(string path, SpriteAlignment alignment) { string atlasPath = path.Replace(".png", ".txt"); atlasPath = @"Assets\" + Regex.Split(atlasPath, @"Assets")[1].Remove(0, 1); TextAsset atlas = (TextAsset)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(TextAsset)); path = @"Assets\" + Regex.Split(path, @"Assets")[1].Remove(0, 1); Texture2D image = (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)); List <SpriteMetaData> sprites = SpritePacker.Parse(atlas, _height, (int)alignment); TextureImporter textureImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(image)) as TextureImporter; textureImporter.spritesheet = sprites.ToArray(); textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = SpriteImportMode.Multiple; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); InstallerAtlasProperties.SetDefaultSettings(image); }