Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by vexe · Jul 01, 2015 at 01:19 PM · uigridrecttransform

GridLayoutGroup requires a single frame till it does layout? thus giving incorrect element positions in Awake/OnEnable

Hi all,

so in my inventory I've been recently using a GridLayoutGroup for my slots. The inventory takes items as data (by names) in editor, and looks up a database to allocate those items. (e.g. data["it0"] = "Handgun") - I do the item allocation/initialization in OnEnable. I allocate the necessary UI elements for the item (Button, Image, etc) and position it correctly with the right scale etc in the inventory.

The thing is, to properly position the item I'd have to access the position of my slots, which are contained within the GridLayoutGroup. I noticed if I try to access the position of the slots in Awake/OnEnable I get incorrect results. If i do it later (say skip a single frame) in Update it then gives me correct results!

I tried a few things to manually trigger the layout, but nothing seemed to work. e.g.

 LayoutBuilder.MarkLayoutForRebuild(slots_parent);

and a bunch of grid/layout functions: CalculateLayoutInputHorizontal, CalculateLayoutInputVertical

Here is my grid settings:

alt text

Here is a video demonstrating the problem. Please consider watching it.

Notice:

  1. The first time I run the game, items are not positioned correctly

  2. I then reset and re-initialize items manually from editor while the game is running, it positions correctly

  3. I then don't do the allocation in my initialize() but in Update() after skipping a frame or two. It works just fine.

  4. This is obvious, but the same code is used in all 3 cases.

Why is this happening and how to avoid it? Is there anyway I could trigger the layout manually from code, so I don't have to do this ugly hack of waiting a single frame before allocating items?

Thanks in advance, any help is appreciated!

grid.png (30.7 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Jul 01, 2015 at 02:07 PM 0
Share

Doesnt this mean you should use Start ins$$anonymous$$d of Awake? Its still before Update.

I do notice with a canvas that it REALLY likes to resize itself on every resolution change. You have to make sure that it is the canvas that is properly set-up. If the Canvas is initialised before the screen is setup it is very likely that the configuration will come out incorrectly.

Also can you simply fire a coroutine to wait one or two frames?

 OnEnable()
 {
     StartCoroutine(Funcatron());
 }
 
 IEnumerator Functatron()
 {
     yield;
     //yield;
     //DoStuff;
 }

avatar image xpavelos · Jul 10, 2019 at 05:57 PM 0
Share

Hey, I have got the same issue. It's been several years since you posted a question, did you find any technique to get correct positions in Awake?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponent() Problem with getting value. 1 Answer

Move a UI element with respect to canvas scaler 0 Answers

UI scrollable rect, get position on content based on mousePos. 0 Answers

Unity2D Grid disappears when zooming out 0 Answers

Content size fitter does not update rect transform immediately 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges