正常情況下,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。