码迷,mamicode.com
首页 > 编程语言 > 详细

Unity3D中读取CSV文件

时间:2016-03-19 11:22:52      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:

直接上代码

Part1:

 1 using UnityEngine;
 2 using System.IO;
 3 using System.Collections.Generic;
 4 
 5 public class CSV
 6 {
 7     static CSV csv;
 8     public List<string[]> m_ArrayData;
 9     public static CSV GetInstance()
10     {
11         if (csv == null)
12         {
13             csv = new CSV();
14         }
15         return csv;
16     }
17     private CSV() { m_ArrayData = new List<string[]>(); }
18     public string GetString(int row, int col)
19     {
20         return m_ArrayData[row][col];
21     }
22     public int GetInt(int row, int col)
23     {
24         return int.Parse(m_ArrayData[row][col]);
25     }
26     public void LoadFile(string path, string fileName)
27     {
28         m_ArrayData.Clear();
29         StreamReader sr = null;
30         try
31         {
32             sr = File.OpenText(path + "//" + fileName);
33             Debug.Log("file finded!");
34         }
35         catch
36         {
37             Debug.Log("file don‘t finded!");
38             return;
39         }
40         string line;
41         while ((line = sr.ReadLine()) != null) 
42         {
43             m_ArrayData.Add(line.Split(,));
44         }
45         sr.Close();
46         sr.Dispose();
47     }
48 }

 Part2:

using UnityEngine;

public class FileController : MonoBehaviour
{

    // Use this for initialization
    void Start ()
    {
        CSV.GetInstance().LoadFile(Application.dataPath + "/Res", "myTest.csv");

        Debug.Log("GetString : " + CSV.GetInstance().GetString(1, 1));
        Debug.Log("GetInt : " + CSV.GetInstance().GetString(1, 2));
    }
}

补充

关于路径有4个类型:

Application.dataPath:该路径指向我们Unity编辑器的Asset文件夹

Application.persistentDataPath:该路径指向iOS和Android的沙盒路径

Application.streamingAssetsPath:streamingAsset文件夹路径,在任何平台都可以通过这个路径读取到文件夹里的内容

Application.temporaryCachePath:临时数据文件路径

Unity3D中读取CSV文件

标签:

原文地址:http://www.cnblogs.com/JMcc020/p/5294460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!