自学内容网 自学内容网

在UI界面实现画线功能

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


public class JHSCZPanel : PanelBase
{
    private Dropdown dropdown;


    private Button CloseBtn;


    private Button btn_1;
    private Button btn_2;
    private Button btn_3;
    private Text tip;


    private UILineRenderer lineRenderer;


    private int value = 0;
    private int index = 0;
    private RectTransform curTr;


    private List<Transform> trueList = new List<Transform>();
    private List<Transform> curList = new List<Transform>();
    public override void OnOpen(params object[] args)
    {
        base.OnOpen(args);


        tip = panel.transform.Find("Tip").GetComponent<Text>();
        tip.gameObject.SetActive(false);


        CloseBtn = transform.Find(GetType().Name + "/Title/CloseBtn").GetComponent<Button>();
        CloseBtn.onClick.AddListener(() =>
        {
            CloseSelf();
        });


        lineRenderer = panel.transform.GetComponentInChildren<UILineRenderer>();


        dropdown = panel.transform.GetComponentInChildren<Dropdown>();
        dropdown.onValueChanged.AddListener(DropValueChanged);


        btn_1 = panel.transform.Find("Button").GetComponent<Button>();
        btn_1.onClick.AddListener(() => 
        {
            curTr = btn_1.GetComponent<RectTransform>();
            DrawLine();
        });


        btn_2 = panel.transform.Find("Button (1)").GetComponent<Button>();
        btn_2.onClick.AddListener(() => 
        {
            curTr = btn_2.GetComponent<RectTransform>();
            DrawLine();
        });


        btn_3 = panel.transform.Find("Button (2)").GetComponent<Button>();
        btn_3.onClick.AddListener(() => 
        {
            curTr = btn_3.GetComponent<RectTransform>();
            DrawLine();
        });


        trueList.Add(btn_1.transform);
        trueList.Add(btn_2.transform);
        trueList.Add(btn_3.transform);
    }


    private void DropValueChanged(int arg0)
    {
        value = arg0;
        lineRenderer.ResetSelf();
        curList.Clear();
        index = 0;
        tip.gameObject.SetActive(false);
    }


    private void DrawLine()
    {
        curList.Add(curTr);
        lineRenderer.AppendUIElement(curTr);
        index++;


        if (index >= 3)
        {
            Debug.Log("连线完成");


            if (curList[0] == trueList[0] && curList[1] == trueList[1] && curList[2] == trueList[2])
            {
                Debug.Log("正确");
                tip.gameObject.SetActive(true);
                tip.text = "连线正确";
                tip.color = Color.green;
                lineRenderer.SetLineColor(Color.white);
            }
            else
            {
                if (value == 0)
                {
                    tip.gameObject.SetActive(true);
                    tip.text = "连线错误";
                    tip.color = Color.red;
                    lineRenderer.SetLineColor(Color.red);
                }
                Debug.Log("错误");
            }
            curList.Clear();
        }
    }
}


原文地址:https://blog.csdn.net/qq_39872270/article/details/144668074

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!