Unity 使用UI搭建下拉复选框 ,Toggle监听事件

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

public class Drow : MonoBehaviour
{
//下拉框面板
public GameObject Dro;

string str;
//
public List<Toggle> to;

List<string> strlist=new List<string>();

List<int> intlist = new List<int>();
//提示框显示选中字  显示选中得第一个字
public GameObject Te;
//选中框对应Image亮
public List<Image> ImageList;

int MI = 0;
// Start is called before the first frame update
void Start()
{
    for (int i = 0; i < to.Count; i++)
    {
        int L = i;
        to[L].onValueChanged.AddListener((bool b) => SetTo(b, L));
        to[L].transform.Find("Label").GetComponent<Text>().text = to[i].name;
    }
}

void SetTo(bool b,int j)
{
    if (j == 0 )
    {
        StrMethod(0, b);
    }
    else if (j == 1 )
    {
        StrMethod(1,b);
    }
    else if (j == 2 )
    {
        StrMethod(2, b);
    }
    else if (j == 3 )
    {
        StrMethod(3, b);
    }
    else if (j == 4 )
    {
        StrMethod(4, b);
    }
    else if (j == 5 )
    {
        StrMethod(5, b);
    }
    else if (j == 6 )
    {
        StrMethod(6, b);
    }

}
//判断ison
private  void StrMethod(int i,bool b)
{
    if (b)
    {
        strlist.Add(to[i].name);

       intlist.Add(i);
    }
    else
    {
        intlist.Remove(i);
    }

} 

//下拉框箭头按钮

public void Setbool()
{

    Dro.SetActive(!Dro.activeSelf);
    if (Dro.activeSelf==false)
    {

        ImageMethod(intlist);
       
        //foreach (var item in strlist)
        //{
        //    print(item);
        //}
        //for (int i = 0; i < to.Count; i++)
        //{
        //    if (to[i].isOn)
        //    {
        //        intlist.Add(i);
        //    }
        //}
        for (int i = 0; i < to.Count; i++)
        {
           
            if (intlist[MI]==i)
            {
               
                ImageList[i].color = Color.yellow;
               
                if (MI<intlist.Count-1)  
                {
                    MI++;
                    print(MI);
                }
                   
             
              
            }
            else
            {
                ImageList[i].color = Color.red;  

            }
        }
        MI = 0;
       
       // Te.GetComponent<Text>(). text = strlist[0];
    }
    else
    {
        strlist.Clear();
       // intlist.Clear();
    }
}
//排序
private void ImageMethod(List<int> li)
{
    for (int i = 0; i < li.Count - 1; i++) //外层循环需要循环n - 1次
    {
        for (int j = 0; j < li.Count - 1 - i; j++)
        {
            if (li[j] > li[j + 1]) //从小到大排序
            {
                int temp = li[j + 1]; //定义一个值用于交换使用 如果数组第二个数小于前一个数,那么把第二个小的数显存放着
                li[j + 1] = li[j]; //把前一个大的数放到后面
                li[j] = temp; //再把存放在temp中小的数放在后面
            }
        }
    }

}

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值