Search Unity

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.
     
    donmetapod likes this.
  4. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,043
    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:
    339
    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.
     
    mrmcduck, megabrobro, petey and 6 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:
    30
    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:
    339
    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
    Goodfeel and Sillixx like this.
  10. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    173
    So, despite the 16 likes, this is 100% incorrect and should NOT be used:

    ...that will only work in the random situation where your anchors are all at a single point (and it's only lucky that it works). It will fail in most situations.

    As per IgorAherne's posts, as far as I can tell, setting size should only be done using:

    RectTransform.SetSizeWithCurrentAnchors

    NB: the Unity docs are shamefully bad here. They literally don't say what the correct way is to set size, and only tell you that the normally, default, correct way won't work because it's blocked (Unity made the rect read-only).

    EDIT: as of 2018/2019, there's also a minor bug in Unity's implementation of this method. Latest 2018.x version was spamming my console with internal Unity errors. Logged the bug, and had an email this week from Unity saying it'll be fixed soon.
     
    Last edited: Feb 23, 2019
  11. magnitocorp

    magnitocorp

    Joined:
    Dec 5, 2018
    Posts:
    1
    Get the rect of that RectTransform,
    Rect rectContainer = Other.rect;
    rectContainer.height = 'the size you want';
     
  12. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    173
    LOL. Now the definitely, absolutely, incorrect answer has 21 likes. People just don't read. Lazy developers = developers who won't be shipping games.

    @magnitocorp - you are 100% incorrect. That will achieve literally nothing. There is no way you tried that code - it could never work. We all make mistakes, that's fine, but ... if you're going to give advice, at least TRY IT YOURSELF first and check your understanding before handing out ideas :).
     
  13. elKarkayu

    elKarkayu

    Joined:
    Mar 23, 2017
    Posts:
    15
    thanks for your replay a436t4ataf!!