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);
        }
Beispiel #2
0
        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();
        }