Version: 2019.1
public EventSystems.RaycastResult pointerPressRaycast ;

説明

Returns the RaycastResult associated with a mouse click, gamepad button press or screen touch.

PointerEventData.pointerPressRaycast returns the RaycastResult hit result when the game requests the player press details.

The example below uses the IBeginDragHandler, IDragHandler, and IEndDragHandler interfaces. They provide OnBeginDrag, OnDrag, and OnEndDrag event handlers respectively. OnBeginDrag queries when a raycast hits a point in World space. OnBeginDrag sends a message to PointerEventData.pointerPressRaycast.worldPosition. The code example then reports the screen position when OnBeginDrag starts.

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