Hi, so I want to save & load any replays which are made in the game. the replay mode already works, and there are no issues in the editor. but as soon as I try to save or load in the build game, the game crashes at the point I click. does anybody maybe know the reason? thanks!
btw: this is for my game Unity Flight Simulator with over 4.5k downloads, and It would be very nice, if I can implement that save & load function
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System;
using SFB;
public class ReplaySaveLoadSystem : MonoBehaviour
{
public Replay replay;
public void OpenFileBrowser()
{
string filePath = StandaloneFileBrowser.OpenFilePanel("Open Save File", "", "", false)[0];
// Check if a file was selected
if (!string.IsNullOrEmpty(filePath))
{
LoadData(filePath);
}
}
public void SaveData()
{
// Define the folder path
string folderPath = Path.Combine(Application.dataPath, "Data", "Replays");
// Check if the folder exists, create it if not
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
string fileName = "replay_" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".rpl";
string fileFilter = "Replay Files (*.rpl)|*.rpl";
string saveFilePath = StandaloneFileBrowser.SaveFilePanel("Save Replay", folderPath, fileName, fileFilter);
if (!string.IsNullOrEmpty(saveFilePath))
{
SaveDataToFile(saveFilePath);
}
}
private void SaveDataToFile(string filePath)
{
StringBuilder csvData = new StringBuilder();
// Create a culture with the desired decimal separator
CultureInfo culture = new CultureInfo("en-US"); // Example: en-US for period (.), fr-FR for comma (,)
// Append header row
csvData.AppendLine("position_x,position_y,position_z,rotation_x,rotation_y,rotation_z,rotation_w,throttle,rpm,controlInputs_x,controlInputs_y,controlInputs_z,timestamp,airspeed,verticalspeed,altitude");
// Append data rows
foreach (ObjectReplayRecord record in replay.actionReplayRecords)
{
// Convert the data to a CSV-formatted string with specific decimal precision and culture
string rowData = string.Format(
culture,
"{0:F4},{1:F4},{2:F4},{3:F4},{4:F4},{5:F4},{6:F4},{7},{8},{9:F4},{10:F4},{11:F4},{12:F4},{13:F4},{14:F4},{15:F4}",
record.position.x, record.position.y, record.position.z,
record.rotation.x, record.rotation.y, record.rotation.z, record.rotation.w,
record.throttle, record.rpm,
record.ControlInputs.x, record.ControlInputs.y, record.ControlInputs.z,
record.timestamp, record.airspeed, record.verticalspeed, record.altitude
);
csvData.AppendLine(rowData);
}
// Write the data to the file
File.WriteAllText(filePath, csvData.ToString());
}
private void LoadData(string filePath)
{
// Read all lines from the CSV file
string[] lines = File.ReadAllLines(filePath);
// Clear the existing list
replay.actionReplayRecords.Clear();
// Iterate over each line (excluding the header)
for (int i = 1; i < lines.Length; i++)
{
// Split the line by comma to get the individual values
string[] values = lines[i].Split(',');
// Create a new ObjectReplayRecord and populate its fields
ObjectReplayRecord record = new ObjectReplayRecord();
// Parse the numbers with InvariantCulture
record.position = new Vector3(
float.Parse(values[0], CultureInfo.InvariantCulture),
float.Parse(values[1], CultureInfo.InvariantCulture),
float.Parse(values[2], CultureInfo.InvariantCulture)
);
record.rotation = new Quaternion(
float.Parse(values[3], CultureInfo.InvariantCulture),
float.Parse(values[4], CultureInfo.InvariantCulture),
float.Parse(values[5], CultureInfo.InvariantCulture),
float.Parse(values[6], CultureInfo.InvariantCulture)
);
record.throttle = float.Parse(values[7], CultureInfo.InvariantCulture);
record.rpm = int.Parse(values[8], CultureInfo.InvariantCulture);
record.ControlInputs = new Vector3(
float.Parse(values[9], CultureInfo.InvariantCulture),
float.Parse(values[10], CultureInfo.InvariantCulture),
float.Parse(values[11], CultureInfo.InvariantCulture)
);
record.timestamp = float.Parse(values[12], CultureInfo.InvariantCulture);
record.airspeed = float.Parse(values[13], CultureInfo.InvariantCulture);
record.verticalspeed = float.Parse(values[14], CultureInfo.InvariantCulture);
record.altitude = float.Parse(values[15], CultureInfo.InvariantCulture);
// Add the record to the list
replay.actionReplayRecords.Add(record);
}
}
}