using AX.MessageSystem; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class LinerRotationReplay : MonoBehaviour { void Start() { MessageDispatcher.AddListener("ReplayEvent", ReplayAgent); MessageDispatcher.AddListener("PlayStatusChanged", PlayStatusChanged); } private void PlayStatusChanged(IMessage obj) { var playStatus = ReplaySetting.PlayStatus; switch (playStatus) { case PlayStatus.normal: pairs.Clear(); break; case PlayStatus.isReplay: break; case PlayStatus.isEditor: pairs.Clear(); break; default: break; } } List pairs = new List(); private void ReplayAgent(IMessage obj) { var eventData = (EventData)obj.Data; if (eventData.eventType == RecordEventType.LinearData) { var data = JsonUtility.FromJson(eventData.json); if ((LinearEventType)data.LinearEventType != LinearEventType.Rotation) return; var data1 = JsonUtility.FromJson(data.Data); Transform tran = TransformHelper.FindChild(EntitiesManager.Instance.GetEntityByID(data.GameId).transform, data1.Name); var pair = new Pair() { Time = 0f, keyValuePair = new KeyValuePair>(data.TimeLength, (time, index) => { tran.localRotation = Quaternion.Lerp(tran.localRotation, TransformHelper.ConvertToQuaternion(data1.EndRotation), time * 2f); if (tran.localRotation == TransformHelper.ConvertToQuaternion(data1.EndRotation)) if (index <= pairs.Count) pairs.RemoveAt(index); }) }; pairs.Add(pair); } } public float speed; void FixedUpdate() { if (RecordEvent.IsReplayPause()) return; for (int i = 0; i < pairs.Count; i++) { if (pairs[i].Time >= pairs[i].keyValuePair.Key) { pairs.RemoveAt(i); break; } pairs[i].Time += Time.deltaTime; if (pairs[i].Time <= pairs[i].keyValuePair.Key) { speed = Time.deltaTime * GlobalVariable.ReplaySpeed; pairs[i].keyValuePair.Value.Invoke(speed, i); } } } private void OnDestroy() { MessageDispatcher.RemoveListener("ReplayEvent", ReplayAgent); MessageDispatcher.RemoveListener("PlayStatusChanged", PlayStatusChanged); } } public class Pair { public float Time; public KeyValuePair> keyValuePair; }