Skip to content

Commit

Permalink
feat: fix image size problem in Unity 2019
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarianZ committed Dec 6, 2024
1 parent ed806b5 commit 1c881f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
36 changes: 26 additions & 10 deletions Editor/Scripts/IMGUIImageElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public IMGUIImageElement()
style.flexGrow = 1;
style.flexShrink = 0;
style.alignSelf = Align.Stretch;
style.justifyContent = Justify.Center;

RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);

Expand All @@ -45,25 +46,40 @@ private void OnGeometryChanged(GeometryChangedEvent evt)
private void CalcImageDrawerSize()
{
Vector2 containerSize = localBound.size;
if (float.IsNaN(containerSize.x) || float.IsNaN(containerSize.y))
return;

if (!Image || Image.height == 0 || containerSize.y == 0)
return;

float imageWidth = Image.width;
float imageHeight = Image.height;
float aspect = imageWidth / imageHeight;
float maxWidth = Mathf.Min(imageWidth, containerSize.x);
float maxHeight = Mathf.Min(imageHeight, containerSize.y);
float tempAspect = maxWidth / maxHeight;
if (tempAspect > 1 + 1E-3F)
float containerWidth = containerSize.x;
float containerHeight = containerSize.y;
if (imageWidth <= containerWidth && imageHeight <= containerHeight)
{
maxWidth = aspect * maxHeight;
_imageDrawer.style.minWidth = _imageDrawer.style.maxWidth = imageWidth;
_imageDrawer.style.minHeight = _imageDrawer.style.maxHeight = imageHeight;
return;
}
else if (tempAspect < 1 - 1E-3F)

float imageAspect = imageWidth / imageHeight;
float containerAspect = containerWidth / containerHeight;
if (imageAspect >= containerAspect)
{
maxHeight = maxWidth / aspect;
// scale image width to container width
imageWidth = containerWidth;
imageHeight = imageWidth / imageAspect;
}
_imageDrawer.style.minWidth = _imageDrawer.style.maxWidth = maxWidth;
_imageDrawer.style.minHeight = _imageDrawer.style.maxHeight = maxHeight;
else
{
// scale image height to container height
imageHeight = containerHeight;
imageWidth = imageHeight * imageAspect;
}

_imageDrawer.style.minWidth = _imageDrawer.style.maxWidth = imageWidth;
_imageDrawer.style.minHeight = _imageDrawer.style.maxHeight = imageHeight;
}

private void DrawImage()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.greenbamboogames.builtinuiresbrowser",
"version": "1.0.1",
"version": "1.0.2",
"displayName": "Built-in UI Resources Browser!",
"description": "Browser built-in icons, StyleSheets and VisualTreeAssets in Unity Editor.",
"unity": "2019.4",
Expand Down

0 comments on commit 1c881f6

Please sign in to comment.