Search Unity

Very useful RectTransform extension methods

Discussion in 'UGUI & TextMesh Pro' started by vexe, Dec 13, 2014.

  1. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    Recently I came across this question that had some really handy extension methods I thought I'd share. I've added AnchorToCorners as well. Feel free to share what you found useful for you

    Code (csharp):
    1.  
    2.   public static class RectTransformExtensions
    3.    {
    4.      public static void AnchorToCorners(this RectTransform transform)
    5.      {
    6.        if (transform == null)
    7.          throw new ArgumentNullException("transform");
    8.  
    9.        if (transform.parent == null)
    10.          return;
    11.  
    12.        var parent = transform.parent.GetComponent<RectTransform>();
    13.  
    14.        Vector2 newAnchorsMin = new Vector2(transform.anchorMin.x + transform.offsetMin.x / parent.rect.width,
    15.                          transform.anchorMin.y + transform.offsetMin.y / parent.rect.height);
    16.  
    17.        Vector2 newAnchorsMax = new Vector2(transform.anchorMax.x + transform.offsetMax.x / parent.rect.width,
    18.                          transform.anchorMax.y + transform.offsetMax.y / parent.rect.height);
    19.  
    20.        transform.anchorMin = newAnchorsMin;
    21.        transform.anchorMax = newAnchorsMax;
    22.        transform.offsetMin = transform.offsetMax = new Vector2(0, 0);
    23.      }
    24.  
    25.      public static void SetPivotAndAnchors(this RectTransform trans, Vector2 aVec)
    26.      {
    27.        trans.pivot = aVec;
    28.        trans.anchorMin = aVec;
    29.        trans.anchorMax = aVec;
    30.      }
    31.  
    32.      public static Vector2 GetSize(this RectTransform trans)
    33.      {
    34.        return trans.rect.size;
    35.      }
    36.  
    37.      public static float GetWidth(this RectTransform trans)
    38.      {
    39.        return trans.rect.width;
    40.      }
    41.  
    42.      public static float GetHeight(this RectTransform trans)
    43.      {
    44.        return trans.rect.height;
    45.      }
    46.  
    47.      public static void SetSize(this RectTransform trans, Vector2 newSize)
    48.      {
    49.        Vector2 oldSize = trans.rect.size;
    50.        Vector2 deltaSize = newSize - oldSize;
    51.        trans.offsetMin = trans.offsetMin - new Vector2(deltaSize.x * trans.pivot.x, deltaSize.y * trans.pivot.y);
    52.        trans.offsetMax = trans.offsetMax + new Vector2(deltaSize.x * (1f - trans.pivot.x), deltaSize.y * (1f - trans.pivot.y));
    53.      }
    54.  
    55.      public static void SetWidth(this RectTransform trans, float newSize)
    56.      {
    57.        SetSize(trans, new Vector2(newSize, trans.rect.size.y));
    58.      }
    59.  
    60.      public static void SetHeight(this RectTransform trans, float newSize)
    61.      {
    62.        SetSize(trans, new Vector2(trans.rect.size.x, newSize));
    63.      }
    64.  
    65.      public static void SetBottomLeftPosition(this RectTransform trans, Vector2 newPos)
    66.      {
    67.        trans.localPosition = new Vector3(newPos.x + (trans.pivot.x * trans.rect.width), newPos.y + (trans.pivot.y * trans.rect.height), trans.localPosition.z);
    68.      }
    69.  
    70.      public static void SetTopLeftPosition(this RectTransform trans, Vector2 newPos)
    71.      {
    72.        trans.localPosition = new Vector3(newPos.x + (trans.pivot.x * trans.rect.width), newPos.y - ((1f - trans.pivot.y) * trans.rect.height), trans.localPosition.z);
    73.      }
    74.  
    75.      public static void SetBottomRightPosition(this RectTransform trans, Vector2 newPos)
    76.      {
    77.        trans.localPosition = new Vector3(newPos.x - ((1f - trans.pivot.x) * trans.rect.width), newPos.y + (trans.pivot.y * trans.rect.height), trans.localPosition.z);
    78.      }
    79.  
    80.      public static void SetRightTopPosition(this RectTransform trans, Vector2 newPos)
    81.      {
    82.        trans.localPosition = new Vector3(newPos.x - ((1f - trans.pivot.x) * trans.rect.width), newPos.y - ((1f - trans.pivot.y) * trans.rect.height), trans.localPosition.z);
    83.      }
    84.    }
    85.  
     
    steril and nareshbishtasus like this.
  2. fermmmm

    fermmmm

    Joined:
    Oct 18, 2013
    Posts:
    129
  3. nareshbishtasus

    nareshbishtasus

    Joined:
    Jun 11, 2018
    Posts:
    36
    Trying to set size for last 2 hours and then found this script, thought will not work but is worked like a charm thank you. you are the man!
     
  4. piggyfatguy

    piggyfatguy

    Joined:
    Mar 4, 2023
    Posts:
    3
    it says SetSize doesn't exist? i want to set the height and width of my button