繁体   English   中英

Unity3D中UI项目的Raycast替代方案

[英]Alternative of Raycast for UI Items in Unity3D

我正在为Unity3D中的VR项目实现注视算法。 到目前为止,我在这里发现的是使用Physics.Raycast并在每个按钮对象中添加BoxCollider2D组件以检测用户是否正在查看它。 我想避免使用Raycast,因为它很昂贵并且可能会产生性能问题,因为我有非常复杂的UI结构来处理。

我尝试使用光标锁但不幸的是它不适用于移动平台。

我也尝试使用相机视口中心来检查按钮对象的rect是否包含由创建的世界点

Camera.main.ViewportToWorldPoint(new Vector3(0.5f,0.5f,Camera.main.nearClipPlane));

当按钮对象的rect变换锚定为中间中心时,它工作正常。 但是当它有一些其他设置或按钮稍微倾斜时(在世界空间画布中)不起作用。 而且我对矩形变换的工作方式以及如何在世界空间中找到它的确切位置并不熟悉。

有谁知道解决方案? 或者还有其他方法吗?

所有的帮助都非常感谢。

我想出了问题,它是z轴引起问题的不同值。 以下解决方案工作正常,不使用Physics.Raycast

void Update () 
{
    pos = Camera.main.ViewportToWorldPoint(new Vector3(0.5f,0.5f,Vector3.Distance(transform.position,Camera.main.transform.position)));
    _rect = new Rect(pos.x-size,pos.y-size,size,size);

    _rect2 = new Rect(transform.position.x-rt.rect.width/2,transform.position.y-rt.rect.height/2,rt.rect.width,rt.rect.height);

    Hit = _rect2.Overlaps(_rect);

    if(Hit)
    {
        if (!isEntered)
        {
            _button.OnPointerEnter(new PointerEventData(EventSystem.current));
        }
        isEntered = true;

        timeElapsed += Time.deltaTime;
        if(timeElapsed >= 3) // autoclick after 3 sec.
        {
            timeElapsed = 0;
            _button.onClick.Invoke();
            isEntered = false;
        }
    }
    else
    {
        timeElapsed = 0;
        if (isEntered)
        {
            _button.OnPointerExit(new PointerEventData(EventSystem.current));
        }
        isEntered = false;

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM