using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; using UnityEngine.UI; namespace Script.UI { public class GroupPanel : PanelBase { public GameObject item; public Button btn_Close; public Button btn_Stop; class GroupPanelData { private GameObject go; private Text txt_ID; private Text txt_Name; public GroupPanelData(GameObject go, string idStr, string nameStr = "") { txt_ID = go.transform.GetChild(0).GetComponent(); txt_Name = go.transform.GetChild(1).GetComponent(); txt_ID.text = idStr; txt_Name.text = nameStr; } public void SetCultureName(string name) { this.txt_Name.text = name; } } private Dictionary groups = new Dictionary(); private bool isRolling; public override void Init() { base.Init(); btn_Close.onClick.AddListener(ClickClose); btn_Stop.onClick.AddListener(ClickStop); btn_Close.gameObject.SetActive(false); btn_Stop.gameObject.SetActive(true); } private void ClickStop() { btn_Close.gameObject.SetActive(true); btn_Stop.gameObject.SetActive(false); // isRolling = false; } public override void Dispose() { base.Dispose(); btn_Close.onClick.RemoveListener(ClickClose); btn_Stop.onClick.RemoveListener(ClickStop); } public override void Open() { base.Open(); InitItem(); } private void ClickClose() { UIManager.Instance.OpenPanel(PanelType.Finish); } void InitItem() { List cache = new List(GlobalManager.Instance.list); List end = new List(); var num = cache.Count % 2; // 平均后多出来的 var listNum = cache.Count / 2; // var groupAName = ExcelManager.Instance.GetGroupInfoA.groupName; var groupBName = ExcelManager.Instance.GetGroupInfoB.groupName; // A var count = listNum + num; for (int i = 0; i < count; i++) { var range = Random.Range(0, cache.Count - 1); var summaryInfo = cache[range]; summaryInfo.group = groupAName; end.Add(summaryInfo); cache.Remove(summaryInfo); } // B for (int i = 0; i < listNum; i++) { var range = Random.Range(0, cache.Count - 1); var summaryInfo = cache[range]; summaryInfo.group = groupBName; end.Add(summaryInfo); cache.Remove(summaryInfo); } // 排个序 var list = end.OrderBy(t => t.id).ToList(); // StringBuilder sb = new StringBuilder(); foreach (var summaryInfo in list) { item.SetActive(false); GenItem(summaryInfo.id, summaryInfo.group); // sb.AppendLine($"选手ID:{summaryInfo.id}"); // sb.AppendLine($"选手分组:{summaryInfo.group}"); // sb.AppendLine($"\n"); } isRolling = true; StartCoroutine(RandomRoll()); // FileManager.Instance.SavePlayerData(sb.ToString()); } private void GenItem(string id, string group) { var go = GameObject.Instantiate(item, item.transform.parent); go.name = id; // var txt_ID = go.transform.Find("txt_ID").GetComponent(); // var txt_Group = go.transform.Find("txt_Group").GetComponent(); // txt_ID.text = id; // txt_Group.text = group; go.SetActive(true); groups.Add(int.Parse(id), new GroupPanelData(go, id, group)); } IEnumerator RandomRoll() { while (isRolling) { yield return new WaitForSeconds(0.02f); var info = ExcelManager.Instance.GetGroupRandomInfo; foreach (var culturePanelData in groups.Values) { culturePanelData.SetCultureName(info.groupName); } } ShowResult(); } void ShowResult() { List cache = new List(GlobalManager.Instance.list); List end = new List(); var num = cache.Count % 2; // 平均后多出来的 var listNum = cache.Count / 2; // var groupAName = ExcelManager.Instance.GetGroupInfoA.groupName; var groupBName = ExcelManager.Instance.GetGroupInfoB.groupName; // A var count = listNum + num; for (int i = 0; i < count; i++) { var range = Random.Range(0, cache.Count - 1); var summaryInfo = cache[range]; summaryInfo.group = groupAName; end.Add(summaryInfo); cache.Remove(summaryInfo); } // B for (int i = 0; i < listNum; i++) { var range = Random.Range(0, cache.Count - 1); var summaryInfo = cache[range]; summaryInfo.group = groupBName; end.Add(summaryInfo); cache.Remove(summaryInfo); } // 排个序 var list = end.OrderBy(t => t.id).ToList(); StringBuilder sb = new StringBuilder(); foreach (var summaryInfo in list) { item.SetActive(false); SetItem(summaryInfo.id, summaryInfo.group); sb.AppendLine($"选手ID:{summaryInfo.id}"); sb.AppendLine($"选手分组:{summaryInfo.group}"); sb.AppendLine($"\n"); } GlobalManager.Instance.list = list; FileManager.Instance.SavePlayerData(sb.ToString()); } private void SetItem(string id, string name) { var i = int.Parse(id); if (groups.TryGetValue(i, out var data)) { data.SetCultureName(name); } } } }