gpt4 book ai didi

c# - 如何将 Graphic Raycaster 与 WorldSpace UI 结合使用?

转载 作者:行者123 更新时间:2023-12-02 17:06:03 26 4
gpt4 key购买 nike

我试图弄清楚 Graphic.Raycaster 是如何工作的,但文档没有帮助。我想用它从某个位置以某个角度转换光线并点击 UI。另一件事是我不知道如何让它与 UI 交互(拖动、单击等)。我知道这是一个广泛的主题,但我找不到任何关于如何使用它的好的解释,所以我将不胜感激任何解释。

最佳答案

来自 Unity 文档:

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针对图形(UI)元素进行光线转换。

这是您的案例的一个简短示例:

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();
}
}
}

以上脚本未经本人测试。所以可能会有一些错误。

以下是一些其他引用资料,可帮助您了解更多信息:

  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

希望有帮助。

关于c# - 如何将 Graphic Raycaster 与 WorldSpace UI 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39087609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com