public EventSystems.RaycastResult pointerPressRaycast ;

描述

返回与鼠标单击、游戏手柄按钮按下或屏幕触摸相关联的 RaycastResult。

当游戏请求玩家按下详情时,PointerEventData.pointerPressRaycast 返回 RaycastResult 命中结果。

以下示例使用 IDragHandlerIDragHandlerIEndDragHandler 接口。这些接口分别提供 OnDragOnDragOnEndDrag 事件处理程序。当射线投射命中世界空间中的某个点时,OnBeginDrag 将进行查询。OnBeginDrag 将消息发送到 PointerEventData.pointerPressRaycast.worldPosition。然后代码示例报告 OnBeginDrag 启动时的屏幕位置。

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

// PointerEventData.pointerPressRaycast example

// A cube which can be moved by the mouse or track bar at runtime. // Up/down and left/right are supported. // // Create a 3D project and add a Cube. Position the Cube at the origin. Add a // PhysicsRaycaster component to the Main Camera. Also, add an Empty GameObject // to the Hierarchy. Apply an EventSystem component and a StandaloneInputModule // component to this Empty GameObject. Next create this script and assign it to // the Cube. Now, run the Game. The Cube can be moved up/down and left/right.

public class Example : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { private Vector3 newPosition; private Vector3 delta; private Vector3 startPosition;

void Awake() { // Move the Cube away from the origin. startPosition = new Vector3(0.25f, -0.5f, 3.0f); transform.position = startPosition;

newPosition = Vector3.zero; delta = Vector2.zero;

// Position the camera and switch to solid color. Camera.main.transform.position = new Vector3(0.5f, 1.0f, -3.0f); Camera.main.transform.localEulerAngles = new Vector3(15.0f, -20.0f, 0.0f); Camera.main.clearFlags = CameraClearFlags.SolidColor; }

public void OnBeginDrag(PointerEventData eventData) { Debug.Log("OnBeginDrag: " + eventData.pointerPressRaycast.screenPosition);

// Obtain the position of the hit GameObject. delta = eventData.pointerPressRaycast.worldPosition; delta = delta - transform.position; }

public void OnDrag(PointerEventData eventData) { newPosition = eventData.pointerCurrentRaycast.worldPosition - delta;

if (eventData.pointerCurrentRaycast.worldPosition.x == 0.0f || eventData.pointerCurrentRaycast.worldPosition.y == 0.0f) { newPosition = eventData.delta;

transform.Translate(newPosition * Time.deltaTime); return; }

transform.position = newPosition; }

public void OnEndDrag(PointerEventData eventData) { Debug.Log("OnEndDrag");

transform.position = startPosition; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961