更改 UI 矩形变换位置

Clé*_*aud 3 c# position unity-game-engine

我有这些行:

GameObject bp = Instantiate(MyPrefab);
bp.transform.SetParent(GameObject.FindGameObjectWithTag("ContentFactory").transform);
bp.transform.localPosition = new Vector3(0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

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

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

我怎样才能改变 的位置bp?谢谢 !

编辑 :

ContentFactory 的检查员:

在此输入图像描述

bp 检查员:

在此输入图像描述

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

在此输入图像描述

Pro*_*mer 7

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

GameObject bp = Instantiate(MyPrefab);
bp.transform.SetParent(GameObject.FindGameObjectWithTag("ContentFactory").transform);
bp.transform.position = new Vector3(0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

编辑:

这是一个带有RectTransform. 你不应该用transform.position移动它。

它应该被移动:

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

或者

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

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

这包括anchorMax并且anchorMin可以通过以下方式修改:

bp.GetComponent<RectTransform>().anchorMax = ...
bp.GetComponent<RectTransform>().anchorMin = ...
Run Code Online (Sandbox Code Playgroud)