Last active
March 24, 2018 16:37
-
-
Save smkplus/cadf28aa1b8cf4a63d0fc2906c74ff47 to your computer and use it in GitHub Desktop.
ExcelForUnity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to write data to CSV file in UNITY | |
Hi Guys, if any one is facing problem to save data in csv file can use this solution. If you have any questions, you can ask me through comments. | |
Things to remember : | |
Never ever add any commas to your data otherwise, cells will get messed up completely. | |
Saving Your Spreadsheet as a CSV | |
Note: These instructions are for Microsoft Excel 2010, but any spreadsheet software will follow a similar process. | |
1. Open your file in your spreadsheet program. | |
2. Click on File and choose Save As. | |
File › Save As | |
3. Under Save as type, choose CSV (Comma delimited). Click Save. | |
Save as CSV comma delimited, then save | |
4. You may see a message that your file "may contain features that are not compatible with CSV." This message is to inform you that any formatting you may have (such as colors or bold text) and any formulas will not be preserved in the CSV formatted file. Click Yes to continue. | |
Excel CSV error message | |
Here is the code | |
``` | |
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 | |
} | |
} | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment