public override void OnImportAsset(AssetImportContext ctx) { // Load the raw file. var file = PAFile.Load(ctx.assetPath); var texture = new Texture2D(64, 64, TextureFormat.RGBA32, false); texture.filterMode = FilterMode.Point; texture.name = "atlas"; var textures = file.frames.Select(f => file.RenderFrame(f)).ToArray(); var rects = texture.PackTextures(textures, 1).Select(r => new Rect(r.xMin * texture.width, r.yMin * texture.height, r.width * texture.width, r.height * texture.height)).ToArray(); texture.Apply(); // TODO: look for duplicates by checking imageContentsHash for all textures var sprites = new Dictionary <string, Sprite>(); for (int frameIndex = 0; frameIndex < file.frames.Count; frameIndex++) { var frame = file.frames[frameIndex]; var sprite = Sprite.Create(texture, rects[frameIndex], new Vector2(0.5f, 0.5f)); sprite.name = $"{frame.animation.name}.{frame.order:D03}"; ctx.AddObjectToAsset(frame.id, sprite); sprites[frame.id] = sprite; } ctx.AddObjectToAsset("_atlas", texture); var pixelArt = PixelArt.Create( file.animations.Select(a => new PixelArtAnimation( a.name, file.frames.Where(f => f.animation == a).OrderBy(f => f.order).Select(f => new PixelArtAnimationFrame { sprite = sprites[f.id] }).ToArray() )).ToArray(), defaultAnimationIndex); var defaultFrame = file.frames.Where(f => f.animation == file.animations[defaultAnimationIndex]).OrderBy(f => f.order).First(); #if false var go = new GameObject(); var sr = go.AddComponent <SpriteRenderer>(); sr.sprite = sprites[defaultFrame.id];; go.name = "Prefab"; var animator = go.AddComponent <PixelArtAnimator>(); animator.PixelArt = pixelArt; animator.layers = new SpriteRenderer[] { sr }; ctx.AddObjectToAsset("prefab", go, PAUtils.LoadImage("PixelArtIcon.psd")); #endif pixelArt.name = "PixelArt"; ctx.AddObjectToAsset("pixelArt", pixelArt, PAUtils.LoadImage("PixelArtIcon.psd")); //ctx.AddObjectToAsset("main", pixelArt, file.RenderFrame(file.frames[0])); ctx.SetMainObject(pixelArt); }
public override void OnEnable() { base.OnEnable(); layerMode = serializedObject.FindProperty("layerMode"); defaultAnimationIndex = serializedObject.FindProperty("defaultAnimationIndex"); _file = PAFile.Load((target as PixelArtImporter).assetPath); _animationNames = _file.animations.Select(a => a.name).ToArray(); }
private static void CreatePixelArt(int width, int height) { // Generate a unique filename for the new artwork var filename = Path.Combine( Application.dataPath, AssetDatabase.GenerateUniqueAssetPath($"{PAUtils.GetSelectedPathOrFallback()}/New PixelArt.pixelart").Substring(7)); // Create an empty file var file = new PAFile(); file.width = width; file.height = height; file.AddFrame(file.AddAnimation("New Animation")); file.AddLayer(); file.Save(filename); AssetDatabase.Refresh(); }
public PAFrame(PAFile file) { File = file; }
/// <summary> /// Load a pixel edtior file from the given filename /// </summary> public static PAFile Load(string filename) { var file = new PAFile { name = Path.GetFileNameWithoutExtension(filename) }; using (var reader = new BinaryReader(File.OpenRead(filename))) { file.version = reader.ReadInt32(); file.width = reader.ReadInt32(); file.height = reader.ReadInt32(); var textureSize = 4 * file.width * file.height; // Read the layers var layerCount = reader.ReadInt32(); for (var layerIndex = 0; layerIndex < layerCount; layerIndex++) { var layer = new PALayer(file); layer.id = reader.ReadString(); layer.name = reader.ReadString(); layer.opacity = reader.ReadSingle(); layer.order = reader.ReadInt32(); layer.visible = reader.ReadBoolean(); file.layers.Add(layer); } // Read the animations var animationCount = reader.ReadInt32(); for (var animationIndex = 0; animationIndex < animationCount; animationIndex++) { var animation = new PAAnimation(); animation.id = reader.ReadString(); animation.name = reader.ReadString(); if (file.version < 3) { animation.fps = 10; } else { animation.fps = reader.ReadInt32(); } file.animations.Add(animation); } // Read the frames var frameCount = reader.ReadInt32(); for (var frameIndex = 0; frameIndex < frameCount; frameIndex++) { var frame = new PAFrame(file); frame.id = reader.ReadString(); frame.animation = file.FindAnimation(reader.ReadString()); frame.order = reader.ReadInt32(); file.frames.Add(frame); } // Read the textures var imageCount = reader.ReadInt32(); for (var imageIndex = 0; imageIndex < imageCount; imageIndex++) { var image = new PAImage(); image.frame = file.FindFrame(reader.ReadString()); image.layer = file.FindLayer(reader.ReadString()); if (reader.ReadBoolean()) { image.texture = new Texture2D(file.width, file.height, TextureFormat.RGBA32, false); image.texture.LoadRawTextureData(reader.ReadBytes(textureSize)); image.texture.filterMode = FilterMode.Point; image.texture.Apply(); } file.images.Add(image); } } return(file); }