-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsvWrite.cs
71 lines (54 loc) · 1.7 KB
/
CsvWrite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System;
public class CsvReadWrite : MonoBehaviour {
private List<string[]> rowData = new List<string[]>();
// Use this for initialization
void Start () {
Save();
}
void Save(){
// Creating First row of titles manually..
string[] rowDataTemp = new string[3];
rowDataTemp[0] = "Name";
rowDataTemp[1] = "ID";
rowDataTemp[2] = "Income";
rowData.Add(rowDataTemp);
// You can add up the values in as many cells as you want.
for(int i = 0; i < 10; i++){
rowDataTemp = new string[3];
rowDataTemp[0] = "Sushanta"+i; // name
rowDataTemp[1] = ""+i; // ID
rowDataTemp[2] = "$"+UnityEngine.Random.Range(5000,10000); // Income
rowData.Add(rowDataTemp);
}
string[][] output = new string[rowData.Count][];
for(int i = 0; i < output.Length; i++){
output[i] = rowData[i];
}
int length = output.GetLength(0);
string delimiter = ",";
StringBuilder sb = new StringBuilder();
for (int index = 0; index < length; index++)
sb.AppendLine(string.Join(delimiter, output[index]));
string filePath = getPath();
StreamWriter outStream = System.IO.File.CreateText(filePath);
outStream.WriteLine(sb);
outStream.Close();
}
// Following method is used to retrive the relative path as device platform
private string getPath(){
#if UNITY_EDITOR
return Application.dataPath +"/CSV/"+"Saved_data.csv";
#elif UNITY_ANDROID
return Application.persistentDataPath+"Saved_data.csv";
#elif UNITY_IPHONE
return Application.persistentDataPath+"/"+"Saved_data.csv";
#else
return Application.dataPath +"/"+"Saved_data.csv";
#endif
}
}