본문 바로가기
게임 개발/Unity3D

[Unity | 유니티] 데이터를 CSV 파일로 추출하기

by 불타는홍당무 2018. 12. 13.


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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System;
 
public class CSVFileWriter : MonoBehaviour 
{
    private List<string[]> studentData = new List<string[]>();
    
    void Write(){
 
        string[] tempStudentData = new string[3];
        tempStudentData[0= “Name";
        tempStudentData[1] = “Age";
        tempStudentData[2= “ID";
        studentData.Add(tempStudentData);
        for(int i = 0; i < 10; i++)
        {
            tempStudentData = new string[3];
            tempStudentData[0] = “Micheal"+i; // Name
            tempStudentData[1= (i+20).ToString(); // Age
            tempStudentData[2= i.ToString() // ID
            studentData.Add(tempStudentData);
        }
 
        string[][] output = new string[studentData.Count][];
 
        for(int i = 0; i < output.Length; i++)
        {
            output[i] = studentData[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();
    }
 
    private string getPath(){
        #if UNITY_EDITOR
        return Application.dataPath +"/CSV/“+”/Student Data.csv";
        #elif UNITY_ANDROID
        return Application.persistentDataPath+"Student Data.csv";
        #elif UNITY_IPHONE
        return Application.persistentDataPath+"/"+"Student Data.csv";
        #else
        return Application.dataPath +"/"+"Student Data.csv";
        #endif
    }
}
 
cs