150 lines
4.0 KiB
C#
150 lines
4.0 KiB
C#
using F3Device.Screen;
|
||
using QFramework;
|
||
using QFramework.Example;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.UI;
|
||
using static UnityEngine.GraphicsBuffer;
|
||
|
||
/// <summary>
|
||
/// 通用拖拽组件
|
||
/// </summary>
|
||
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<RectTransform>();
|
||
canvas = GetComponentInParent<Canvas>();
|
||
graphicRaycaster = canvas.GetComponent<GraphicRaycaster>();
|
||
startPosition = rectTransform.anchoredPosition;
|
||
parentLayoutGroup = GetComponentInParent<LayoutGroup>();
|
||
|
||
raycastResults = new List<RaycastResult>();
|
||
IsFirst = true;
|
||
}
|
||
|
||
public void OnBeginDrag(PointerEventData eventData)
|
||
{
|
||
if (parentLayoutGroup != null)
|
||
{
|
||
parentLayoutGroup.enabled = false;
|
||
|
||
if (parentLayoutGroup.GetComponent<ContentSizeFitter>() != null)
|
||
parentLayoutGroup.GetComponent<ContentSizeFitter>().enabled = false;
|
||
}
|
||
|
||
startIndex = transform.GetSiblingIndex();
|
||
}
|
||
|
||
public void OnDrag(PointerEventData eventData)
|
||
{
|
||
rectTransform.transform.position = Input.mousePosition;
|
||
}
|
||
|
||
bool isRight;
|
||
List<RaycastResult> 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<GraphicRaycaster>();
|
||
if (graphicRaycaster != null)
|
||
{
|
||
graphicRaycaster.enabled = false;
|
||
}
|
||
|
||
// 或者禁用Image组件的raycastTarget
|
||
Image image = GetComponent<Image>();
|
||
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<ContentSizeFitter>() != null)
|
||
parentLayoutGroup.GetComponent<ContentSizeFitter>().enabled = true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
IsFirst = false;
|
||
ResetPosition(); Debug.Log("放置错误,不得分");
|
||
var data = new UIResultTipData();
|
||
data.label = $"放置错误,请重新放置!";
|
||
data.isRight = false;
|
||
data.autoHideTime = 0.5f;
|
||
UIKit.OpenPanelAsync<UIResultTip>(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<ContentSizeFitter>() != null)
|
||
parentLayoutGroup.GetComponent<ContentSizeFitter>().enabled = true;
|
||
}
|
||
|
||
}
|
||
} |