Skip to content

Instantly share code, notes, and snippets.

@Hsgngr
Created July 28, 2020 10:21
Show Gist options
  • Save Hsgngr/b09958705039d8f6f9ed6c4c9d040593 to your computer and use it in GitHub Desktop.
Save Hsgngr/b09958705039d8f6f9ed6c4c9d040593 to your computer and use it in GitHub Desktop.
Exporting the data to csv from Unity
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class ExportCsv : MonoBehaviour
{
public StringBuilder sb = new System.Text.StringBuilder();
public PandemicArea pandemicArea;
private string contentData;
void Start()
{
pandemicArea = GetComponentInParent<PandemicArea>();
}
public void addHeaders()
{
sb.AppendLine("HealthyCount;InfectedCount;RecoveredCount;Time");
}
public void record()
{
decimal time = Decimal.Round((decimal)Time.time, 2);
sb.AppendLine(pandemicArea.healthyCounter.ToString() + ';' + pandemicArea.infectedCounter.ToString() + ";" + pandemicArea.recoveredCounter.ToString() + ";" + time.ToString());
SaveToFile(sb.ToString());
}
public void SaveToFile(string content)
{
// Use the CSV generation from before
//var content = ToCSV();
// The target file path e.g.
var folder = Application.streamingAssetsPath;
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
var filePath = Path.Combine(folder, "export.csv");
using (var writer = new StreamWriter(filePath, false))
{
writer.Write(content);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment