一、普通拖拽
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);
}
}
}