c# - 更改 UI RectTransform 位置

标签 c# unity3d position

我有这些行:

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

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

我的问题是,函数 SetParent 为我的 bp GameObject 设置了一个固定位置,之后我不知道如何更改这个位置。最后一行没有任何改变......与 .position 相同。

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

编辑:

ContentFactory 检查员:

enter image description here

bp 的检查员:

enter image description here

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

enter image description here

最佳答案

使用 transform.localPosition将使用它的相对位置。如果你想改变世界空间的位置,你需要使用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 移动它。

它应该移动:

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

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

请注意,在使用 RectTransform 时还有其他因素在发挥作用.

这包括 anchorMaxanchorMin可以修改为:

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

关于c# - 更改 UI RectTransform 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44186212/

相关文章:

jquery - 按位置对元素进行分组

css - 居中内联 block - Chrome 在窗口调整大小后忽略 css 顶部位置,BUG?

c# - 使用结构的子集

c# - 管理 NuGet 包将 jquery 恢复到旧版本

c++ - 我可以将 visual studio 2013 与 unity 5 一起使用吗

ios - touchesMoved 不定期调用

c# - 在 c# 中的 grideview + 模板上设置交替行样式

c# - 将组合框添加到 WinForms 中的 GridView

android - 如何链接到 GooglePlay 来评价游戏(Unity3d)?

css - HTML 和 CSS : 2 DIVS on left, 右侧 1 个独立的 DIV