Author Topic: [Script] UITextureAspectRatio  (Read 3491 times)

mimminito

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
[Script] UITextureAspectRatio
« on: September 20, 2013, 05:54:14 AM »
Hi,

I was in need of a solution to place a UITexture inside a Clipped Panel, so that it would fit keeping its aspect ratio.
The UIStretch did not provide a valid solution, as it is based on fixed values specified in the editor, and the Texture can change at runtime.

So, I wrote a simple script, based off of UIStretch, which will correctly fit the UITexture into the Clipped Panel, and respect its aspect ratio.

Im sure it could be improved, but it works fine for me, so I thought I would share it :)

  1. using UnityEngine;
  2.  
  3. [ExecuteInEditMode]
  4. [RequireComponent(typeof(UITexture))]
  5. public class UITextureAspectRatio : MonoBehaviour
  6. {
  7.         /// <summary>
  8.         /// Panel used to determine the container's bounds. Overwrites the widget-based anchoring if the value was specified.
  9.         /// </summary>
  10.         public UIPanel panelContainer = null;
  11.        
  12.         private UITexture mUITexture;
  13.         private Rect mRect;
  14.         private Transform mTrans;
  15.        
  16.         void Awake() {
  17.                 mRect = new Rect();
  18.                 mTrans = transform;
  19.         }
  20.        
  21.         void Start() {
  22.                 mUITexture = GetComponent<UITexture>();
  23.                
  24.                 Update();
  25.         }
  26.        
  27.         void Update() {
  28.                 if (panelContainer != null)
  29.                 {
  30.                         if (panelContainer.clipping == UIDrawCall.Clipping.None)
  31.                         {
  32.                                 // Panel has no clipping -- just use the screen's dimensions
  33.                                 mRect.xMin = -Screen.width * 0.5f;
  34.                                 mRect.yMin = -Screen.height * 0.5f;
  35.                                 mRect.xMax = -mRect.xMin;
  36.                                 mRect.yMax = -mRect.yMin;
  37.                         }
  38.                         else
  39.                         {
  40.                                 // Panel has clipping -- use it as the rect
  41.                                 Vector4 pos = panelContainer.clipRange;
  42.                                 mRect.x = pos.x - (pos.z * 0.5f);
  43.                                 mRect.y = pos.y - (pos.w * 0.5f);
  44.                                 mRect.width = pos.z;
  45.                                 mRect.height = pos.w;
  46.                         }
  47.                 }
  48.                
  49.                 Vector3 localScale = mTrans.localScale;
  50.                
  51.                 float rectWidth = mRect.width;
  52.                 float rectHeight = mRect.height;
  53.                
  54.                 Texture tex = mUITexture.mainTexture;
  55.                 Rect uvRect = mUITexture.uvRect;
  56.                
  57.                 float texWidth = tex.width * uvRect.width;
  58.                 float texHeight = tex.height * uvRect.height;
  59.                
  60.                 float screenRatio = rectWidth / rectHeight;
  61.                 float imageRatio = texWidth / texHeight;
  62.  
  63.                 if (imageRatio > screenRatio)
  64.                 {
  65.                         // Fit horizontally
  66.                         float scale = rectWidth / texWidth;
  67.                         localScale.x = rectWidth;
  68.                         localScale.y = texHeight * scale;
  69.                 }
  70.                 else
  71.                 {
  72.                         // Fit vertically
  73.                         float scale = rectHeight / texHeight;
  74.                         localScale.x = texWidth * scale;
  75.                         localScale.y = rectHeight;
  76.                 }
  77.                 if (mTrans.localScale != localScale) mTrans.localScale = localScale;
  78.         }
  79. }
  80.  
  81.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Script] UITextureAspectRatio
« Reply #1 on: September 20, 2013, 04:46:59 PM »
Cool, thanks for sharing.