C# 世界坐标到UI坐标,反之亦然?
我有画布在屏幕空间覆盖渲染模式。所以我想要的是当用户触摸屏幕时,一些图像必须出现在用户触摸屏幕的位置。我的图像应该在UI层。如果我的画布处于屏幕空间相机渲染模式,我会通过以下方式获得结果C# 世界坐标到UI坐标,反之亦然?,c#,user-interface,unity3d,C#,User Interface,Unity3d,我有画布在屏幕空间覆盖渲染模式。所以我想要的是当用户触摸屏幕时,一些图像必须出现在用户触摸屏幕的位置。我的图像应该在UI层。如果我的画布处于屏幕空间相机渲染模式,我会通过以下方式获得结果 Image img = null // I assign it via the inspector void Update() { if(Input.GetMouseButtonDown(0)) { Vector2 point; RectTransformUti
Image img = null // I assign it via the inspector
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Vector2 point;
RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)img.rectTransform.parent,
Input.mousePosition, canvasCamera, out point);
img.rectTransform.anchorPosition = point;
}
}
但是我想在屏幕空间覆盖渲染模式中实现相同的结果,因为我有很多UI组件不适合屏幕。请帮帮我
[SerializeField] private Image image = null;
void Update()
{
image.position = Input.mousePosition;
}
这将使图像跟随鼠标指针。轴心点定义图像在鼠标指针正下方的位置。我不希望图像跟随鼠标指针,我只想将图像放置在画布中检测到触摸的位置。我显示的是,您不需要转换任何内容即可获取UI对象鼠标的位置。只需将它放在您显示的输入代码中,这就应该完成您所要做的工作,即将UI项放在发生单击的位置