我有这些行:
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 检查员:
bp 的检查员:
层次结构(蓝色内容是 ContentFactory):
最佳答案
使用 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
时还有其他因素在发挥作用.
这包括 anchorMax
和 anchorMin
可以修改为:
bp.GetComponent<RectTransform>().anchorMax = ...
bp.GetComponent<RectTransform>().anchorMin = ...
关于c# - 更改 UI RectTransform 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44186212/