如何在WorldSpace UI中使用Graphic Raycaster? [英] How to use Graphic Raycaster with WorldSpace UI?

查看:242
本文介绍了如何在WorldSpace UI中使用Graphic Raycaster?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚Graphic.Raycaster的工作原理,但文档无济于事.我想用它从某个位置以某个角度投射光线投射并点击UI.另一件事是我不知道如何使其与UI交互(拖动,单击等).我知道这是一个广泛的主题,但我只是找不到关于如何使用它的任何好方法,所以我将不胜感激.

I'm trying to figure out how Graphic.Raycaster works, but documentation doesn't help. I want to use it to cast raycast from some position at a certain angle and hit the UI. The other thing is that I don't know how to make it interact with the UI(drag, click etc.). I know that's broad subject, but I just can't find any good explanation of how to use it, so I would be grateful for any explanation.

推荐答案

来自Unity文档:

图形Raycaster用于对Canvas进行光线投射.这 Raycaster会查看画布上的所有图形,并确定是否有任何图形 他们被打了.

The Graphic Raycaster is used to raycast against a Canvas. The Raycaster looks at all Graphics on the canvas and determines if any of them have been hit.

您可以对图形使用 EventSystem.RaycastAll raycast (UI)元素.

You can use EventSystem.RaycastAll to raycast against graphics(UI) elements.

以下是您的案例的简短示例:

Here is a short example for your case:

void Update() {


// Example: get controller's current orientation:
  Quaternion ori = GvrController.Orientation;

  // If you want a vector that points in the direction of the controller
  // you can just multiply this quat by Vector3.forward:
  Vector3 vector = ori * Vector3.forward;

  // ...or you can just change the rotation of some entity on your scene
  // (e.g. the player's arm) to match the controller's orientation
  playerArmObject.transform.localRotation = ori;

  // Example: check if touchpad was just touched
  if (GvrController.TouchDown) {
    // Do something.
    // TouchDown is true for 1 frame after touchpad is touched.

    PointerEventData pointerData = new PointerEventData(EventSystem.current);

    pointerData.position = Input.mousePosition; // use the position from controller as start of raycast instead of mousePosition.

    List<RaycastResult> results = new List<RaycastResult>();
    EventSystem.current.RaycastAll(pointerData, results);

    if (results.Count > 0) {
         //WorldUI is my layer name
         if (results[0].gameObject.layer == LayerMask.NameToLayer("WorldUI")){ 
         string dbg = "Root Element: {0} \n GrandChild Element: {1}";
         Debug.Log(string.Format(dbg, results[results.Count-1].gameObject.name,results[0].gameObject.name));
         //Debug.Log("Root Element: "+results[results.Count-1].gameObject.name);
         //Debug.Log("GrandChild Element: "+results[0].gameObject.name);
          results.Clear();
     }
   }
}

上面的脚本未经我本人测试.因此可能会有一些错误.

The above script is not tested by myself. So there might be some errors.

还有其他一些参考资料可帮助您了解更多信息:

Here are some other references to help you understand more:

  1. Unity图形Raycaster;如何运作?
  2. 针对世界空间中的UI进行广播
  3. 如何从任意屏幕/画布位置对uGUI对象进行光线投射
  4. 如何执行图形处理射线广播?
  5. GraphicRaycaster
  1. Graphics Raycaster of Unity; How does it work?
  2. Raycast against UI in world space
  3. How to raycast against uGUI objects from an arbitrary screen/canvas position
  4. How do you perform a Graphic Raycast?
  5. GraphicRaycaster

希望有帮助.

这篇关于如何在WorldSpace UI中使用Graphic Raycaster?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆