鼠标事件总结

触发的每个UI上都要挂上相应的组件。

鼠标事件接口:
1.鼠标的移入,移出事件要继承实现IPointerEnterHandler,IPointerExitHandler这2个接口

public class UImanager : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
       //实现功能
        print("进入");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        //实现功能
        print("移出");
    }
 }

2.鼠标的按下、抬起、点击要实现IPointerDownHandler,IPointerClickHandler,IPointerUpHandler

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

public class UIinfor : MonoBehaviour,IPointerDownHandler,IPointerClickHandler,IPointerUpHandler
{
    bool  _isPress;//用来判断鼠标是按下还是抬起
    float timer = 0;
    float downTime;
    float upTime;

    //鼠标按下和抬起2个工作都完成了才进行,但是几乎是和抬起一起完成的
    public void OnPointerClick(PointerEventData eventData)
    {
        _isPress = true;
        timer = Time.time;
        print("点击的时间:"+Time.time);
        print("OnPointerClick:``````");
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        _isPress = false;
        downTime = Time.time;
        print("按下的时间:" + Time.time);
        print("OnPointerDown:``````");
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        upTime = Time.time;
        print("抬起的时间:" + Time.time);
        print("OnPointerUp:``````");
    }
  //在Update中实现要表现的功能
  void Update()
  {
      if(_isPress==true)
      {
       //实现功能
      }
      if(_isPress==false)
      {
       //实现功能
      }
  }
 }

你可能感兴趣的:(鼠标操作,鼠标事件接口,鼠标,继承)