using AX.MessageSystem; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class FloorsPanelManager : MonoBehaviour { public Transform NeiParent; public Transform WaiParent; public List AllNei;//存储所有楼层内层信息 public List AllWai;//存储所有楼层的外层信息 public Transform FloorBtnsParentPanel;//所有楼层按钮显示的父对象 private GameObject FloorBtnPrefab; private ToggleGroup ParentToggleGroup; private Button OutBtn; private Button InBtn; public Scrollbar scrollbar; private List ParentArea = new List(); void Start() { if (AreaNameDic.type==SceneType.chemicalBuilding) { gameObject.SetActive(false); return; } if (FloorBtnPrefab == null) { FloorBtnPrefab = Resources.Load("UI/FloorItem") as GameObject; } Initialization(); OutBtn = GetComponent().MoveOut; InBtn = GetComponent().MoveIn; scrollbar = transform.Find("Scroll View/Scrollbar Vertical").GetComponent(); scrollbar.onValueChanged.AddListener(scrollbar_valueChanged); OutBtn.onClick.AddListener(OutBtn_Click); InBtn.onClick.AddListener(InBtn_Click); MessageDispatcher.AddListener("ReplayFrame", ReplayFrameFloors); MessageDispatcher.AddListener("ReplayEvent", ReplayEventFloors); MessageDispatcher.AddListener("ToolsOn", ToolsOnChanged); } private void scrollbar_valueChanged(float value) { GlobalVariable.FloorsScrollBarValue = value; if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal) { var eventData = new EventData(); eventData.time = RecordManager.Instance.RecordTimer; eventData.cloneObjType = CloneObjType.None; eventData.eventType = RecordEventType.FloorsScrollBar; eventData.json = scrollbar.value.ToString(); RecordManager.Instance.jsonData.eventDataList.Add(eventData); } } private void InBtn_Click() { GlobalVariable.FloorsOn = true; AddRecord(GlobalVariable.FloorsOn); MessageDispatcher.SendMessage("FloorsOn"); } private void OutBtn_Click() { GlobalVariable.FloorsOn = false; AddRecord(GlobalVariable.FloorsOn); //foreach (Transform item in FloorBtnsParentPanel) //{ // if (item.GetComponent().floorNum == 0) // { // if (item.GetComponent().isOn) // { // item.GetComponent().isOn = false; // } // } //} } private void AddRecord(bool IsOn) { if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal) { var eventData = new EventData(); eventData.time = RecordManager.Instance.RecordTimer; eventData.cloneObjType = CloneObjType.None; eventData.eventType = RecordEventType.FloorsOn; eventData.json = IsOn.ToString(); RecordManager.Instance.jsonData.eventDataList.Add(eventData); } } private void OnDestroy() { MessageDispatcher.RemoveListener("ReplayFrame", ReplayFrameFloors); MessageDispatcher.RemoveListener("ReplayEvent", ReplayEventFloors); MessageDispatcher.RemoveListener("ToolsOn", ToolsOnChanged); } private void ToolsOnChanged(IMessage obj) { if (GlobalVariable.FloorsOn) { OutBtn_Click(); GetComponent().MoveOut_Click(); } } private void ReplayEventFloors(IMessage obj) { var eventData = (EventData)obj.Data; if (eventData.eventType == RecordEventType.FloorsOn) { bool IsOn = bool.Parse(eventData.json); if (IsOn) { InBtn_Click(); GetComponent().MoveIn_Click(); } else { OutBtn_Click(); GetComponent().MoveOut_Click(); } } else if (eventData.eventType == RecordEventType.FloorsScrollBar) { scrollbar.value = float.Parse(eventData.json); } else if (eventData.eventType == RecordEventType.UIFloor) { FloorData data = JsonUtility.FromJson(eventData.json); Setting(data); } } private void ReplayFrameFloors(IMessage obj) { var objectData = (ObjectData)obj.Data; if (objectData.cloneObjType == CloneObjType.FloorsOn) { bool IsOn = bool.Parse(objectData.json); if (IsOn) { if (GlobalVariable.FloorsOn == false) { InBtn_Click(); GetComponent().MoveIn_Click(); } } else { if (GlobalVariable.FloorsOn == true) { OutBtn_Click(); GetComponent().MoveOut_Click(); } } } else if (objectData.cloneObjType == CloneObjType.FloorsScrollBar) { scrollbar.value = float.Parse(objectData.json); } else if (objectData.cloneObjType == CloneObjType.UIFloor) { FloorData data = JsonUtility.FromJson(objectData.json); Setting(data); } } private void Setting(FloorData data) { if (data.buildNum == null || data.isOn == false) { foreach (var item in ParentArea) { if (item.GetComponent().isOn) { item.GetComponent().isOn = false; } } return; } UIFloor parent = null; foreach (var item in ParentArea) { if (item.GetComponent().isOn) { item.GetComponent().isOn = false; } if (item.buildNum == data.buildNum) parent = item; } if (Mathf.Abs(data.floorNum) == 0) { parent.GetComponent().isOn = true; } else { parent.GetComponent().isOn = true; foreach (var item in parent.MyChildFloors) { if (item.GetComponent().floorNum == data.floorNum && item.GetComponent().interlayerNum == data.interlayerNum) { item.GetComponent().isOn = true; } } } } private void Initialization()//初始化数据信息 { NeiParent = GameObject.Find("Scene").transform.Find("ShiNei"); WaiParent = GameObject.Find("Scene").transform.Find("ZhuTi"); foreach (Transform item in FloorBtnsParentPanel) { Destroy(item.gameObject); } if (GetComponent()) ParentToggleGroup = GetComponent(); else ParentToggleGroup = gameObject.AddComponent(); ParentToggleGroup.allowSwitchOff = true; var MaxNumNei = NeiParent.transform.childCount; for (int i = 0; i < MaxNumNei; i++) { Transform child = NeiParent.GetChild(i); GameObject floorBtnParent = Instantiate(FloorBtnPrefab, FloorBtnsParentPanel); ToggleGroup floorBtnParentGroup = floorBtnParent.AddComponent(); floorBtnParentGroup.allowSwitchOff = true; UIFloor floorParent = floorBtnParent.GetComponent(); floorParent.GetComponent().group = ParentToggleGroup; int length = child.name.IndexOf("ShiNei"); string name = child.name.Substring(0, length); floorParent.MyText.text = AreaNameDic.GetAreaName(name); floorParent.buildNum = name; floorParent.floorNum = 0; floorParent.interlayerNum = 0; floorParent.HasChild.SetActive(true); floorParent.floorsPanelManager = this; floorParent.MyTarget = GetAreaTargetManager.Instance.GetTarget(name); ParentArea.Add(floorParent); var childNum = child.transform.childCount; for (int j = 0; j < childNum; j++) { Transform childchild = child.GetChild(j); GameObject floorBtn = Instantiate(FloorBtnPrefab, FloorBtnsParentPanel); UIFloor floor = floorBtn.GetComponent(); floor.GetComponent().group = floorBtnParentGroup; FloorMessage msg = childchild.GetComponent(); floor.MyTarget = GetTarget(childchild); floor.buildNum = msg.buildNum; floor.buildNum = msg.buildNum; floor.floorNum = msg.floorNum; floor.interlayerNum = msg.interlayerNum; floor.MyText.text = GetName(floor.floorNum, floor.interlayerNum); floor.floorsPanelManager = this; floorBtn.gameObject.SetActive(false); floorParent.MyChildFloors.Add(floorBtn.transform); if (msg != null) AllNei.Add(msg); } } var MaxNumWai = WaiParent.childCount; for (int i = 0; i < MaxNumWai; i++) { Transform child = WaiParent.GetChild(i); var childNum = child.transform.childCount; for (int j = 0; j < childNum; j++) { Transform childchild = child.GetChild(j); FloorMessage msg = childchild.GetComponent(); if (msg != null) AllWai.Add(msg); } } } Transform GetTarget(Transform parent) { Transform target = null; if (parent.childCount <= 4) { Transform child = parent.GetChild(0); int count = child.childCount; for (int i = 0; i < count; i++) { Transform t = child.GetChild(i); if (t.name.ToUpper().Contains("_FLOOR") || t.name.ToUpper().Contains("-FLOOR")) { target = t; break; } } } else { int count = parent.childCount; for (int i = 0; i < count; i++) { Transform t = parent.GetChild(i); if (t.name.ToUpper().Contains("_FLOOR")) { target = t; break; } } } return target; } private string GetName(int floorNum, int interlayerNum) { string result = ""; if (floorNum < 0) { result += "B"; floorNum = -floorNum; } else { result += "F"; } result += floorNum.ToString(); if (interlayerNum > 0) { result += "M"; result += interlayerNum.ToString(); } return result; } }