Unity拖拽UI界面元素可区分点击和拖拽

一、普通拖拽

1.新建场景,在场景中创建一个UI的Panel。

2.创建脚本Drag.cs,将脚本附加到Panel上。

using UnityEngine;
using UnityEngine.EventSystems;

public class DragUI : MonoBehaviour,IDragHandler,IBeginDragHandler, IEndDragHandler, IPointerClickHandler
{    
    private Vector3 offset;//记录初始鼠标与元素的偏移量
    bool isDrag = false;//区分点击还是拖拽

    public void OnBeginDrag(PointerEventData eventData)
    {
        offset = transform.position - new Vector3(eventData.position.x, eventData.position.y, transform.position.z);
    }

    public void OnDrag(PointerEventData eventData)
    {
        isDrag = true;
        transform.position = offset + new Vector3(eventData.position.x, eventData.position.y, transform.position.z);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if(!isDrag)
        {
            //点击
        }
            
    }
}

一、拖拽排序

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

public class ItemOnShelf : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerClickHandler
{
    public int index;
    public Text textIndex;
    public Text textIp;

    bool isDrag = false;
    private Vector2 offset;
    private RectTransform rectTr;

    public delegate void OnDragEndHandle(int dragIndex, int dropIndex);
    public OnDragEndHandle OnDragEndHandler;

    private void Start()
    {
        rectTr = GetComponent<RectTransform>();
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        float dx = 1920.0f / Screen.width;
        float dy = 1080.0f / Screen.height;
        float ds = Mathf.Min(dx, dy);
        offset = rectTr.anchoredPosition - ds * eventData.position;

        Debug.Log("OnBeginDrag");
    }

    public void OnDrag(PointerEventData eventData)
    {
        float dx = 1920.0f / Screen.width;
        float dy = 1080.0f / Screen.height;
        float ds = Mathf.Min(dx, dy);

        if (eventData.position.x > 0 && eventData.position.x < Screen.width && eventData.position.y > 0 && eventData.position.y < Screen.height)
            rectTr.anchoredPosition = offset + ds * eventData.position;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
        GameObject target = eventData.pointerCurrentRaycast.gameObject;
        

        List<RaycastResult> raycastResults = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventData, raycastResults);
        if (raycastResults.Count > 0)
        {
            for(int i = 0; i < raycastResults.Count; i++)
            {
                ItemOnShelf itemOnShelf = raycastResults[i].gameObject.GetComponentInParent<ItemOnShelf>();
                if (itemOnShelf != null && !itemOnShelf.Equals(this))
                {
                    Debug.Log(itemOnShelf.textIp.text);

                    if (OnDragEndHandler != null && itemOnShelf != null)
                    {
                        OnDragEndHandler(index, itemOnShelf.index);
                    }
                    break;
                }
            }
        }
    }

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

    public void OnClick()
    {
        Debug.Log("OnClick");
    }
}
public class TargetsOnShelfMgr : MonoBehaviour
{
    public Transform content;
    public GameObject prefabItem;

    List<ItemOnShelf> _listItems = new List<ItemOnShelf>();
    // Start is called before the first frame update
    void Start()
    {
        OpenPanel();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void OpenPanel()
    {
        for(int i = 0; i < 10; i++)
        {
            GameObject Obj = Instantiate(prefabItem,content);

            ItemOnShelf itemOnShelf = Obj.GetComponent<ItemOnShelf>();
            itemOnShelf.OnDragEndHandler += DragSortItem;
            itemOnShelf.index = i;
            itemOnShelf.textIp.text = string.Format("device:{0}", i);
            //itemOnShelf.textIndex.text = i.ToString();

            _listItems.Add(itemOnShelf);
        }
    }

    public void DragSortItem(int dragIndex, int dropIndex)
    {
        ItemOnShelf dragItem =  _listItems[dragIndex];
        if(dragIndex < dropIndex)
        {
            _listItems.RemoveAt(dragIndex);
            _listItems.Insert(dropIndex - 1, dragItem);
        }
        else
        {
            _listItems.RemoveAt(dragIndex);
            _listItems.Insert(dropIndex, dragItem);
           
        }
        

        for (int i = 0; i < _listItems.Count; i++)
        {
            _listItems[i].index = i;
            _listItems[i].transform.SetSiblingIndex(i);
        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值