using AX.MessageSystem; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Xml; using UnityEngine; using UnityEngine.UI; public class ReplayManager : MonoBehaviour { public SecondNodeObject replayNode; public static ReplayStatus ReplayStatus; public JsonData jsonData; public float replayTime; public int frameIndex; public int eventIndex; public int tagIndex; private TimeLineManager timeLineManager; public TagManager tagManager; public GameObject timeLineSlider; public GameObject nodeDetailShow; public Button lessen; public Button add; public Text speed; public Toggle auto; public Button playLast; public Button playNext; public Toggle playOrPause; public Button playOver; public static int playSpeed = 1; // Use this for initialization void Start() { ReplayStatus = ReplayStatus.over; MessageDispatcher.AddListener("PlayStatusChanged", SetUIByPlayStatus); MessageDispatcher.AddListener("Replay", Replay); lessen.onClick.AddListener(Lesson); add.onClick.AddListener(Add); auto.onValueChanged.AddListener(Auto); playLast.onClick.AddListener(PlayLast); playNext.onClick.AddListener(PlayNext); playOrPause.onValueChanged.AddListener(PlayOrPause); playOver.onClick.AddListener(PlayOver); timeLineManager = timeLineSlider.GetComponent(); MessageDispatcher.AddListener("DisasterUpdateCompelete", ResetUI);//更新完成重置回放状态 } void OnDestroy() { MessageDispatcher.RemoveListener("PlayStatusChanged", SetUIByPlayStatus); MessageDispatcher.RemoveListener("Replay", Replay); MessageDispatcher.RemoveListener("DisasterUpdateCompelete", ResetUI);//更新完成重置回放状态 } private void ResetUI(IMessage obj) { ReplayStatus = ReplayStatus.over; playOrPause.isOn = false; ReplaySetting.PlayStatus = PlayStatus.normal; ChangeReplaySpeed = null;//回放结束清空关心回放速度的事件 auto.isOn = false; } public static int GetNowSpeed() { return playSpeed; } /// /// 接收播放通知,改变按钮属性 /// /// private void Replay(IMessage obj) { playOrPause.isOn = true; } /// /// 播放结束,显示最后数据 /// private void PlayOver() { auto.isOn = false; ReplayStatus = ReplayStatus.over; playOrPause.isOn = false; ReplaySetting.PlayStatus = PlayStatus.normal; var list = jsonData.frameDataList; if (list.Count<1) { return; } var frameData = list[list.Count - 1]; MessageDispatcher.SendMessage("ClearObject"); var dataList = frameData.objectDataList; foreach (var data in dataList) { MessageDispatcher.SendMessage("ReplayFrame", data); } tagManager.HideTagShow(); MessageDispatcher.SendMessage("ReplayStatusChanged", ReplayStatus); //在结束播放的时候如果正在显示提示信息,结束倒计时并隐藏信息窗口 nodeDetailShow.GetComponent().SetControl();// = false; nodeDetailShow.SetActive(false); } /// /// 播放暂停 /// /// private void PlayOrPause(bool isOn) { if (isOn) { var secondNode = SecondNodeManager.selectNode; if (secondNode == null) { ResourceLoadWindow.Instance.LoadTextHintWindow("请选择播放节点", 0.5f); playOrPause.isOn = false; return; } else if (secondNode == replayNode && ReplayStatus == ReplayStatus.pause)//续播 { ReplayStatus = ReplayStatus.normal; MessageDispatcher.SendMessage("ReplayStatusChanged", ReplayStatus); tagManager.HideTagShow(); } else//开始播放 { replayNode = secondNode; var flag = GetJsonData();//获取节点文件数据 if (flag) { MessageDispatcher.SendMessage("ClearObject"); replayTime = 0; frameIndex = 0; eventIndex = 0; tagIndex = 0; //是否显示提示信息 if (string.IsNullOrEmpty(replayNode.nodeDetail)) { Play(); } else { nodeDetailShow.SetActive(true); nodeDetailShow.GetComponent().SetUI(replayNode.nodeDetail); } } else { playOrPause.isOn = false; } } } else //暂停 { if (ReplayStatus == ReplayStatus.normal) { ReplayStatus = ReplayStatus.pause; MessageDispatcher.SendMessage("ReplayStatusChanged", ReplayStatus); } } } /// /// 提示信息显示后播放 /// public void Play() { ReplaySetting.PlayStatus = PlayStatus.isReplay; ReplayStatus = ReplayStatus.normal; timeLineManager.SetSlider(jsonData.sumTime, jsonData.tagDataList); } private void PlayNext() { playOrPause.isOn = false;//必须先暂停 tagManager.HideTagShow(); ReplayNextNode(); } private void PlayLast() { playOrPause.isOn = false;//必须先暂停 tagManager.HideTagShow(); ReplayLastNode(); } private void Auto(bool isOn) { } public static event Action ChangeReplaySpeed; public static event Action ChangeReplayStatus; private void Add() { int Num = int.Parse(speed.text); if (Num == 1) { speed.text = "2"; } else if (Num == 2) { speed.text = "4"; } else if (Num == 4) { speed.text = "8"; } playSpeed = Num; GlobalVariable.ReplaySpeed = int.Parse(speed.text); // MessageDispatcher.SendMessage("ReplaySpeed", GlobalVariable.ReplaySpeed);//回放速度改变事件 if (ChangeReplaySpeed != null) { ChangeReplaySpeed(GlobalVariable.ReplaySpeed); } } private void Lesson() { int Num = int.Parse(speed.text); if (Num == 8) { speed.text = "4"; } else if (Num == 4) { speed.text = "2"; } else if (Num == 2) { speed.text = "1"; } playSpeed = Num; GlobalVariable.ReplaySpeed = int.Parse(speed.text); // MessageDispatcher.SendMessage("ReplaySpeed", GlobalVariable.ReplaySpeed);//回放速度改变事件 if (ChangeReplaySpeed != null) { ChangeReplaySpeed(GlobalVariable.ReplaySpeed); } } /// /// 跳转到标签位置 /// /// public void MoveToTag(TagData tag) { auto.isOn = false;//自动播放关闭 playOrPause.isOn = false;//播放暂停 timeLineManager.UpdateSlider(tag.time);//设置时间轴 tagManager.ShowTag(tag);//显示标签 //设置回放索引 var tagList = jsonData.tagDataList; for (int i = 0; i < tagList.Count; i++) { var tagData = tagList[i]; if (tagData.time == tag.time) { replayTime = tag.time;//设置时间 tagIndex = i + 1; break; } } var frameList = jsonData.frameDataList; for (int j = 0; j < frameList.Count; j++) { var frameData = frameList[j]; if (frameData.time == tag.time) { MessageDispatcher.SendMessage("ClearObject"); var dataList = frameData.objectDataList; foreach (var data in dataList) { MessageDispatcher.SendMessage("ReplayFrame", data); } frameIndex = j + 1; break; } } var eventList = jsonData.eventDataList; for (int k = 0; k < eventList.Count; k++) { var eventData = eventList[k]; if (eventData.time > tag.time) { eventIndex = k; break; } } } // Update is called once per frame void FixedUpdate() { if (ReplaySetting.PlayStatus == PlayStatus.isReplay && ReplayStatus == ReplayStatus.normal) { ReplayFrameData(); ReplayEventData(); ReplayTagData(); timeLineManager.UpdateSlider(replayTime); replayTime += Time.deltaTime * int.Parse(speed.text); } } /// /// 根据游戏状态显示UI /// /// private void SetUIByPlayStatus(IMessage data) { var playStatus = ReplaySetting.PlayStatus; if (playStatus == PlayStatus.normal) { gameObject.SetActive(true); timeLineSlider.SetActive(false); } else if (playStatus == PlayStatus.isEditor) { gameObject.SetActive(false); timeLineSlider.SetActive(false); } else if (playStatus == PlayStatus.isReplay) { gameObject.SetActive(true); timeLineSlider.SetActive(true); } } /// /// 获取节点数据 /// /// private bool GetJsonData() { string nodeName = replayNode.nodeName; string filePath = Application.dataPath + @"/Data/" + DisasterManager.disaster.Id + "/File/XmlData/" + nodeName + ".xml"; if (File.Exists(filePath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filePath); XmlNode firstNodes = xmlDoc.SelectSingleNode("Record"); jsonData = JsonUtility.FromJson(firstNodes.InnerText); return true; } else { ResourceLoadWindow.Instance.LoadTextHintWindow("节点数据文件不存在,无法正常播放", 1f); return false; } } /// /// 帧数据回放 /// private void ReplayFrameData() { var list = jsonData.frameDataList; while (frameIndex < list.Count) { var frameData = list[frameIndex]; if (replayTime >= frameData.time) { MessageDispatcher.SendMessage("ClearObject"); var dataList = frameData.objectDataList; foreach (var data in dataList) { MessageDispatcher.SendMessage("ReplayFrame", data); } frameIndex++; } else break; } } /// /// 事件回放 /// private void ReplayEventData() { var list = jsonData.eventDataList; while (eventIndex < list.Count) { var eventData = list[eventIndex]; if (replayTime >= eventData.time) { MessageDispatcher.SendMessage("ReplayEvent", eventData); eventIndex++; } else break; } if (replayTime > jsonData.sumTime) { ReplayStatus = ReplayStatus.over; nodeDetailShow.GetComponent().SetControl(); playOrPause.isOn = false; ReplaySetting.PlayStatus = PlayStatus.normal; ChangeReplaySpeed = null;//回放结束清空关心回放速度的事件 if (auto.isOn) { ReplayNextNode(); } } } /// /// 标签回放 /// private void ReplayTagData() { var list = jsonData.tagDataList; while (tagIndex < list.Count) { var tagData = list[tagIndex]; if (replayTime >= tagData.time) { playOrPause.isOn = false;//暂停播放 tagManager.ShowTag(tagData); tagIndex++; } else break; } } /// /// 播放下一节点 /// private void ReplayNextNode() { var list = new List(); var firstSelected = FirstNodeManager.selectNode; var secondNode = DisasterManager.secondNodeObjects; foreach (var obj in secondNode) { if (obj.ParentID == firstSelected.nodeID) { list.Add(obj); } } var secondIndex = list.IndexOf(SecondNodeManager.selectNode); if (secondIndex == -1) { return; } else if (secondIndex < list.Count - 1) { //存在未播的二级节点 MessageDispatcher.SendMessage("ReplaySecondNode", list[secondIndex + 1].nodeID); } else if (secondIndex == list.Count - 1) { //已是最后一个二级节点,播放下一个一级节点 var firstNode = DisasterManager.firstNodeObjects; var firstIndex = firstNode.IndexOf(firstSelected); if (firstIndex == -1) { return; } else { var nextIndex = GetNextFirstNodeIndex(firstIndex); if (nextIndex == -1) { return; } else { MessageDispatcher.SendMessage("ReplayFirstNode", firstNode[nextIndex].nodeID); } } } } /// /// 查找下一个非空一级节点的索引值,-1不存在 /// /// /// private int GetNextFirstNodeIndex(int index) { var firstNode = DisasterManager.firstNodeObjects; if (index < firstNode.Count - 1) { //存在下一个一级节点 var nextIndex = index + 1; //查找是否存在二级节点,即判断是否为空节点 var secondNode = DisasterManager.secondNodeObjects; foreach (var node in secondNode) { if (node.ParentID == firstNode[nextIndex].nodeID) { return nextIndex; } } return GetNextFirstNodeIndex(nextIndex); } else { //已是最后一个一级节点 return -1; } } /// /// 播放上一节点 /// private void ReplayLastNode() { var list = new List(); var firstSelected = FirstNodeManager.selectNode; var secondNode = DisasterManager.secondNodeObjects; foreach (var obj in secondNode) { if (obj.ParentID == firstSelected.nodeID) { list.Add(obj); } } var secondIndex = list.IndexOf(SecondNodeManager.selectNode); if (secondIndex == -1) { return; } else if (secondIndex > 0) { //存在未播的二级节点 MessageDispatcher.SendMessage("ReplaySecondNode", list[secondIndex - 1].nodeID); } else if (secondIndex == 0) { //已是第一个二级节点,播放上一个一级节点 var firstNode = DisasterManager.firstNodeObjects; var firstIndex = firstNode.IndexOf(firstSelected); if (firstIndex == -1) { return; } else { var lastIndex = GetLastFirstNodeIndex(firstIndex); if (lastIndex == -1) { return; } else { MessageDispatcher.SendMessage("ReplayFirstNode", firstNode[lastIndex].nodeID); } } } } /// /// 查找上一个非空一级节点的索引值,-1不存在 /// /// /// private int GetLastFirstNodeIndex(int index) { var firstNode = DisasterManager.firstNodeObjects; if (index > 0) { //存在上一个一级节点 var lastIndex = index - 1; //查找是否存在二级节点,即判断是否为空节点 var secondNode = DisasterManager.secondNodeObjects; foreach (var node in secondNode) { if (node.ParentID == firstNode[lastIndex].nodeID) { return lastIndex; } } return GetLastFirstNodeIndex(lastIndex); } else { //已是第一个一级节点 return -1; } } }