using AX.MessageSystem; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; [Serializable] public class SpreadFireData { public long sourceId; public string name; public Vector3 position;//位置 public Vector3 eulerAngles;//旋转 public Vector3 scale;//缩放 public string buildNum;//楼号 public int floorNum;//层号 public int interlayerNum;//夹层号,0表示不是夹层,1表示第一个夹层 public int Layer; public string UserId; } [Serializable] public class SpreadFireRecordData//蔓延出来的火的帧数据 { public List spreadFiredataList = new List(); } public class SpreadFireManager : MonoBehaviour { public static SpreadFireManager Instance; public GameObject firePrefab; private void Awake() { Instance = this; } void Start() { MessageDispatcher.AddListener("ReplayFrame", ReplayFrameData); MessageDispatcher.AddListener("ReplayEvent", ReplayEventData); } public SpreadFireRecordData GetFrameData() { SpreadFireRecordData recordData = new SpreadFireRecordData(); foreach (Transform item in transform) { SpreadFireData data = new SpreadFireData(); data.name = item.name; data.sourceId = item.GetComponent().SourceId; data.position = item.localPosition; data.eulerAngles = item.localRotation.eulerAngles; data.scale = item.localScale; data.buildNum = item.GetComponent().buildNum;//楼号 data.floorNum = item.GetComponent().floorNum;//层号 data.interlayerNum = item.GetComponent().interlayerNum;//夹层号,0表示不是夹层,1表示第一个夹层 data.Layer = item.GetComponent().Layer; recordData.spreadFiredataList.Add(data); } return recordData; } private void ReplayFrameData(IMessage obj) { var baseData = (ObjectData)obj.Data; if (baseData.cloneObjType == CloneObjType.SpreadFireFrame) { ClearObject(); SpreadFireRecordData data = Newtonsoft.Json.JsonConvert.DeserializeObject(baseData.json); if (data.spreadFiredataList.Count > 0) { foreach (var item in data.spreadFiredataList) { GameObject spreadfire = Instantiate(firePrefab, transform); spreadfire.name = item.name; spreadfire.transform.localPosition = item.position; spreadfire.transform.localRotation = Quaternion.Euler (item.eulerAngles.x, item.eulerAngles.y, item.eulerAngles.z) * Quaternion.identity; spreadfire.transform.localScale = item.scale; spreadfire.GetComponent().SourceId = item.sourceId; spreadfire.GetComponent().buildNum = item.buildNum; spreadfire.GetComponent().floorNum = item.floorNum; spreadfire.GetComponent().interlayerNum = item.interlayerNum; spreadfire.GetComponent().Layer = item.Layer; } } } } private void ClearObject() { foreach (Transform item in transform) { Destroy(item.gameObject); } } private void ReplayEventData(IMessage obj) { var baseEventData = (EventData)obj.Data; if (baseEventData.eventType == RecordEventType.SpreadFireCreat) { SpreadFireCreatData data = Newtonsoft.Json.JsonConvert.DeserializeObject(baseEventData.json); GameObject spreadfire = Instantiate(firePrefab, transform); spreadfire.name = data.name; spreadfire.transform.localPosition = data.position; spreadfire.transform.localRotation = Quaternion.Euler (data.eulerAngles.x, data.eulerAngles.y, data.eulerAngles.z) * Quaternion.identity; spreadfire.transform.localScale = data.scale; spreadfire.GetComponent().SourceId = data.sourceId; spreadfire.GetComponent().buildNum = data.buildNum; spreadfire.GetComponent().floorNum = data.floorNum; spreadfire.GetComponent().interlayerNum = data.interlayerNum; spreadfire.GetComponent().Layer = data.Layer; } else if (baseEventData.eventType == RecordEventType.SpreadFireDestory) { foreach (Transform item in transform) { if (item.GetComponent()) { SpreadFireDestoryData data = (SpreadFireDestoryData)obj.Data; if (item.GetComponent().SourceId == data.sourceId) { Destroy(item.gameObject); } } } } } private void OnDestroy() { MessageDispatcher.RemoveListener("ReplayEvent", ReplayEventData); MessageDispatcher.RemoveListener("ReplayFrame", ReplayFrameData); } }