Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Modify the width and height of RectTransform

Discussion in 'Unity UI & TextMesh Pro' started by lzt120, Sep 27, 2014.

  1. lzt120

    lzt120

    Joined:
    Apr 13, 2010
    Posts:
    93
    How Can I modify the width and height of RectTransform ?
     
    belwar likes this.
  2. Chudziutki

    Chudziutki

    Joined:
    May 16, 2014
    Posts:
    3
    Try this

    RectTransform rt = UICanvas.GetComponent (typeof (RectTransform)) as RectTransform;
    rt.sizeDelta = new Vector2 (100, 100);

    where UICanvas is Canvas from scene.
     
  3. lzt120

    lzt120

    Joined:
    Apr 13, 2010
    Posts:
    93
    Thanks very much.
     
    nomadRaptor likes this.
  4. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    2,950
    A somewhat nicer way to write this (assuming that canvas is a variable with that Canvas from the scene):

    Code (csharp):
    1. RectTransform rt = canvas.GetComponent<RectTransform>();
    2. rt.sizeDelta = new Vector2(100, 100);
     
    Laniemme, Harinezumi, ow3n and 4 others like this.
  5. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    289
    if anyone like me is wondering what the is sizeDelta, here is explanation with pictures.
    Was browsing interwebs, trying to understand what the documentation actually means.
     
  6. Jayno

    Jayno

    Joined:
    Feb 20, 2013
    Posts:
    3
    @Chudziutki thank you
     
  7. lemonLimeBitta

    lemonLimeBitta

    Joined:
    Aug 28, 2014
    Posts:
    2
    Sorry to necropost a bit here,
    Can anyone explain to me why I can't change the rect transforms height directly?

    In @jashan 's example,

    1. Code (CSharp):
      1. RectTransform rt = canvas.GetComponent<RectTransform>();
      2. rt.sizeDelta = new Vector2(100, 100);
      Why can't I do something like:
      Code (CSharp):
      1. RectTransform rt = canvas.GetComponent<RectTransform>();
      2. rt.rect.height= 100;
      or even this doesn't work
      Code (CSharp):
      1. RectTransform rt = canvas.GetComponent<RectTransform>();
      2. rt.sizeDelta.y =  100;
     
  8. mStep

    mStep

    Joined:
    Sep 24, 2012
    Posts:
    25
    That's just not how the properties work. Similarly to how you can't directly set a single axis of a Vector3, you have to set the whole thing at once. But you can reference the current value for whatever you don't want to change. So instead of:

    Code (CSharp):
    1. RectTransform rt = canvas.GetComponent<RectTransform>();
    2. rt.sizeDelta.y =  100;
    It would be:

    Code (CSharp):
    1. RectTransform rt = canvas.GetComponent<RectTransform>();
    2. rt.sizeDelta = new Vector2 (rt.sizeDelta.x, 100);
     
  9. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    289
    gonna also try now

    myRecttransf.SetSizeWithCurrentAnchors() ;
    followed by
    myRecttransf.ForceUpdateRectTransforms();

    Need something like this because re-parenting back and forth, and adjusting the recttransform several times during a frame messes up any adjustments done previously, during this frame. Hopefully forceUpdateRectTranasforms will fix these things

    Edit: Well, decided to go with a hybrid:
    Code (CSharp):
    1. // places rect transform to have the same dimensions as 'other', even if they don't have same parent.
    2.     // Relatively non-expensive.
    3.     // NOTICE - also modifies scale of your rectTransf to match the scale of other
    4.     public static void MatchOther(this RectTransform rt,  RectTransform other){
    5.         Vector2 myPrevPivot = rt.pivot;
    6.         myPrevPivot = other.pivot;
    7.         rt.position =  other.position;
    8.  
    9.         rt.localScale = other.localScale;
    10.            
    11.         rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,  other.rect.width);
    12.         rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical,  other.rect.height);
    13.         //rectTransf.ForceUpdateRectTransforms(); - needed before we adjust pivot a second time?
    14.         rt.pivot = myPrevPivot;
    15.     }
     
    Last edited: Jun 15, 2018