Exemplo n.º 1
0
        public (IComponent[], IAsset[]) Render(XdObjectJson xdObject, XdAssetHolder assetHolder, IObbGetter obbGetter)
        {
            var components = new List <IComponent>();
            var assets     = new List <IAsset>();

            var color = xdObject.GetFillUnityColor();
            var svg   = SvgUtil.CreateSvg(xdObject);

            xdObject.Group.Children = new XdObjectJson[] { };

            var size      = obbGetter.Get(xdObject).Size;
            var spriteUid = $"{xdObject.GetSimpleName()}_{xdObject.Id.Substring(0, 8)}.png";
            var svgHash   = FastHash.CalculateHash(svg);

            var cachedSvg = assetHolder.GetCachedSvg(svgHash);

            if (cachedSvg != null)
            {
                spriteUid = cachedSvg.SpriteUid;
            }
            else
            {
                assets.Add(new SpriteAsset(spriteUid, svgHash, size, null, null));
                var xdImportSettings = XdImporter.Settings;
                assetHolder.Save(spriteUid, () => SvgToPng.Convert(svg, size, xdImportSettings));
                assetHolder.SaveCacheSvg(spriteUid, svgHash);
            }
            components.Add(new ImageComponent(
                               spriteUid,
                               new Color(1f, 1f, 1f, color.a),
                               Vector2Int.one
                               ));

            return(components.ToArray(), assets.ToArray());
        }
Exemplo n.º 2
0
        public static (ImageComponent, IAsset[]) RenderImage(XdObjectJson xdObject, Obb obb, XdAssetHolder assetHolder)
        {
            ImageComponent imageComponent = null;
            SpriteAsset    asset          = null;

            var color     = xdObject.GetFillUnityColor();
            var ux        = xdObject.Style?.Fill?.Pattern?.Meta?.Ux;
            var flipX     = ux?.FlipX ?? false;
            var flipY     = ux?.FlipY ?? false;
            var direction = new Vector2Int(flipX ? -1 : 1, flipY ? -1 : 1);
            var shapeType = xdObject.Shape?.Type;

            if (!string.IsNullOrWhiteSpace(ux?.Uid))
            {
                var spriteUid = $"{xdObject.GetSimpleName()}_{ux?.Uid.Substring(0, 8)}.png";
                asset          = new SpriteAsset(spriteUid, xdObject.Style.Fill.Pattern.Meta.Ux.HrefLastModifiedDate, obb.Size, null, null);
                imageComponent = new ImageComponent(
                    spriteUid,
                    color,
                    direction
                    );
                assetHolder.Save(spriteUid, xdObject.Style.Fill.Pattern.Meta);
            }
            else if (SvgUtil.Types.Contains(shapeType))
            {
                var spriteUid = $"{xdObject.GetSimpleName()}_{xdObject.Id.Substring(0, 8)}.png";
                var svg       = SvgUtil.CreateSvg(xdObject);
                var svgHash   = FastHash.CalculateHash(svg);

                var cachedSvg = assetHolder.GetCachedSvg(svgHash);
                if (cachedSvg != null)
                {
                    spriteUid = cachedSvg.SpriteUid;
                }
                else
                {
                    asset = new SpriteAsset(spriteUid, svgHash, obb.Size, null, null);
                    var xdImportSettings = XdImporter.Settings;
                    assetHolder.Save(spriteUid, () => SvgToPng.Convert(svg, obb.Size, xdImportSettings));
                    assetHolder.SaveCacheSvg(spriteUid, svgHash);
                }

                imageComponent = new ImageComponent(
                    spriteUid,
                    new Color(1f, 1f, 1f, color.a),
                    direction
                    );
            }
            else
            {
                Debug.LogError($"Unknown shape type {shapeType} in {xdObject.Name}({xdObject.Id}, {xdObject.Guid})");
            }

            var assets = new List <IAsset>();

            if (!xdObject.HasParameter("placeholder") && asset != null)
            {
                assets.Add(asset);
            }
            return(imageComponent, assets.ToArray());
        }