返回与鼠标单击、游戏手柄按钮按下或屏幕触摸相关联的 RaycastResult。
当游戏请求玩家按下详情时,PointerEventData.pointerPressRaycast 返回 RaycastResult 命中结果。
以下示例使用 IDragHandler
、IDragHandler
和 IEndDragHandler
接口。这些接口分别提供 OnDrag
、OnDrag
和 OnEndDrag
事件处理程序。当射线投射命中世界空间中的某个点时,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; } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.