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

2022-08-15  本文已影响0人  GameObjectLgy
一、拖拽

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
{
    
    /// <summary>
    /// 开始触摸的手指的位置
    /// </summary>
    private Vector3 _startTouchPos;
    private PointerEventData _pointerEventData;
    /// <summary>
    /// 拖拽的方向
    /// </summary>
    private E_DragDir _dragDir;

    private VBR_CardInfoWindow vBR_CardInfoWindow;
    void Start()
    {
        vBR_CardInfoWindow = PanelMgr.Instance.GetPanel<VBR_CardInfoWindow>(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<VBR_CardInfoWindow>(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<VBR_CardInfoWindow>(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;
    }

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

    
}

上一篇下一篇

猜你喜欢

热点阅读