CSV 파서

Unity/자료 2016. 4. 14. 10:46

속도가 나름 괜찮은 csv 파서를 작성해 봤어요.
맘껐 가져다 쓰세요.
(출처는 표시해 주세요)

CSVParser.cs

//s 20160412-deed, csv 파서
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class CSVParser
{
	public CSVParser()
	{
	}
	~CSVParser()
	{
	}

    List stringList = new List();
    protected int _lineCount = 0;
    public int LineCount
    {
        get
        {
            return _lineCount;
        }
    }

	// Default Functions
	public void LoadFile( string filePath )
	{
		string fileFullPath = Application.dataPath + "/Resources/" + filePath;
		Debug.Log( fileFullPath );

        _lineCount = 0;

		TextAsset tmpText = (TextAsset)Resources.Load( "csv/weapon" );
		string fileAsset = tmpText.text;

		string[] delimeter = { "\r\n" };
		string[] fileDate = fileAsset.Split( delimeter, 
           StringSplitOptions.RemoveEmptyEntries );

		foreach (string rowData in fileDate)
        {
            string[] temp = rowData.Split(',');
            stringList.Add(new string[temp.Length]);
            for (int i = 0; i < temp.Length; ++i)
            {
                stringList[_lineCount][i] = temp[i];
            }

            _lineCount++;
        }

        _lineCount = fileDate.Length;
    }

    public int GetInt(int nRow, int nColumn)
    {
        if (stringList[nRow][nColumn] == null || stringList[nRow][nColumn].Length == 0)
            return 0;

        return System.Convert.ToInt32(stringList[nRow][nColumn]);
    }

    public float GetFloat(int nRow, int nColumn)
    {
        if (stringList[nRow][nColumn] == null || stringList[nRow][nColumn].Length == 0)
            return 0;

        return System.Convert.ToSingle(stringList[nRow][nColumn]);
    }
    public string GetString(int nRow, int nColumn)
    {
        if (stringList[nRow][nColumn] == null || stringList[nRow][nColumn].Length == 0)
            return "";

        return stringList[nRow][nColumn];
    }
}
//e 20160412-deed, csv 파서


블로그 이미지

아몬드콩

,