2020년 1월 7일 화요일

unity 에서 csv파일 write

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
70
71
72
73
74
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
    }
}
cs

댓글 없음:

댓글 쓰기

git rejected error(feat. cherry-pick)

 문제 아무 생각 없이 pull을 받지않고 로컬에서 작업! 커밋, 푸시 진행을 해버렷다. push에선 remote와 다르니 당연히 pull을 진행해라고 하지만 로컬에서 작업한 내용을 백업하지 않고 진행하기에는 부담스럽다(로컬작업 유실 가능성) 해결하려...