Change UI RectTransform position after setParent()

一个人想着一个人 提交于 2019-12-11 06:35:09

问题


I have these lines :

GameObject bp = Instantiate(MyPrefab);
bp.transform.SetParent(GameObject.FindGameObjectWithTag("ContentFactory").transform);
bp.transform.localPosition = new Vector3(0, 0, 0);

Si, I just instantiate a prefab, I set a parent and I want to change the position.

My problem is, the function SetParent set a fixed position to my bp GameObject, and after that I don't know how to change this position. The last line change nothing.... The same with .position.

How can I change the position of bp ? Thanks !

Edit :

Inspector of ContentFactory :

Inspector of a bp :

Hierarchy (Content in blue is ContentFactory) :


回答1:


Using transform.localPosition will use it relative position. If you want to change the position in world space, you need to use transform.position.

GameObject bp = Instantiate(MyPrefab);
bp.transform.SetParent(GameObject.FindGameObjectWithTag("ContentFactory").transform);
bp.transform.position = new Vector3(0, 0, 0);

EDIT:

This is a UI Object with a RectTransform. You shouldn't move it with transform.position.

It should be moved with:

bp.GetComponent<RectTransform>().anchoredPosition = ...

or

bp.GetComponent<RectTransform>().anchoredPosition3D = ...

Note that there are other things that play role when using RectTransform.

This includes anchorMax and anchorMin which can be modified with:

bp.GetComponent<RectTransform>().anchorMax = ...
bp.GetComponent<RectTransform>().anchorMin = ...



回答2:


I'm going to supply a different answer than Programmer did, just because it's how I handle my dynamically filled ScrollViews with the new UI system. I will have to play around to see if using RectTransform.anchoredPosition instead works better or not.


I set the anchor min:max values to (0,1):(0,1) so that the buttons (images, text, whatever) treats the top-left corner of the scroll view as the origin. Then I set the pivot to be (0,1) as well, so that the top left corner of the button is the origin (and then these values are baked into the prefab: I never need to fiddle with them again). This lets me set the position to (0,0,0) and have it be placed where I want it to be, although I'll generally nudge them right and down by a few pixels so that it Looks Nice, but all of the items receive the same flat offsets.

Then in my code I can use .localPosition in a manner that allows those values to line up with where I expect my object to be placed, keeping in mind that new items need to be added below the old items, therefor each button's Y value is going to be negative after the first one. This offset will be equal to the item's height (visible in the Inspector) plus a few pixels for padding.

Finally, I always make sure to call SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, totalHeight) on the ScrollView's content's RectTransform to update the scrollbars to the proper values, and if necessary, to reset the content transform's own position to (0,0,0) so that it is scrolled back to the top. totalHeight is effectively the (positive) Y position of the next item that would be placed, plus another flat buffer amount (same value used to offset things from the top) again, so it Looks Nice.



来源:https://stackoverflow.com/questions/44186212/change-ui-recttransform-position-after-setparent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!