Files
Studio/Assets/Scripts/XED/UI/Elements/UI_DragDrop.cs
2025-02-24 09:32:17 +09:00

60 lines
1.7 KiB
C#

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.UI.Extensions;
using XRLib.UI;
using static DrawLine;
namespace XED.UI
{
public class UI_DragDrop : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler,IDropHandler
{
protected UI_DragDrop Clone;
public float cloneAlpha=1f;
public Action<UIBase, UIBase> OnDropItem;
public virtual void OnBeginDrag(PointerEventData eventData)
{
CreateClone();
}
public virtual void Clear()
{
}
UI_DragDrop CreateClone()
{
Clone = Instantiate(gameObject, gameObject.transform.parent).GetComponent<UI_DragDrop>();
var CI = Clone.GetComponentsInChildren<Image>();
foreach (var c in CI)
{
c.color = new Color(c.color.r, c.color.g, c.color.b, cloneAlpha);
}
return Clone;
}
public virtual void OnDrag(PointerEventData eventData)
{
Clone.transform.position = Input.mousePosition;
}
public virtual void OnEndDrag(PointerEventData eventData)
{
gameObject.transform.position = Clone.transform.position;
Destroy(Clone.gameObject);
}
public virtual void OnDrop(PointerEventData eventData)
{
if (eventData.pointerDrag != null)
{
UI_DragDrop draggedItem = eventData.pointerDrag.GetComponent<UI_DragDrop>();
if (draggedItem)
{
OnDropItem?.Invoke(gameObject.GetComponent<UIBase>(), draggedItem.GetComponent<UIBase>());
}
}
}
}
}