我在使用Unity的UI rectTransform编程时发现了一个非常令人困惑的问题:
在检查器中,我们可以发现RectTransform的位置被设置为(0,0,0)
但是在Start()
函数中的代码中,当我打印位置时:
parentObject = transform.parent.gameObject;
Debug.Log("parentObject rectTransform :" + parentObject.GetComponent<RectTransform>().localPosition);
这将给我一个位置:
parentObject rectTransform :Panel: (0.0, -562.5, 0.0)
UnityEngine.Debug:Log(Object)
NewWorkerController:Start() (at Assets/Scripts/NewWorkerController.cs:60)
当我们尝试获取position
而不是localPosition
时,事情会变得更加混乱:
parentObject rectTransform :Panel: (621.0, 1365.5, 0.0)
UnityEngine.Debug:Log(Object)
NewWorkerController:Start() (at Assets/Scripts/NewWorkerController.cs:60)
我在playMode模式下检查了该rectTransform的posX和posY,发现它们没有被改变(一直显示为(0,0,0)
)。
出了什么问题?
GameObject
上? - SmartisPanel
的子节点newWorkPlace
上。 - armnotstrong.GetComponent<RectTransform>().position
而不是.GetComponent<RectTransform>().localPosition
。 - Smartisposition
将会得到一个更奇怪的结果。 - armnotstrong