• unity 实现拖动ui填空,并判断对错


    参考:https://ask.csdn.net/questions/7971448

    根据自己的需求修改为如下代码

    使用过程中,出现拖动ui位置错误的情况,修改为使用 localPosition
    但是吸附到指定位置却需要用的position

        public class DragAndDrop : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
        {
            private RectTransform dragTransform;
            private Vector2 initPosition;
            private Vector2 startPosition;
    
            [Header("正确区域")]
            public RectTransform targetArea;
    
            [Header("初始区域")]
            public RectTransform originalArea;
            [Header("错误区域")]
            public RectTransform[] targetOtherArea;
    
            private bool result;    //拖动结果
            private bool valid;     //拖动有效
    
            public bool Result { get => result; private set => result = value; }
            public bool Valid { get => valid; private set => valid = value; }
    
            public void Awake()
            {
                dragTransform = GetComponent<RectTransform>();
    
                //初始位置
                initPosition = dragTransform.localPosition;
            }
    
            /// 
            /// 重置位置
            /// 
            public void ResetPos()
            {
                dragTransform.localPosition = initPosition;
                result = false;
                valid = false;
            }
    
            public void OnBeginDrag(PointerEventData eventData)
            {
                //开始拖动的位置
                startPosition = dragTransform.localPosition;
    
                //设置UI最后渲染
                dragTransform.SetAsLastSibling();
            }
    
            public void OnDrag(PointerEventData eventData)
            {
                dragTransform.localPosition = GetLocalPosition(eventData.position);
            }
    
            public void OnEndDrag(PointerEventData eventData)
            {
                result = RectTransformUtility.RectangleContainsScreenPoint(targetArea, eventData.position);
    #if UNITY_EDITOR
                Debug.Log($"拖动结果: {result}");
    #endif
                if (result)
                {
                    //吸附到目标位置
                    dragTransform.position = targetArea.position;
                    valid = true;
                    return;
                }
                else
                {
                    foreach (var item in targetOtherArea)
                    {
                        if (RectTransformUtility.RectangleContainsScreenPoint(item, eventData.position))
                        {
                            //吸附到目标位置
                            dragTransform.position = item.position;
                            valid = true;
                            return;
                        }
                    }
                    //原始位置,拖动无效
                    if (RectTransformUtility.RectangleContainsScreenPoint(originalArea, eventData.position))
                    {
                        //吸附到目标位置
                        dragTransform.position = originalArea.position;
                        valid = false;
                        return;
                    }
                }
    
                //其它情况还原为开始拖动的位置
                valid = false;
                dragTransform.localPosition = startPosition;
            }
    
            private Vector2 GetLocalPosition(Vector2 screenPosition)
            {
                Vector2 localPosition = Vector2.zero;
                RectTransformUtility.ScreenPointToLocalPointInRectangle(dragTransform.parent as RectTransform, screenPosition, null, out localPosition);
                return localPosition;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    Demo

    using MyTool.Tools;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class Demo : MonoBehaviour
    {
        public DragAndDrop dragAndDrop;
        public Button okBtn;
    
        // Start is called before the first frame update
        void Start()
        {
            okBtn.onClick.AddListener(OnClickOkBtn);
        }
    
        private void Update()
        {
            okBtn.interactable = dragAndDrop.Valid;
        }
    
        void OnClickOkBtn()
        {
            if (dragAndDrop.Result)
            {
                Debug.Log("恭喜你答对了!");
            }
            else
            {
                Debug.Log("很遗憾,没有答对!");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    ui搭建
    在这里插入图片描述
    效果
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    2023大连工业大学计算机考研信息汇总
    Stable Diffusion V3测评
    BS-GX-018 基于SSM实现在校学生考试系统
    Vue学习:组件化
    Python异常「1」(异常的概念、异常捕获、异常的传递、自定义异常)
    【漏洞复现】图书馆集群管理系统存在逻辑绕过
    【博客439】Kubernetes CRI
    地图结构 | 图解占据栅格地图原理(附Matlab建图实验)
    【洛谷题解/AcWing题解/NOIP2006提高组】P1064/AcWing481 金明的预算方案
    数字藏品市场“三大套路”
  • 原文地址:https://blog.csdn.net/weixin_44238530/article/details/133820972