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);
        }
Esempio n. 2
0
        public PAImageView(PACanvas canvas)
        {
            AddToClassList("canvas");

            pickingMode = PickingMode.Ignore;

            _canvas     = canvas;
            _background = PAUtils.LoadImage("Grid.psd");

            generateVisualContent += OnGenerateVisualContent;
        }
Esempio n. 3
0
 /// <summary>
 /// Set the window title with with the pixel art icon
 /// </summary>
 public void RefreshTitle() =>
 titleContent = new GUIContent(
     Workspace == null ? "PixelArt" : $"{Workspace.Target.name}{(Workspace.IsModified ? "*" : "")}",
     PAUtils.LoadImage("PixelArtEditor.psd"));