Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rect transform values greyed out due to driven values - but need to get them from script

Discussion in 'Scripting' started by agostonr, Feb 21, 2017.

  1. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    I use layout groups to mimic the WPF XAML layout and I need to get the RectTransform width and height in my script to calculate the elements' width and to fit them. Thing is, as my panel's RectTransform's width and height are greyed out I can't access them from my script - I get 0 (as those are driven values themselves, but the values DO get displayed in the inspector). Note that I don't want to change them from my script, just get them. How to?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if the values are greyed out in the inspector then they are being driven by the parent's components or whatever.

    Regardless they are always accessible to scripts, you just need to pull the relevant attribute off the recttransform class.

    https://docs.unity3d.com/ScriptReference/RectTransform.html
     
    agostonr and Kiwasi like this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,163
    One thing I noticed is if your child recttransform is being modified(like a content size fitter or by something else) you may get 0 back if you try to access the size right away.

    I have had to use a coroutine to add a yield waitForEndOfFrame() before I could get the values(otherwise, I would get 0 back instead of the actual width/height.)

    I can't say if this is the best/right way, but I tried several methods of getting the rect size and using sizedelta and had no luck getting the values unless I did a wait.
     
    Ne0mega and agostonr like this.
  4. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Brathnann, you were right, that way I could manage to make it work. One bit is problem though, now it pops once when I start the game (to the desired positions). How to get around this, so the game renders only after the first frame?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I had this issue once. I cheated and put a black quad in front of the camera for the first frame.

    Not idea, but it works.
     
    agostonr likes this.
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,163
    In one project
    Yep, was going to say we do a similar thing. We actually have a title image that is displayed first while the stuff is setup behind it.
     
    agostonr likes this.
  7. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Thanks for the answer. Well the thing is, this won't be a game. I use Unity as a front end tool for a simple project that lets you log in, browse in the wares on store, and mark some with "add to cart". So basically I will use LINQ databases and all those munchies. But fact is, the list won't be the first that is seen and will use a fade in anim anyway :D
     
  8. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    I want to get these values after it's fixed by parent, Any Idea ?
     
  9. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    Inspector and LayoutElement (by enabling preferedWidth and preferedHeight) can access to these values, why we can't ? Unity Developers ?
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,163
    Why we can't what? What values are you wanting?
     
  11. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    The values which topic mentions :) width and height values that is grayed in inspector. (driven by parent)
     
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,163
    It's already been covered on how to get them, or I should say, I added the method I used. Is it not working for you? What issue are you experiencing? Or did you just need a different way of accessing it?
     
  13. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    By getting
    Code (CSharp):
    1. RectTransofrm.rect.prefereWidth
    , the value that return is zero although in Inspector showing correct value.
     
  14. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,163
    And I mentioned trying the coroutine with waitForEndOfFrame. Is this not working for you?
     
  15. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    Actually I want the values that are driven by parent, after that I want to get these values so coroutine doesn't work for this situation.
     
  16. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    OK, This is solution for my problem: I tried to change parent of derived UI and then get width and height of it and end of the frame set previous parent for it.