2025-06-19 18:23:31 +08:00

74 lines
1.9 KiB
C#

using F3Device.Screen;
using QFramework;
using QFramework.Example;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UIImageDrag_JiHe : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
RectTransform rect;
Canvas canvas; // 用于坐标转换
List<RaycastResult> raycastResults;
GameObject target;
public Vector3 originPos;
bool isOverTarget = false; // 是否在目标物体上结束拖拽
void Awake()
{
rect = GetComponent<RectTransform>();
canvas = GetComponentInParent<Canvas>(); // 获取父级Canvas
originPos = rect.transform.localPosition;
raycastResults = new List<RaycastResult>();
}
public void OnBeginDrag(PointerEventData eventData)
{
}
public void OnDrag(PointerEventData eventData)
{
rect.transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
var uiPanel = UIKit.GetPanel<UIImageSelectMap_JiHe>();
if (uiPanel == null) return;
// 获取目标对象名称
string targetObjName = uiPanel.Data.TargetObj;
// 判断当前拖拽的物体是否与目标物体匹配
if (isOverTarget && target != null && target.name == targetObjName)
{
Debug.Log("成功放置到目标物体: " + targetObjName);
// 可以在这里执行成功放置的逻辑
// 例如:将当前物体位置固定到目标位置
transform.position = target.transform.position;
// 通知UI面板操作成功
// uiPanel.OnDragSuccess();
}
else
{
Debug.Log("未放置到正确目标,返回原位");
Debug.Log(originPos+"??????");
// 未放置到正确目标,返回原始位置
rect.anchoredPosition = Vector3.zero;
}
// 重置状态
isOverTarget = false;
target = null;
}
}