RectTransformUtility是RectTransform的一个辅助类
上代码:
public RectTransform UI_Canvas;
float temp_z;
Vector3 mousPos;
private void FixedUpdate()
{
RectTransformUtility.ScreenPointToWorldPointInRectangle(
UI_Canvas, new Vector2(Input.mousePosition.x, Input.mousePosition.y), Camera.main, out mousPos);
if (mousPos.x > transform.position.x)
{
temp_z = -Vector3.Angle(Vector3.up, mousPos - transform.position);
}
else
{
temp_z = Vector3.Angle(Vector3.up, (mousPos - transform.position));
}
transform.localRotation = Quaternion.Euler(0, 0, temp_z);
}
UI_Canvas : 当前界面的Canvas
满足当前设置
PS:之前用Camera.main.XXX的时侯出了个Bug,,不管我怎么调整 它打印的都是当前相机的世界坐标,着急的时候发现了这个,等到后面复查的时候才发现是传值传错了... ...