using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using Newtonsoft.Json; using UnityEngine.UI; using AX.MessageSystem; using System; [Serializable] public class SubEquipData { public List equips; public string gameObjectname; } public class EquipSelect : ResourceLoadPanel { /// /// 所有装备 /// public List Equips = new List(); /// /// 装备组有哪些装备类型,key为装备组,valus为改组包含的装备类型 /// public Dictionary> Groups = new Dictionary>(); /// /// 本次提交所选择的装备 /// private List selectEquips = new List(); public string Cloths = "隔热防护服,灭火防护服,消防避火服" ; public bool HasCloth = false; public float DrawLineLength = 25f; public float LiftLightLength = 25f; ///// ///// 装备组有哪些类,类里有哪些装备 ///// //public static Dictionary>> equipsGroups = new Dictionary>>(); private GameObject GroupToggleItem;//组按钮预设 private GameObject GroupPanelItem;//组主体预设 private Transform GroupContent; private Transform bag; private Transform equipsumselect; void Awake() { GroupContent = transform.Find("Group/Viewport/Content"); GroupToggleItem = Resources.Load("UI/EquipUI/GroupToggle") as GameObject; GroupPanelItem= Resources.Load("UI/EquipUI/GroupPanel") as GameObject; bag = transform.parent.Find("Bag"); equipsumselect = transform.parent.Find("EquipNumSelectPanel"); InitData(); InitGroups(); gameObject.SetActive(false); } private void Start() { DrawGroup(); MessageDispatcher.AddListener("EquipAddSubmit", AddSubmit); MessageDispatcher.AddListener("EquipRemoveSubmit", RemoveSubmit); MessageDispatcher.RemoveListener("SelectChange", selectchange); MessageDispatcher.AddListener("ReplayEvent", ReplayEventSelectEquip); } private void ReplayEventSelectEquip(IMessage obj) { if (RecordEvent.IsReplay()) { var eventData = (EventData)obj.Data; if (eventData.eventType == RecordEventType.SelectEquip) { SubEquipData args = JsonUtility.FromJson(eventData.json); string equipsname = ""; for (int i = 0; i < args.equips.Count; i++) { equipsname += args.equips[i].Name+" "; } equipsname += "已提交"; ResourceLoadWindow.Instance.LoadTextHintWindow(equipsname, 2f); } } } private void OnDestroy() { MessageDispatcher.RemoveListener("EquipAddSubmit", AddSubmit); MessageDispatcher.RemoveListener("EquipRemoveSubmit", RemoveSubmit); MessageDispatcher.RemoveListener("SelectChange", selectchange); MessageDispatcher.RemoveListener("ReplayEvent", ReplayEventSelectEquip); } private void selectchange(IMessage obj) { Close(); } /// /// 读取json文件 /// /// /// /// public T LoadJson(string fileName) { string json = null; string path = Path.Combine(Application.streamingAssetsPath, fileName); if (File.Exists(path)) { json = File.ReadAllText(path); } T data = JsonConvert.DeserializeObject(json); return data; } /// /// 从json中读取装备数据 /// private void InitData() { Equips.Clear(); Equips = LoadJson>("Equip.json"); //foreach (var item in tempData) //{ // Equips.Add(item.ID, item); //} } /// /// 从json中读取组数据 /// private void InitGroups() { List tempData = LoadJson>("EquipGroup.json"); foreach (var item in tempData) { Groups.Add(item.Name, item.Types); } } /// /// 画Group按钮和panel /// private void DrawGroup() { Dictionary>.KeyCollection keycoll = Groups.Keys; List keylist = new List(); foreach (var item in keycoll) { keylist.Add(item); } for (int i = 0; i < keylist.Count; i++) { GameObject grouptoggle = Instantiate(GroupToggleItem, GroupContent); grouptoggle.name = keylist[i]; grouptoggle.transform.Find("Label").GetComponent().text = keylist[i]; grouptoggle.GetComponent().group = GroupContent.GetComponent(); GameObject groupMain = Instantiate(GroupPanelItem, transform); groupMain.name = keylist[i]; grouptoggle.GetComponent().ControlPanel = groupMain; if (i==0) { grouptoggle.GetComponent().isOn = true; } } } /// /// 从所有装备中找到某件装备的初始信息 /// /// /// public Equip GetInitInfoByName(string equipName) { Equip eq = null; for (int i = 0; i < Equips.Count; i++) { if (Equips[i].Name==equipName) { eq = Equips[i]; break; } } return eq; } /// /// 根据名称获取一个装备拷贝,未进行深复制.除了装备数量及是否选中 其余为定值 /// /// /// public Equip GetInitEquipByName(string equipname) { Equip eq = new Equip(); Equip baseeq = GetInitInfoByName(equipname); eq.ID = baseeq.ID; eq.Name = baseeq.Name; eq.IsSelected = false; eq.SelectGroup = baseeq.SelectGroup; eq.PicName = baseeq.PicName; eq.Group = baseeq.Group; eq.Type = baseeq.Type; eq.Number =0; eq.IsSelectMore = baseeq.IsSelectMore; return eq; } public List GetSelectEquips() { return selectEquips; } public Equip GetEquipFromSelectByName(string name) { Equip eq = null; for (int i = 0; i (true); for (int i = 0; i < eqs.Length; i++) { if (eqs[i].GetComponent().isOn) { eqs[i].GetComponent().isOn = false; } eqs[i].GetComponent().SetImgHide(); } } /// /// 是否可提交,false为服装多选了,不可提交 /// /// public bool CheckCloth() { bool can = true; Bag b = SelectedObjs.selectedCharacters[0].GetComponent(); if (CheckHasCloth(b.EquipList)>0) {//当前消防员有服装 if (CheckHasCloth(selectEquips)>0) { can = false; ResourceLoadWindow.Instance.LoadTextHintWindow("背包中已有服装,不可再选择服装", 0.5f); } else { can = true; } } else { if (CheckHasCloth(selectEquips) > 1) { can = false; ResourceLoadWindow.Instance.LoadTextHintWindow("只能选择一套服装", 0.5f); } else { can = true; } } return can; } /// /// 检查list里有几件服装 /// /// /// private int CheckHasCloth(List lists) { int num = 0; for (int i = 0; i < lists.Count; i++) { if (lists[i].Name== "隔热防护服" || lists[i].Name == "消防避火服" || lists[i].Name == "消防防化服") { num++; } } return num; } IEnumerator WaitClose(float time) { yield return new WaitForSeconds(time); Close(); } }