Unity 给3D物体添加事件(使用EventSystem)

  首先,说一下什么是EventSystem:

官网文档:

第一步:在场景中挂载  EventSystem组件和射线检测组件physics raycaster

如下:

physics raycaster组件要挂载在摄像机下面:

第二步:创建一个cube物体并对物体挂上代码(CubeTrigger):

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

public class CubeTrigger : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
    /// <summary>
    /// 目标对象的屏幕坐标
    /// </summary>
    private Vector3 targetScreenPoint;
    /// <summary>
    /// 获得鼠标的位置和cube位置差
    /// </summary>
    private Vector3 offset;

    private bool isDrag = false;

    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("开始拖拽!!");
      
    }

    public void OnDrag(PointerEventData eventData)
    {
        //Debug.Log("拖拽中!!");
        if (CheckGameObject())
        {
            var pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenPoint.z));
            offset = this.transform.position - pos;
        }
        if (isDrag)
        {
            //当前鼠标所在的屏幕坐标
            Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenPoint.z);
            //把当前鼠标的屏幕坐标转换成世界坐标
            Vector3 curWorldPoint = Camera.main.ScreenToWorldPoint(curScreenPoint);
            Debug.Log(curWorldPoint  +  " :"+ offset);
            
            this.transform.position = curWorldPoint + offset;
            Debug.Log(this.transform.position);
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
        Debug.Log("拖拽结束!!");
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("点击!!");
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("移入!!");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("移出!!");
    }

   
    /// <summary>
    /// 检查是否点击到cbue
    /// </summary>
    /// <returns></returns>
    bool CheckGameObject()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, LayerMask.GetMask("Cube")))
        {
            isDrag = true; Debug.DrawLine(ray.origin, hitInfo.point);
            //得到射线碰撞到的物体
            targetScreenPoint = Camera.main.WorldToScreenPoint(this.transform.position);
            return true;
        }
        return false;
    }
}

这样就实现了对物体的所有事件了。

三、实现时遇到的问题:

在鼠标拖拽时我一开始是这样实现的

    /// <summary>
    /// 拖拽中
    /// </summary>
    void IDragHandler.OnDrag(PointerEventData eventData)
    {

            //XXX:这种方式在3d物体中存在Bug,已用写在Update里面做替代
            //RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("POI3D")))
            {
                var pos = CreateMap3DPOI.Instance.parent.transform.InverseTransformPoint(hit.point);
                transform.localPosition = new Vector3(pos.x, 1.2f, pos.z);
            }
            if (base.OnDrag != null) base.OnDrag(this);
    }

这样遇到的问题是,一旦拖拽过快,鼠标脱离物理是,拖拽事件消失,于是就有了最上面代码中的解决方式,

当然还有另一种解决方式——在Update中用射线检测,如下:

 void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (CheckGameObject())
            {
                var pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenPoint.z));
                offset = this.transform.position - pos;
            }

        }

        if (isDrag)
        {
            //当前鼠标所在的屏幕坐标
            Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenPoint.z);
            //把当前鼠标的屏幕坐标转换成世界坐标
            Vector3 curWorldPoint = Camera.main.ScreenToWorldPoint(curScreenPoint);
            //    var pos = parent.transform.InverseTransformPoint(curWorldPoint);
            this.transform.localPosition = curWorldPoint + offset;
        }

        if (Input.GetMouseButtonUp(0))
        {
            isDrag = false;
        }
    }

其基本原理是利用屏幕坐标偏移量转世界坐标的偏移量做位移。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值