unity连线选择

 

 此脚本挂在左侧按钮上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class line : MonoBehaviour,IPointerDownHandler,IPointerUpHandler,IDragHandler, IEndDragHandler
{
    [Header("线宽度")]
    public float lineWidth = 3f;
    [Header("线颜色")]
    public Color lineColor = Color.white;
    [Header("限制多选")]
    bool isUP = true;
    //连线
    GameObject lineObj = null;
    RectTransform lineObjRT = null;
    Image lineObjImg;
    //初始点
    Vector3 startPos = Vector3.zero;
    Vector3 endPos = Vector3.zero;
    // 最后落脚的物体
    GameObject GetObj;

    public void Decide()
    {
        if (GetObj != null)
        {
            if (GetObj.name == transform.name)
            {
                Debug.Log("回答正确");
                lineObjImg.color = Color.green;
            }
            else
            {
                lineObjImg.color = Color.red;
                Debug.Log("回答错误");
            }
        }
        else
        {
            Debug.Log("回答错误");
            //  DestroyImmediate(lineObjRT.gameObject);
        }
    }
    public void OnDrag(PointerEventData eventData)
    {
        if (isUP)
        {
            endPos = Input.mousePosition;
            Vector3 durationPos = endPos - startPos;
            lineObjRT.sizeDelta = new Vector2(durationPos.magnitude, lineWidth);
            float angle = Mathf.Atan2(durationPos.y, durationPos.x) * Mathf.Rad2Deg;
            lineObjRT.localRotation = Quaternion.Euler(0, 0, angle);
        }   
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (isUP)
        {
            if (eventData.pointerEnter != null)
            {
                print(eventData.pointerEnter.name);

                endPos = eventData.pointerEnter.transform.position;
                Vector3 durationPos = endPos - startPos;
                lineObjRT.sizeDelta = new Vector2(durationPos.magnitude, lineWidth);
                float angle = Mathf.Atan2(durationPos.y, durationPos.x) * Mathf.Rad2Deg;
                lineObjRT.localRotation = Quaternion.Euler(0, 0, angle);
               
                isUP = false;
            }
            else
            {
                
                print("未选中,结果为空");
            }
        }    
    }
    public void OnPointerDown(PointerEventData eventData)
    {
     
    
        //限制多选
        if (isUP)
        {
            lineObj = new GameObject("LineObj");
            lineObj.SetActive(false);

            lineObjRT = lineObj.AddComponent<RectTransform>();
            lineObjRT.pivot = new Vector2(0, 0.5f);
            lineObjRT.localScale = Vector3.one;

            lineObjImg = lineObj.AddComponent<Image>();
            lineObjImg.color = lineColor;
            lineObjImg.raycastTarget = false;

            lineObjRT.SetParent(transform);

            startPos = transform.position;

            lineObjRT.position = startPos;
            lineObjRT.sizeDelta = Vector2.zero;
            lineObj.SetActive(true);
        };      

        
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        if (isUP)
        {
            if (eventData.pointerEnter != null)
            {
                GetObj = eventData.pointerEnter;



                //添加数据
                Line_chose.dic_add.Add(int.Parse(transform.name), int.Parse(eventData.pointerEnter.name));
                Line_chose.number++;
               
            }
            if (eventData.pointerEnter == null)
            {
                DestroyImmediate(lineObjRT.gameObject);
            }
        }
        if (eventData.button == PointerEventData.InputButton.Right)
        {
            if(Line_chose.dic_add.ContainsKey(int.Parse(transform.name)))
            {
                //移除数据
                Line_chose.dic_add.Remove(int.Parse(transform.name));
                Line_chose.number--;
            }          
            DestoryAll();
            isUP = true;
        }

    }
    //销毁自身所有的线
    void DestoryAll()
    {
        for (int i = 0; i < transform.childCount; i++)
        {
            Destroy(transform.GetChild(i).gameObject);
        }

    }
}

 挂在任意处

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Line_chose : MonoBehaviour
{
    [Header("左侧选项")]
    public RectTransform[] left_start_arr;
    [Header("右侧选项")]
    public RectTransform[] right_end_arr;
    [Header("结束连线")]
    public Button ok;
    //选择题个数
    public static int number;
    //初始答案
    private Dictionary<int, int> _dic_da_an = new Dictionary<int, int>();
    //答案录入
    public static Dictionary<int, int> dic_add = new Dictionary<int, int>();
    void Init()
    {
        _dic_da_an.Add(0, 0);
        _dic_da_an.Add(1, 1);
        _dic_da_an.Add(2, 2);
      
    }
    // Start is called before the first frame update
    void Start()
    {
        
        Init();
        ok.onClick.AddListener(delegate () { Jie_suan(); });
    }
    //销毁自身所有的线
    void DestoryAll()
    {
        for (int i = 0; i < left_start_arr.Length; i++)
        {
            Destroy(left_start_arr[i].GetChild(i).gameObject);
        }

    }
    //结算答案
    public void Jie_suan()
    {
        print("字典个数"+ dic_add.Count);
        for (int i = 0; i < left_start_arr.Length; i++)
        {
            if (left_start_arr[i].GetComponent<line>() != null)
            {
                left_start_arr[i].GetComponent<line>().Decide();
            }

        }
        //是否都连完
        if(number== left_start_arr.Length)
        {
            for (int i = 0; i < number; i++)
            {
                if (dic_add[i] == _dic_da_an[i])
                {
                    //对
                }

            }
        }       
       
    }
}

 

 

  • 10
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值