1.首先创建2个Image
白色为Drag(拖拽)红色为Drop(放置)
2.创建 C# UIDrag 脚本
这个放在白色Image上面
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class UIDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
private GameObject go;
public void OnBeginDrag(PointerEventData eventData)
{
go = this.gameObject;
go.transform.SetParent(this.transform);
var image = go.GetComponent<Image>();
var group = go.AddComponent<CanvasGroup>();
group.blocksRaycasts = false;
image.sprite = GetComponent<Image>().sprite;
image.SetNativeSize();
}
public void OnDrag(PointerEventData eventData)
{
if (go != null)
go.transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
//Destroy(go);
//go = null;
}
}
3.创建 C# UIDrop 脚本
这个放在红色Image上面
using UnityEngine;
using UnityEngine.EventSystems;
public class UIDrop : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData data)
{
print(111);
}
}
-------------------------------------分割线------------------------------------
最后触发事件: