Search Unity

Who can tell me how to resize a rect transform based UI element in script, like image?

Discussion in 'UGUI & TextMesh Pro' started by Orca-Chen, Aug 26, 2014.

  1. Orca-Chen

    Orca-Chen

    Joined:
    Aug 26, 2014
    Posts:
    7
    All the tutorials only tell us how to resize it in editor, but in my case, I need to resize it in code. How to do this simply?
     
  2. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    322
    The .localscale property should do that nicely for you.
     
  3. Orca-Chen

    Orca-Chen

    Joined:
    Aug 26, 2014
    Posts:
    7
    but I do not wanna change localscale. When I change an image's sprite to another one which size is different, I only wanna to make new sprite turn to it's current size, and keep it's position, scale, etc.
     
  4. T2Unity

    T2Unity

    Joined:
    Aug 21, 2014
    Posts:
    26
    call image.SetNativeSize()
    or
    change rectTransform`s sizeDelta
    from script
     
  5. Orca-Chen

    Orca-Chen

    Joined:
    Aug 26, 2014
    Posts:
    7
    thanks. I got it.
     
    T2Unity likes this.