参考文章:
https://www.cnblogs.com/chinarbolg/p/9601417.html
https://www.cnblogs.com/Fivee/p/13099362.html
using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class MultipleSelection_Dropdown : Dropdown { public bool AlwaysCallback = false;//是否开启 点击选项按钮总是回调 // 当前筛选的技能质量标记 private int SelectIndexBitMark = 0; public List<int> indexs = new List<int>(); public new void Show() { base.Show(); DropdownToggleEvent(); } public void DropdownToggleEvent() { Transform toggleRoot = transform.Find("Dropdown List/Viewport/Content"); //Transform toggleRoot = template.Find("Viewport/Content"); Toggle[] toggleList = toggleRoot.GetComponentsInChildren<Toggle>(false); for (int i = 0; i < toggleList.Length; i++) { Toggle temp = toggleList[i]; temp.onValueChanged.RemoveAllListeners(); // temp.isOn = false; // 原文 //temp.SetIsOnWithoutNotify() temp.isOn = indexs.Contains(i); // 改造后 //temp.isOn = ((1 << i) & SelectIndexBitMark) > 0; //第一次改造后原文 temp.onValueChanged.AddListener(x => OnSelectItemEx(temp, x)); } RefreshShownValue(); } public override void OnPointerClick(PointerEventData eventData) { Show(); } public void OnSelectItemEx(Toggle toggle, bool isOn) { int selectedIndex = -1; Transform tr = toggle.transform; Transform parent = tr.parent; for (int i = 0; i < parent.childCount; i++) { if (parent.GetChild(i) == tr) { selectedIndex = i - 1; break; } } if (selectedIndex < 0) return; if (value == selectedIndex && AlwaysCallback) onValueChanged?.Invoke(value); else value = selectedIndex; if (!isOn) { if (indexs.Contains(value)) indexs.Remove(value); //toggle.isOn = true; //Debug.Log("value = " + value + "+ indexs.count =" + indexs.Count); //return; //SelectIndexBitMark -= SelectIndexBitMark & (1 << value); // 新增 } else { if (!indexs.Contains(value)) indexs.Add(value); //SelectIndexBitMark |= 1 << value; // 新增 } Debug.Log("selectedIndex = " + string.Join(",", indexs)); Hide(); RefreshShownValue(); } }
标签:temp,Dropdown,isOn,value,复选框,indexs,MultipleSelection,public From: https://www.cnblogs.com/Roz-001/p/16859957.html