Got a little surprised when there was no option to set "Aspect Ratio" (eg. Aspect fit & aspect fill) for a UITexture. No need to argue about the numerous times the aspect is not known until runtime (downloaded textures).
Just to be clear, the current "Aspect Ratio"-setting is not what I mean. One should be able to set the UITexture-widget to be of any size, but let the raw texture be rendered within this rect based on a setting that is either "Fill", "Aspect Fit" or "Aspect Fill" (leaving am empty area in the case if aspect fit). That way you can show textures with different resolutions within the same rect. Just a guess is that this should be calculated in the
UITexture.drawingDimensions.
Any plans on implementing this with a nice drop-down list in the inspector? Mandatory IMO.
Cheers!
Edit.Just to save you the trouble, I wrapped up a quick solution (needs minor optimisations, like caching of values etc.).
UITexture.cs:public enum AspectRatio{
Fill,
AspectFill,
AspectFit
}
[SerializeField, HideInInspector] private AspectRatio m_AspectRatioMode = AspectRatio.Fill;
public AspectRatio AspectRatioMode{
get{
return m_AspectRatioMode;
}
set{
if(m_AspectRatioMode != value){
m_AspectRatioMode = value;
MarkAsChanged();
}
}
}
[...]
public override Vector4 drawingDimensions
{
Vector2 size = localSize;
if(mainTexture != null){
switch (m_AspectRatioMode) {
case AspectRatio.Fill:
break;
case AspectRatio.AspectFill:
{
Vector2 maxSize = size;
float xRatio = maxSize.x / mainTexture.width;
float yRatio = maxSize.y / mainTexture.height;
float maxRatio = Mathf.Max(xRatio, yRatio);
size
= new Vector2
(mainTexture
.width * maxRatio, mainTexture
.height * maxRatio
); }
break;
case AspectRatio.AspectFit:
{
Vector2 maxSize = size;
float xRatio = maxSize.x / mainTexture.width;
float yRatio = maxSize.y / mainTexture.height;
float minRatio = Mathf.Min(xRatio, yRatio);
size
= new Vector2
(mainTexture
.width * minRatio, mainTexture
.height * minRatio
); }
break;
default:
throw new System.ArgumentOutOfRangeException(); }
}
[...]
UITextureInspector.cs:mTex.AspectRatioMode = (UITexture.AspectRatio)EditorGUILayout.EnumPopup("Aspect Mode:", mTex.AspectRatioMode);