unity连线选择

 unity连线选择_第1张图片

 此脚本挂在左侧按钮上

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();
            lineObjRT.pivot = new Vector2(0, 0.5f);
            lineObjRT.localScale = Vector3.one;

            lineObjImg = lineObj.AddComponent();
            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 _dic_da_an = new Dictionary();
    //答案录入
    public static Dictionary dic_add = new Dictionary();
    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() != null)
            {
                left_start_arr[i].GetComponent().Decide();
            }

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

            }
        }       
       
    }
}

 

 

你可能感兴趣的:(连线题)