• 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
0
Question by Sun-Pengfei · Nov 30, 2017 at 10:38 AM · ui

How to get children's world position in a GridLayoutGroup?

I have a scroll view, in its content object there's a GridLayoutGroup component(the content object has its own position and scales instead of (0,0,0) or (1,1,1) ), and under it there're several images. During runtime I want to take out an image and set its parent to other UI object, but I want the image remain its position on screen.

However I tried all the following ways, all ended up with wrong position on screen(the image moved sometimes even off screen).

1 Use SetParent method on the image object(tried both True or false as second parameter):

 imageObject.transform.SetParent(otherObj, True);

2 Use SetParent method in the 1st way, then give a position to the image manually, but id does not appear at the mouse position(this code works fine for other objects that are not in a layoutgroup):

 imageObject.transform.SetParent(otherObj, True);
 var p = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 imageObject.transform.position = new Vector3(p.x, p.y, 0);

3 Set the position to its original value after calling SetParent.

 imageObject.transform.SetParent(otherObj, True);
 var p = imageObject.transform.position;
 imageObject.transform.position = new Vector3(p.x, p.y, 0);

4 don't use any code but during runtime in the editor, manually drag the image out to targeted parent object. Still its position is changed.

5 don't use any code but during runtime in the editor, manually uncheck the GridLayoutGroup to disable it. Still the image position is changed.

Why the 3rd way is not working? I think probably the transform.position of the image object is not used so the value is not used so the value is not where the position is on screen, or something happened in the end of the frame so my reseting of the position is useless.

More info: my canvas render mode is set to "Screen Space-Camera". But even changed it to overlay, it remained the same result. Canvas scaler mode is "Scale with Screen Size", screen match mode is "Expand".

So what should I do to take out the image out of the GridLayoutGroup but let it stays where it is on screen? Thanks!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Sun-Pengfei · Dec 01, 2017 at 05:27 AM

After several tests, I found out that the position after you call SetParent(objParent, True); is right; but due to something related with GridLayoutGroup, the position is changed in the time after this and before the end of the frame. So take a record of that position and do whatever you need in other frames.

Example:

in main thread:

  imageObject.transform.SetParent(otherObj, True);
  originalPosition = imageObject.transform.position;
  imageObject.SetObjectActive(false); // If not do this, the image might flicker at it's position before put it into the GridLayoutGroup. My guess is that it gets rendered before the AdjustTransInTheEndOfFrame method is executed.
  StartCoroutine(AdjustTransInTheEndOfFrame(imageObject));
 
  private IEnumerator AdjustTransInTheEndOfFrame(GameObject obj) 
  {
         yield return new WaitForEndOfFrame();
         obj.transform.position = originalPosition;
         obj.SetObjectActive(true);
  }



Comment
Add comment · Share
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
0

Answer by Thaun_ · Nov 30, 2017 at 10:52 AM

 transform.localPosition

https://docs.unity3d.com/ScriptReference/Transform-localPosition.html

Also, it would not be called world position anymore. transform.position is the world position. transform.localPosition is the local position you are talking about.

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

122 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Image Loading System optimization request 1 Answer

Cannot edit appearence of dropdown UI Unity 5.6 1 Answer

Unity 4.6 UI - A problem and a question 1 Answer

Circular scrolling Main Menu. 0 Answers

Line break UI Text field 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