PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 16, 2022

[FIXED] How to get correct position of a gameobject thatis inside a vertical Layout group in unity?

 November 16, 2022     position, unity3d, vertical-alignment     No comments   

Issue

I have a vertical layout group containing 3 game object

enter image description here

enter image description here

when I try to get the correct position of the child Objects from the vertical Layout group, it does not giving the correct output. enter image description here

enter image description here

I tried

myPosition = this.gameObject.transform.localPosition;
myPosition = this.gameObject.transform.position;
myPosition = this.GetComponent<RectTransform>().position;
myPosition = this.GetComponent<RectTransform>().anchoredPosition;

Non gave the correct output. How to get this


Solution

You just need to fetch the position in Update or LateUpdate method, don't do that in Awake or Start.

void Update()
{
    if(transform.hasChanged)
        myPosition = ((RectTransform)transform).anchoredPosition;
}

https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/UIAutoLayout.html

The rebuild will not happen immediately, but at the end of the current frame, just before rendering happens.


According to the documentation above, you cannot freely trigger layout rebuild, so this way is just based on the order of layout calculations: Call these 4 methods before fetching the position.

// VerticalLayoutGroup vlg;
vlg.CalculateLayoutInputHorizontal();
vlg.SetLayoutHorizontal();
vlg.CalculateLayoutInputVertical();
vlg.SetLayoutVertical();
myPosition = ((RectTransform)transform).anchoredPosition;


Answered By - shingo
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,213,753

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing