我有一個包含 3 個游戲物件的垂直布局組
當我嘗試從垂直布局組中獲取子物件的正確位置時,它沒有給出正確的輸出。
我試過了
myPosition = this.gameObject.transform.localPosition;
myPosition = this.gameObject.transform.position;
myPosition = this.GetComponent<RectTransform>().position;
myPosition = this.GetComponent<RectTransform>().anchoredPosition;
Non 給出了正確的輸出。如何得到這個
uj5u.com熱心網友回復:
您只需要在Update
orLateUpdate
方法中獲取位置,不要在Awake
or中執行此操作Start
。
void Update()
{
if(transform.hasChanged)
myPosition = ((RectTransform)transform).anchoredPosition;
}
https://docs.unity3d.com/Packages/[email protected]/manual/UIAutoLayout.html
重建不會立即發生,而是在當前幀結束時,就在渲染發生之前。
根據上面的檔案,你不能隨意觸發布局重建,所以這種方式只是基于布局計算的順序:在獲取位置之前呼叫這4個方法。
// VerticalLayoutGroup vlg;
vlg.CalculateLayoutInputHorizontal();
vlg.SetLayoutHorizontal();
vlg.CalculateLayoutInputVertical();
vlg.SetLayoutVertical();
myPosition = ((RectTransform)transform).anchoredPosition;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/490174.html