Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

How to get current height of button

Discussion in 'Getting Started' started by jshrek, Mar 26, 2015.

  1. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    I have the following code which hard codes the button size to 50:
    Code (CSharp):
    1. btnTr.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 50);
    I actually want to get the current height of the button and add 10 to it's size, but not quite sure how to do that. So something like this:
    Code (CSharp):
    1. btnTr.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, currentHeightOfButton + 10);
     
  2. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    Well thank God for autocomplete ... I figured it out:
    btnTr.rect.height

    So
    Code (CSharp):
    1. btnTr.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, btnTr.rect.height + 10);
     
    DMorock likes this.