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。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM