static void Init() { SpritePacker window = (SpritePacker)EditorWindow.GetWindow(typeof(SpritePacker)); window.Show(); window.Initialize(); }
public void TestMethod1() { var packer = new SpritePacker(); var result = packer.Pack(new[] { new Sprite("a", 10, 4), new Sprite("b", 4, 4), new Sprite("c", 4, 4), new Sprite("d", 3, 3) }); Console.WriteLine($"Result = {result.Width}x{result.Height}"); foreach (var sprite in result.Sprites) { Console.WriteLine($"{sprite.Sprite.Reference} = {sprite.X}x{sprite.Y} {sprite.Sprite.Width}x{sprite.Sprite.Height}"); } }
private static bool GeneratePacker(List <Sprite> sprites, string path) { var packer = AssetDatabase.LoadAssetAtPath <SpritePacker>(path); if (packer == null) { packer = SpritePacker.CreateInstance <SpritePacker>(); UnityEditor.AssetDatabase.CreateAsset(packer, path); } //no eidtable packer.hideFlags = HideFlags.NotEditable; if (packer.spriteList == null) { packer.spriteList = new List <Sprite>(); } packer.spriteList.Clear(); packer.spriteList.AddRange(sprites); return(false); }
/// <summary> /// Loads source sprites referenced in declaration input and packs sprites into a sprite sheet. /// </summary> /// <param name="declaration">Sprite sheet delcaration input.</param> /// <param name="context">Context of the process routine.</param> /// <returns>Sprite sheet content.</returns> public override SpriteSheetContent Process(SpriteSheetDeclaration declaration, ContentProcessorContext context) { if (declaration == null) { throw new ArgumentNullException(nameof(declaration)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } // Collect all texture file paths. // var texturePaths = new HashSet <string>(); foreach (string pathPattern in declaration.PathPatterns) { foreach (string filePath in Glob.Files(Directory.GetCurrentDirectory(), pathPattern)) { texturePaths.Add(filePath); } } // Limit the packed texture based on the target graphics profile. // int maximumDimension = context.TargetProfile == GraphicsProfile.HiDef ? 4096 : 2048; PackedTexture packedTexture = SpritePacker.Pack( configuration: new PackConfiguration(maximumDimension, maximumDimension, this.IsPowerOfTwo, this.IsSquare, this.Padding), spritePaths: texturePaths.ToArray()); // Ensure our output directory exists. // if (!Directory.Exists(context.OutputDirectory)) { Directory.CreateDirectory(context.OutputDirectory); } // Write the packed texture to the output location. // string outputPath = Path.Combine(context.OutputDirectory, $"{declaration.Name}.png"); using (var stream = new FileStream(outputPath, FileMode.Create)) { packedTexture.Texture.Save(stream, ImageFormat.Png); } context.AddOutputFile(outputPath); // Finalize the content to serialize from this processor. // return(new SpriteSheetContent { Name = declaration.Name, SpriteNamesToIndexMapping = packedTexture.SpriteNamesToIndexMapping, SpriteRectangles = packedTexture.SpriteRectangles, Texture = this.BuildTexture($"{declaration.Name}Texture", outputPath, context), }); }