UnityEngine.EventSystems常用事件系统 转载集合

IBeginDragHandler, IDragHandler, IEndDragHandler

1.三个拖拽事件相关接口
  * IBeginDragHandler: 开始拖拽事件处理器;开始拖拽的一瞬间触发。
  * IDragHandler: 拖拽中事件处理器;拖拽过程中持续触发。
  * IEndDragHandler: 结束拖拽事件处理器;拖拽结束的一瞬间触发。

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ItemDrag : MonoBehaviour ,IBeginDragHandler,IDragHandler,IEndDragHandler
{
    private RectTransform m_RT;
    void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
    {
        print("IBeginDragHandler.OnBeginDrag");
        gameObject.GetComponent<Transform>().position = Input.mousePosition;
        print("这是实现的拖拽开始接口");
    }

    void IDragHandler.OnDrag(PointerEventData eventData)
    {
        print("IDragHandler.OnDrag");
        //虽然用Input.mousePosition可以得到一个2D坐标,不过我们现在需要的是3D坐标,看下面
        //gameObject.GetComponent<Transform>().position = Input.mousePosition;
        
        //3D坐标获取方法
        Vector3 pos;
        m_RT = gameObject.GetComponent<RectTransform>();
        //屏幕坐标到世界坐标
        RectTransformUtility.ScreenPointToWorldPointInRectangle(m_RT,eventData.position,eventData.enterEventCamera,out pos);
        m_RT.position = pos;
        print("拖拽中……");
    }

    void IEndDragHandler.OnEndDrag(PointerEventData eventData)
    {
        print("IEndDragHandler.OnEndDrag");
        gameObject.GetComponent<Transform>().position = Input.mousePosition;
        print("实现的拖拽结束接口");
    }
}

原文出处

IDragHandler

public void OnDrag(PointerEventData eventData)
    {
        Vector2 vect = eventData.position - centerPOs;

        expectedHandle.position = centerPOs + vect.normalized * distance; //更新位置,中心点加上鼠标向量的归一值乘上距离

        angle = Vector2.Angle(Vector2.down, vect); //计算夹角
        if (vect.x < 0)
        {
            angle = 360 - angle;
        }
        this.transform.localEulerAngles = new Vector3(0, 0, angle);
    }

原文出处

IPointerEnterHandler, IPointerExitHandler

实现监控鼠标的进入和退出UI触发事件

using UnityEngine.EventSystems;
using UnityEngine;

public class UITrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("进入");
    }

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


原文出处

IsPointerOverGameObject

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

public class MouseExample : MonoBehaviour
{
    void Update()
    {
        // Check if the left mouse button was clicked
        if (Input.GetMouseButtonDown(0))
        {
            // Check if the mouse was clicked over a UI element
            if (EventSystem.current.IsPointerOverGameObject())
            {
                Debug.Log("Clicked on the UI");
            }
        }
        // Check if there is a touch
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            // Check if finger is over a UI element
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                Debug.Log("Touched the UI");
            }
        }
    }
}

原文出处

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值