Skip to content

Instantly share code, notes, and snippets.

@blue0513
Created January 13, 2018 15:35
Show Gist options
  • Save blue0513/31e0ed1664924e0dac92ecc0c6d9af46 to your computer and use it in GitHub Desktop.
Save blue0513/31e0ed1664924e0dac92ecc0c6d9af46 to your computer and use it in GitHub Desktop.
Log Writer for Unity
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class LogWriter : MonoBehaviour {
private float timer = 0f;
private float logSpan = 1f;
// log path
private string storedFilePath = "";
void Awake() {
createDir(storedFilePath);
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if(timer > logSpan){
LogSave();
timer = 0f;
}
}
private void createDir (string _storedFilePath) {
#if UNITY_EDITOR
if (!Directory.Exists (Application.dataPath + _storedFilePath))
Directory.CreateDirectory (Application.dataPath + _storedFilePath);
#else
if(!Directory.Exists(Application.persistentDataPath + _storedFilePath))
Directory.CreateDirectory(Application.persistentDataPath + _storedFilePath);
#endif
}
public void LogSave () {
// You should set the data to other store class
// For example,
// Data = "";
// Dictionary<string, string> _data = new Dictionary<string, string> ();
// _data.Add ("devicekey", DeviceKey);
// foreach (KeyValuePair<string, string> pair in _data) {
// Data += pair.Key + "," + pair.Value + ",";
// }
LogManager.setData ();
string filename = "YOUR FILE NAME";
string filepath;
#if UNITY_EDITOR
filepath = Application.dataPath + storedFilePath + filename + ".csv";
#else
filepath = Application.persistentDataPath + storedFilePath + filename + ".csv";
#endif
StreamWriter sw;
FileInfo fi;
fi = new FileInfo (filepath);
sw = fi.AppendText ();
// write data from other store class
sw.WriteLine (LogManager.Data);
sw.Flush ();
sw.Close ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment