【问题标题】:world coordinates to UI cordinates and vice versa?世界坐标到 UI 坐标,反之亦然?
【发布时间】:2015-12-18 11:03:16
【问题描述】:

我的画布处于屏幕空间覆盖渲染模式。所以我想要的是当用户触摸屏幕时,某些图像必须完全出现在用户触摸屏幕的相同位置。我的图像应该在 UI 层。如果我的画布处于屏幕空间相机渲染模式,我会通过以下方式实现结果..

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 组件。请帮帮我。

【问题讨论】:

    标签: c# user-interface unity3d


    【解决方案1】:
    [SerializeField] private Image image = null;
    void Update()
    {
        image.position = Input.mousePosition;
    }
    

    这将使图像跟随鼠标指针。枢轴点定义图像在鼠标指针正下方的位置。

    【讨论】:

    • 我不希望图像跟随鼠标指针我只想将图像放置在检测到触摸的画布中。
    • 我在那里展示的是,您无需转换任何内容即可将 UI 对象置于鼠标位置。只需将它放在您显示的输入代码中,这应该会执行您所追求的操作,即将 UI 项目放置在发生点击的位置
    猜你喜欢
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2015-08-10
    • 1970-01-01
    • 2012-04-10
    • 2017-05-26
    • 1970-01-01
    相关资源
    最近更新 更多