Unity的UGUI在SetParent后修改UI的localposition问题

正常情况下,UGUI设置UI的localposition可以直接赋值

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

运行后在Unity的Inspector面板可以得到正确的()。
但如果

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

运行后得到的坐标值是错误的。
正确的做法:

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


通过设置锚点的坐标来设置UI的position。
运行后,在Inspector面板下可以看到正确的Rect Transform。

猜你喜欢

转载自www.cnblogs.com/Afuness/p/11442545.html