using F3Device.Screen; using QFramework; using QFramework.Example; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using static UnityEngine.GraphicsBuffer; /// /// 通用拖拽组件 /// public class DragUIItem : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler { public string targetName; // 目标物体名称 private RectTransform rectTransform; private Canvas canvas; private Vector2 startPosition; private LayoutGroup parentLayoutGroup; private GraphicRaycaster graphicRaycaster; private int startIndex; public bool IsFirst = true; private void Awake() { rectTransform = GetComponent(); canvas = GetComponentInParent(); graphicRaycaster = canvas.GetComponent(); startPosition = rectTransform.anchoredPosition; parentLayoutGroup = GetComponentInParent(); raycastResults = new List(); IsFirst = true; } public void OnBeginDrag(PointerEventData eventData) { if (parentLayoutGroup != null) { parentLayoutGroup.enabled = false; if (parentLayoutGroup.GetComponent() != null) parentLayoutGroup.GetComponent().enabled = false; } startIndex = transform.GetSiblingIndex(); } public void OnDrag(PointerEventData eventData) { rectTransform.transform.position = Input.mousePosition; } bool isRight; List raycastResults; private GameObject target; public void OnEndDrag(PointerEventData eventData) { raycastResults.Clear(); EventSystem.current.RaycastAll(eventData, raycastResults); target = null; foreach (var result in raycastResults) { if (result.gameObject.name == targetName) { target = result.gameObject; break; } } if (target != null) { if (IsFirst) { ScoreController.Instance.Add("三维构造考核", 1); Debug.Log("放置正确,得1分,禁用拖拽"); } else { Debug.Log("放置正确,但不是第一次,不得分!"); } // 禁用射线检测,防止再次被拖拽 GraphicRaycaster graphicRaycaster = GetComponent(); if (graphicRaycaster != null) { graphicRaycaster.enabled = false; } // 或者禁用Image组件的raycastTarget Image image = GetComponent(); if (image != null) { image.raycastTarget = false; } gameObject.SetActive(false); target.gameObject.transform.Find("Text (Legacy)").gameObject.SetActive(true); if (parentLayoutGroup != null) { parentLayoutGroup.enabled = true; if (parentLayoutGroup.GetComponent() != null) parentLayoutGroup.GetComponent().enabled = true; } } else { IsFirst = false; ResetPosition(); Debug.Log("放置错误,不得分"); var data = new UIResultTipData(); data.label = $"放置错误,请重新放置!"; data.isRight = false; data.autoHideTime = 0.5f; UIKit.OpenPanelAsync(uiData: data, canvasLevel: UILevel.PopUI).ToAction().Start(this); } target = null; } // 重置位置 private void ResetPosition() { transform.SetSiblingIndex(startIndex); if (parentLayoutGroup != null) { parentLayoutGroup.enabled = true; if (parentLayoutGroup.GetComponent() != null) parentLayoutGroup.GetComponent().enabled = true; } } }