六、输入控制:2、使用事件接口

一、拖拽

1、先让MonoBehaviour继承接口IDragHandler,IBeginDragHandler, IEndDragHandler,并实现接口。
2、利用接口中传递出来的PointerEventData数据。
3、对PointerEventData进行处理可以判断方向,也可以得出拖拽的两从而控制其他事务。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public enum E_DragDir
{
    None,
    Right,
    Left,
    Down,
    Up
}
public class DragItem : MonoBehaviour,IDragHandler,IBeginDragHandler, IEndDragHandler
{
    
    /// 
    /// 开始触摸的手指的位置
    /// 
    private Vector3 _startTouchPos;
    private PointerEventData _pointerEventData;
    /// 
    /// 拖拽的方向
    /// 
    private E_DragDir _dragDir;

    private VBR_CardInfoWindow vBR_CardInfoWindow;
    void Start()
    {
        vBR_CardInfoWindow = PanelMgr.Instance.GetPanel(PanelType.CardInfoWindow);
    }


    public void OnEndDrag(PointerEventData baseEventData)
    {
        //Debug.Log("OnEndDrag");
        _pointerEventData = baseEventData;
        if (_pointerEventData.position.x > _startTouchPos.x)
        {
            //Debug.Log("往右边滑动");
            _dragDir = E_DragDir.Right;
            if (vBR_CardInfoWindow != null)
            {
                Debug.Log("lgy 往右边滑动");
                vBR_CardInfoWindow.DisPlayBigPic(_dragDir);
            }
            else
            {
                Debug.Log("lgy vBR_CardInfoWindow == null");
                vBR_CardInfoWindow = PanelMgr.Instance.GetPanel(PanelType.CardInfoWindow);
            }
        }
        else if (_pointerEventData.position.x < _startTouchPos.x)
        {
            //Debug.Log("往左边滑动");
            _dragDir = E_DragDir.Left;
            if (vBR_CardInfoWindow != null)
            {
                vBR_CardInfoWindow.DisPlayBigPic(_dragDir);
            }
            else
                vBR_CardInfoWindow = PanelMgr.Instance.GetPanel(PanelType.CardInfoWindow);
        }
        //else if (_pointerEventData.position.y < _startTouchPos.y)
        //{
        //    Debug.Log("往下面滑动");
        //    _dragDir = E_DragDir.Down;
        //}
        //else if (_pointerEventData.position.y > _startTouchPos.y)
        //{
        //    Debug.Log("往上面滑动");
        //    _dragDir = E_DragDir.Up;

        //}

        _dragDir = E_DragDir.None;
    }

    public void OnDrag(PointerEventData baseEventData)
    {

    }

    public void OnBeginDrag(PointerEventData baseEventData)
    {
        _startTouchPos = baseEventData.position;
    }

    /// 
    /// 得到滑动的方向
    /// 
    /// 
    public E_DragDir GetDragDir()
    {
        return _dragDir;
    }

    
}

你可能感兴趣的:(六、输入控制:2、使用事件接口)