/// <summary> /// Deserializes all active <see cref="ReplayEvent"/> from the state and dispatches any events to the <see cref="OnReplayEvent(ReplayEvent)"/> handler. /// </summary> /// <param name="state">The <see cref="ReplayState"/> to read the variable data from</param> protected virtual void ReplayDeserializeEvents(ReplayState state) { // Read the event size short size = state.Read16(); // Read all events for (int i = 0; i < size; i++) { // Create the event ReplayEvent evt = new ReplayEvent(); // Read the event id evt.eventID = state.ReadByte(); // Read the data size byte dataSize = state.ReadByte(); // Read the event data evt.eventData = state.ReadState(dataSize); try { // Trigger the event callback OnReplayEvent(evt); } catch (Exception e) { Debug.LogException(e); } } }
/// <summary> /// Called by the replay system when this <see cref="ReplayObject"/> should deserialize its replay data. /// </summary> /// <param name="state">The <see cref="ReplayState"/> to deserialize the data from</param> public void OnReplayDeserialize(ReplayState state) { // Always read the prefab name state.ReadString(); // Read all data while (state.EndRead == false) { // Read the id ReplayIdentity identity = state.ReadIdentity(); // Read the size short size = state.Read16(); // Mathc flag bool matchedData = false; // Check for matched id foreach (ReplayBehaviour behaviour in observedComponents) { // Check for destroyed components if (behaviour == null) { continue; } // Check for matching sub id if (behaviour.Identity == identity) { // We have matched the data so we can now deserialize behaviourState = state.ReadState(size); // Deserialize the component behaviour.OnReplayDeserialize(behaviourState); // Set matched flags matchedData = true; break; } } // Check for found data if (matchedData == false) { // We need to consume the data to maintain the state for (int i = 0; i < size; i++) { // Call read byte multiple times to save allocating an array state.ReadByte(); } Debug.LogWarning("Possible replay state corruption for replay object " + gameObject); } } }