Skip to content

Commit

Permalink
Sprites with scale mode set to StretchToFit now draw correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
h-sigma committed Feb 9, 2021
1 parent 9fb5cf3 commit 1a0a244
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions PvCustomizer/Editor/Scripts/PvCustomizerGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private static void DrawTextureDirect(Rect rect, Texture2D texture, Material mat
material.SetFloat(s_TintAmount, PvCustomizerSettings.GetOrCreateSettings().TintAmount);
material.SetColor(s_Tint, tint ?? Color.white);
UnityEditor.EditorGUI.DrawPreviewTexture(rect, texture, material,
(scaleMode ?? PvScaleMode.ScaleAndCrop).UnityScaleMode());
(scaleMode ?? PvScaleMode.ScaleToFit).UnityScaleMode());
}

#endregion
Expand Down Expand Up @@ -87,10 +87,30 @@ public static void DrawColor(Rect rect, Color color)
/// <param name="sprite">The sprite to be drawn.</param>
/// <param name="style">The compiled style for the icon.</param>
/// <param name="material">The Material used to draw the sprite. Leave null to use default.</param>
public static void DrawSprite(Rect rect, Sprite sprite, Material material = null, Color? tint = null)
public static void DrawSprite(Rect rect, Sprite sprite, Material material = null, Color? tint = null, PvScaleMode? scaleMode = null)
{
if (sprite == null) return;
DrawTexWithCoords(rect, sprite.texture, sprite.textureRect, material, tint: tint);
Rect texRect = sprite.textureRect;

if (scaleMode == PvScaleMode.ScaleToFit)
{
float drawAspect = rect.Aspect();
float texAspect = texRect.Aspect();
if (drawAspect > texAspect) //drawer rect is wider than tex rect
{
float widthToAdd = texRect.height * drawAspect;
texRect.x -= (widthToAdd - texRect.width) / 2f;
texRect.width = widthToAdd;
}
else
{
float heightToAdd = texRect.width / drawAspect;
texRect.y -= (heightToAdd - texRect.height) / 2f;
texRect.height = heightToAdd;
}
}

DrawTexWithCoords(rect, sprite.texture, texRect, material, tint: tint);
}

/// <summary>
Expand Down

0 comments on commit 1a0a244

Please sign in to comment.