Bootstrap

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。

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;