Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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,999
    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);
     
  5. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    319
    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.
     
    petey, indieDoroid, Simski and 4 others like this.
  6. Jayno

    Jayno

    Joined:
    Feb 20, 2013
    Posts:
    5
    @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:
    319
    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
    Sillixx likes this.