Unity of UGUI modify localposition problem UI after SetParent

Under normal circumstances, UGUI set the UI can be directly assigned localposition

UIxxx.rectTransform.localPosition = new Vector3(lineLenght[0] / 2f, 0, 0);

After running to get the right in the Unity Inspector panel ().
but if

UIxxx.rectTransform.SetParent(GameObx]);
UIxxx.rectTransform.localPosition = new Vector3(lineLenght[0] / 2f, 0, 0);

After running the coordinate values obtained are wrong.
The right way:

UIxxx.rectTransform.SetParent(GameObx]);
UIxxx.rectTransform.anchoredPosition= new Vector3(lineLenght[0] / 2f, 0, 0);


Position UI set by the coordinate setting anchor.
After the operation, Inspector panel can be seen in the correct Rect Transform.

Guess you like

Origin www.cnblogs.com/Afuness/p/11442545.html