更改 UI RectTransform 位置 [英] Change UI RectTransform position

查看:55
本文介绍了更改 UI RectTransform 位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些行:

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

Si,我只是实例化了一个预制件,我设置了一个父级,我想改变位置.

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

我的问题是,函数SetParent 为我的bp GameObject 设置了一个固定位置,之后我不知道如何更改这个位置.最后一行什么都没有改变......与.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.

如何改变 bp 的位置?谢谢!

How can I change the position of bp ? Thanks !

ContentFactory 的检查员:

Inspector of ContentFactory :

一个 bp 的检查员:

Inspector of a bp :

层次结构(蓝色内容是 ContentFactory):

Hierarchy (Content in blue is ContentFactory) :

推荐答案

使用 transform.localPosition 将使用它的相对位置.如果你想改变世界空间中的位置,你需要使用transform.position.

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);

这是一个带有 RectTransform 的 UI 对象.你不应该用transform.position 移动它.

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

它应该被移动:

bp.GetComponent().anchoredPosition = ...

bp.GetComponent().anchoredPosition3D = ...

请注意,在使用 RectTransform 时,还有其他一些因素在起作用.

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

这包括 anchorMaxanchorMin 可以修改:

This includes anchorMax and anchorMin which can be modified with:

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

这篇关于更改 UI RectTransform 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆