【问题标题】:unity3d - raycasting world space UIunity3d - 光线投射世界空间 UI
【发布时间】:2016-11-01 20:31:08
【问题描述】:

我需要一些有关使用 Daydream 进行光线投射 UI 的帮助。由于只有游戏对象(立方体)的演示,我想知道我可以处理 UI 元素。 UI 元素应该像往常一样做出反应,我的意思是突出显示等等。 How to use Graphic Raycaster with WorldSpace UI? 中的帖子很有帮助,但完全有用。

我使用 DrawRay 来查看我的指针实际经过的位置,并且效果很好。但是没有创建日志消息。

 void Update()
  {
      Quaternion ori = GvrController.Orientation;
      Vector3 vector = ori * Vector3.forward;
      Debug.DrawRay(transform.position, vector * 100000, Color.green);
      PointerEventData pointerData = new PointerEventData(null);
      pointerData.position = vector;
      Debug.Log(vector);
      List<RaycastResult> results = new List<RaycastResult>();
      EventSystem.current.RaycastAll(pointerData, results);
      if (results.Count > 0)
      {
          Debug.Log(results[0]);
      }
  }

画布(世界空间)和按钮被创建为有用的而不是修改的。

【问题讨论】:

    标签: unity3d raycasting virtual-reality daydream


    【解决方案1】:

    在你的情况下,我看到了两个问题:

    • 我认为你应该使用PointerEventData pointerData = new PointerEventData(EventSystem.current); insted of PointerEventData pointerData = new PointerEventData(null);
    • 另外,PointerEventData.positionVector2 而不是 Vector3 :您应该尝试 pointerData.position = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);

    作为旁注,您可以使用graphicRaycaster.Raycast(pointerData, results);,其中 graphicRaycaster 是公开的,并且您将其分配给您 CanvasGraphicRaycaster 组件。

    【讨论】:

    • 感谢@Kardux 的提示。但不幸的是,它不起作用。 1. Vector2Screen 的东西应该如何工作?我不需要屏幕尺寸,而是来自控制器的方向。 vector 输出是 (-0.5, 0.0, 0.9) 和 pointerData.position 是 (-0.5, 0.0) - 可以吗? 2. 我将 GraphicRaycaster 组件添加到我的父 Canvas 并使用了public GraphicRaycaster graphicRaycaster,但根本没有登录条目......还有其他想法吗?我需要 GUICamera 吗?
    • @fmielke 我对 Vector2 的事情(包括屏幕尺寸)的不好:在我的情况下,我总是使用“屏幕”的中间来对 UI 进行光线投射(这样用户只需查看 UI 以激活它)。通过给 Vector2 一个 Vector3 值,您根本不考虑 z 值(这就是您得到 (-0.5, 0.0) 的原因) .在您的情况下,我通常会使用 Physics.Raycast 对放置在 UI 所在的“鬼墙”进行光线投射,然后使用 Camera.WorldToScreenPoint 获取屏幕 xy 坐标设置pointerData.position.
    • 我实际上“只是”需要来自 RaycastHit hitInfo; if (Physics.Raycast(Vector3.zero, vector, out hitInfo)) { Debug.Log("HIT a gameObject"); } 的游戏对象行为,但仅适用于 UI :)
    【解决方案2】:

    这个有用的方法怎么样:

    Vector3 CurrentRaycastResultWorldPosition = GvrPointerInputModule.Pointer.CurrentRaycastResult.worldPosition;         
    

    【讨论】:

      【解决方案3】:

      根据您的问题 raycasting world space UI ,如果您使用的是 google cardbard 或 daydream,那么您应该尝试将脚本 gvrgraphicphysics raycaster 附加到 UI 画布。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-03
        • 2019-01-26
        • 1970-01-01
        • 1970-01-01
        • 2020-06-18
        • 1970-01-01
        • 2021-03-17
        • 1970-01-01
        相关资源
        最近更新 更多