Files
aibis-dream/Assets/Scripts/FixSystem/DraggableUI.cs
T

54 lines
1.4 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DraggableUI : MonoBehaviour,IDragHandler, IPointerDownHandler
{
private RectTransform rectTransform;
private Canvas canvas;
public RectTransform target;
public GameObject button;
public bool isDone=false;
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
canvas = GetComponentInParent<Canvas>();
}
public void OnPointerDown(PointerEventData eventData)
{
// 这里可以添加按下时的逻辑,比如记录初始位置
}
public void OnDrag(PointerEventData eventData)
{
Vector3 screenPosition = new Vector3(eventData.position.x, eventData.position.y,
Camera.main.WorldToScreenPoint(transform.position).z);
Vector3 move = Camera.main.ScreenToWorldPoint(screenPosition);
move.z = transform.position.z;
transform.position = move;
//plugSprite.enabled = true;
}
void Update()
{
// 计算两个物体之间的距离
float distance = Vector3.Distance(transform.position, target.position);
// 检查距离是否在范围内,并激活或禁用 Button
if (distance <= 1&&!isDone)
{
button.SetActive(true);
}
else
{
button.SetActive(false);
}
}
}