Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimpenekli committed Jan 4, 2024
2 parents cbe7981 + de24043 commit 7978eb0
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 49 deletions.
4 changes: 4 additions & 0 deletions Packages/com.cdm.figma.ui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.8.8] - 2024-01-04
### Fixes
- Minor import stats fixes

## [1.8.7] - 2023-12-17
### Fixes
- Fixed a bug that `NullReferenceException` is thrown if a node converter fails to convert a node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ private void DrawStatsGui()

EditorGUILayout.Space();

LabelField("Texture Count - Smallest", $"{_stats.smallestTextureCount} (= {minTextureSize})");
LabelField("Texture Count - Smallest", $"{_stats.smallestTextureCount} (<= {minTextureSize})");
LabelField("Texture Count - Small", $"{_stats.smallTextureCount} (< {avgTextureSize})");
LabelField("Texture Count - Big", $"{_stats.bigTextureCount} (>= {avgTextureSize})");
LabelField("Texture Count - Biggest", $"{_stats.biggestTextureCount} (= {maxTextureSize})");
LabelField("Texture Count - Biggest", $"{_stats.biggestTextureCount} (>= {maxTextureSize})");
}

private void InitStats()
Expand All @@ -62,8 +62,8 @@ private void InitStats()

_stats.smallTextureCount = _stats.textureCount - _stats.bigTextureCount;

_stats.smallestTextureCount = CountTexture(assets, (t, min, max) => t.width == min || t.height == min);
_stats.biggestTextureCount = CountTexture(assets, (t, min, max) => t.width == max || t.height == max);
_stats.smallestTextureCount = CountTexture(assets, (t, min, max) => t.width <= min || t.height <= min);
_stats.biggestTextureCount = CountTexture(assets, (t, min, max) => t.width >= max || t.height >= max);

foreach (var asset in assets)
{
Expand Down
4 changes: 2 additions & 2 deletions Packages/com.cdm.figma.ui/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "com.cdm.figma.ui",
"displayName": "Unity Figma Importer - UGUI",
"version": "1.8.7",
"version": "1.8.8",
"unity": "2021.3",
"author": "CDM Vision",
"description": "Unity Figma Importer turns your Figma design into Unity UI elements and can generate code and layout files to create Unity apps.",
"hideInEditor": false,
"dependencies": {
"com.cdm.figma": "1.8.7",
"com.cdm.figma": "1.8.8",
"com.unity.nuget.newtonsoft-json": "2.0.2",
"com.unity.vectorgraphics": "2.0.0-preview.21"
}
Expand Down
4 changes: 4 additions & 0 deletions Packages/com.cdm.figma/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.8.8] - 2024-01-04
### Fixes
- Fix a bug that `minTextureSize` is used instead of `rectTextureSize`

## [1.8.7] - 2023-12-17
- No changes

Expand Down
117 changes: 76 additions & 41 deletions Packages/com.cdm.figma/Runtime/Utils/NodeSpriteGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ private static GeneratedSprite GenerateSpriteInternal(FigmaFile file, SceneNode
return new GeneratedSprite(imageSprite, true);
}

var svg = GenerateSpriteSvg(node, options.Value.overrideNode, options.Value);
var svg = GenerateSvgFromPath(node, options.Value.overrideNode, options.Value);

#if FIGMA_PRINT_SVG_STRING
Debug.Log($"{svg.node}: {svg.content}");
#endif

var generatedSprite = new GeneratedSprite();

Expand All @@ -73,25 +77,24 @@ private static GeneratedSprite GenerateSpriteInternal(FigmaFile file, SceneNode
// Comparing svg string is faster than calculating hashes and comparing the them.
// So just use svg string for the key.

if (spriteDatabase.TryGetValue(svg, out var sprite))
if (spriteDatabase.TryGetValue(svg.content, out var sprite))
{
generatedSprite = new GeneratedSprite(sprite, false);

}
else
{
sprite = GenerateSprite(node, svg, spriteType, options);
sprite = GenerateSprite(node, svg.content, spriteType, svg.dontResize, options);

if (sprite != null)
{
generatedSprite = new GeneratedSprite(sprite, true);
spriteDatabase.Add(svg, sprite);
spriteDatabase.Add(svg.content, sprite);
}
}
}
else
{
var sprite = GenerateSprite(node, svg, spriteType, options);
var sprite = GenerateSprite(node, svg.content, spriteType, svg.dontResize, options);
generatedSprite = new GeneratedSprite(sprite, true);
}

Expand All @@ -105,14 +108,14 @@ private static GeneratedSprite GenerateSpriteInternal(FigmaFile file, SceneNode
/// </summary>
public static string GenerateSpriteSvg(SceneNode node, SceneNode overrideNode, SpriteGenerateOptions options)
{
return GenerateSvgFromPath(node, overrideNode, options);
return GenerateSvgFromPath(node, overrideNode, options).content;
}

/// <summary>
/// Generates a sprite from the scene node and the SVG string given.
/// </summary>
public static Sprite GenerateSprite(SceneNode node, string svg, SpriteGenerateType spriteType,
SpriteGenerateOptions? options = null)
bool dontResize, SpriteGenerateOptions? options = null)
{
options ??= SpriteGenerateOptions.GetDefault();

Expand All @@ -121,7 +124,7 @@ public static Sprite GenerateSprite(SceneNode node, string svg, SpriteGenerateTy
case SpriteGenerateType.Auto:
if (node is INodeRect)
{
return GenerateRectSpriteFromSvg(node, svg, options.Value);
return GenerateRectSpriteFromSvg(node, svg, dontResize, options.Value);
}
else
{
Expand All @@ -132,7 +135,7 @@ public static Sprite GenerateSprite(SceneNode node, string svg, SpriteGenerateTy
return GenerateSpriteFromSvg(node, svg, options.Value);

case SpriteGenerateType.Rectangle:
return GenerateRectSpriteFromSvg(node, svg, options.Value);
return GenerateRectSpriteFromSvg(node, svg, dontResize, options.Value);

default:
throw new ArgumentOutOfRangeException(nameof(spriteType), spriteType, null);
Expand Down Expand Up @@ -185,7 +188,7 @@ private static Rect CalculateSvgViewBox(SceneNode node, float width, float heigh
height: height + strokePadding);
}

private static string GenerateSvgFromPath(SceneNode node, SceneNode overrideNode, SpriteGenerateOptions options)
private static SvgString GenerateSvgFromPath(SceneNode node, SceneNode overrideNode, SpriteGenerateOptions options)
{
if (node is not VectorNode vectorNode || node is RectangleNode)
{
Expand Down Expand Up @@ -229,17 +232,13 @@ private static string GenerateSvgFromPath(SceneNode node, SceneNode overrideNode
}

svg.AppendLine("</svg>");

#if FIGMA_PRINT_SVG_STRING
Debug.Log($"{node}: {svg}");
#endif
return svg.ToString();
return new SvgString(node, svg.ToString());
}

private static Sprite GenerateSpriteFromSvg(SceneNode node, string svg, SpriteGenerateOptions options)
{
var sceneInfo = ImportSvg(svg);
return CreateTexturedSprite(node, options, sceneInfo);
return CreateSprite(node, options, sceneInfo);
}

private static SVGParser.SceneInfo ImportSvg(string svg)
Expand All @@ -254,7 +253,8 @@ private static SVGParser.SceneInfo ImportSvg(string svg)
}
}

private static string GenerateSvgFromRect(SceneNode node, SceneNode overrideNode, SpriteGenerateOptions options)
private static SvgString GenerateSvgFromRect(SceneNode node, SceneNode overrideNode,
SpriteGenerateOptions options)
{
if (node is not INodeRect nodeRect)
throw new ArgumentException("Specified node does not define a rectangle.", nameof(node));
Expand All @@ -269,13 +269,15 @@ private static string GenerateSvgFromRect(SceneNode node, SceneNode overrideNode
var height = nodeTransform.size.y;

var fills = GetSvgFills(node, overrideNode, null);


var dontResize = false;
var isGradientExist = fills.Any(x => x is GradientPaint);

if (!isGradientExist)
{
width = options.rectTextureSize + nodeRect.topLeftRadius + nodeRect.topRightRadius;
height = options.rectTextureSize + nodeRect.bottomLeftRadius + nodeRect.bottomRightRadius;
dontResize = true;
}

var strokeWeight = nodeFill.GetStrokeWeightOrDefault();
Expand All @@ -296,11 +298,11 @@ private static string GenerateSvgFromRect(SceneNode node, SceneNode overrideNode
}

svg.AppendLine("</svg>");

#if FIGMA_PRINT_SVG_STRING
Debug.Log($"{node}: {svg}");
#endif
return svg.ToString();
return new SvgString(node, svg.ToString())
{
dontResize = dontResize
};
}

private static void AppendSvgSizeAndViewBox(StringBuilder svg, Rect viewBox)
Expand Down Expand Up @@ -337,7 +339,8 @@ private static Sprite GenerateSpriteFromImage(FigmaFile file, SceneNode node, Sp
return CreateTexturedSprite(node, options, imageTexture);
}

private static Sprite GenerateRectSpriteFromSvg(SceneNode node, string svg, SpriteGenerateOptions options)
private static Sprite GenerateRectSpriteFromSvg(SceneNode node, string svg, bool dontResize,
SpriteGenerateOptions options)
{
if (node is not INodeRect nodeRect)
throw new ArgumentException("Specified node does not define a rectangle.", nameof(node));
Expand All @@ -363,7 +366,7 @@ private static Sprite GenerateRectSpriteFromSvg(SceneNode node, string svg, Spri
);

var sceneInfo = ImportSvg(svg);
return CreateTexturedSprite(node, options, sceneInfo, borders);
return CreateSprite(node, options, sceneInfo, dontResize, borders);
}

private static void AppendSvgGradientStops(StringBuilder svg, GradientPaint gradient)
Expand Down Expand Up @@ -640,19 +643,8 @@ private static void AppendSvgStrokeRectElement(StringBuilder svg, SceneNode node
}
}

private static Sprite CreateTexturedSprite(SceneNode node, SpriteGenerateOptions options,
SVGParser.SceneInfo sceneInfo, Vector4? borders = null)
private static (Vector2, float) GetScaledSize(Sprite sprite, SpriteGenerateOptions options)
{
var geometries =
VectorUtils.TessellateScene(sceneInfo.Scene, options.tessellationOptions, sceneInfo.NodeOpacity);

var sprite = VectorUtils.BuildSprite(geometries, sceneInfo.SceneViewport,
options.pixelsPerUnit, VectorUtils.Alignment.Center,
Vector2.zero, options.gradientResolution, true);

if (sprite == null)
return null;

var spriteWidth = sprite.rect.width * options.scaleFactor;
var spriteHeight = sprite.rect.height * options.scaleFactor;

Expand Down Expand Up @@ -681,8 +673,32 @@ private static Sprite CreateTexturedSprite(SceneNode node, SpriteGenerateOptions
heightScaled = spriteHeight;
}

var textureWidth = Mathf.RoundToInt(Mathf.Max(widthScaled, 1f));
var textureHeight = Mathf.RoundToInt(Mathf.Max(heightScaled, 1f));
return (new Vector2(widthScaled, heightScaled), sizeRatio);
}

private static Sprite CreateSprite(SceneNode node, SpriteGenerateOptions options,
SVGParser.SceneInfo sceneInfo, bool dontResize = false, Vector4? borders = null)
{
var geometries =
VectorUtils.TessellateScene(sceneInfo.Scene, options.tessellationOptions, sceneInfo.NodeOpacity);

var sprite = VectorUtils.BuildSprite(geometries, sceneInfo.SceneViewport,
options.pixelsPerUnit, VectorUtils.Alignment.Center,
Vector2.zero, options.gradientResolution, true);

if (sprite == null)
return null;

var size = new Vector2(sprite.rect.width, sprite.rect.height);
var sizeRatio = 1f;

if (!dontResize)
{
(size, sizeRatio) = GetScaledSize(sprite, options);
}

var textureWidth = Mathf.RoundToInt(Mathf.Max(size.x, 1f));
var textureHeight = Mathf.RoundToInt(Mathf.Max(size.y, 1f));

var expandEdges = options.expandEdges &&
(options.filterMode != FilterMode.Point || options.sampleCount > 1);
Expand Down Expand Up @@ -718,7 +734,7 @@ private static Sprite CreateTexturedSprite(SceneNode node, SpriteGenerateOptions
}

private static Sprite CreateTexturedSprite(SceneNode node, SpriteGenerateOptions options,
Texture2D texture, float scale = 1f, Vector4? borders = null)
Texture2D texture, float scale = 1f, Vector4? borders = null)
{
var spriteRect = new Rect(0, 0, texture.width, texture.height);
var spritePivot = spriteRect.center;
Expand Down Expand Up @@ -816,5 +832,24 @@ private static Material GetSpriteMaterial()
return UnityEditor.AssetDatabase.LoadAssetAtPath<Material>(SpriteMaterialPath);
#endif
}

private class SvgString
{
public SceneNode node { get; }
public string content { get; }
public bool dontResize { get; set; }

public SvgString(SceneNode node, string content)
{
this.node = node;
this.content = content;
}

public override string ToString()
{
return content;
}
}
}

}
2 changes: 1 addition & 1 deletion Packages/com.cdm.figma/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.cdm.figma",
"displayName": "Unity Figma Importer",
"version": "1.8.7",
"version": "1.8.8",
"unity": "2021.3",
"author": "CDM Vision",
"description": "Unity Figma Importer turns your Figma design into Unity UI and can generate code and layout files to create Unity apps.",
Expand Down
2 changes: 1 addition & 1 deletion Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"depth": 0,
"source": "embedded",
"dependencies": {
"com.cdm.figma": "1.8.7",
"com.cdm.figma": "1.8.8",
"com.unity.nuget.newtonsoft-json": "2.0.2",
"com.unity.vectorgraphics": "2.0.0-preview.21"
}
Expand Down

0 comments on commit 7978eb0

Please sign in to comment.