Unity读取CSV(XML)文件

Unity读取CSV(XML)文件

创建CSV文件

在Unity的StreamingAssets文件夹下,创建Excel表格,并输入相应数据。
例如:
|  |  ||--|--||  |  |
注意:这里我个人习惯是第一行为序号,第二行数据标题,第三行开始为正式数据。格式会影响到代码的解析,大家可以根据自己喜欢的格式对表格和代码进行相应的更改。
在Excel中输入相应的数据后,将文件另存为CSV类型的文件。这里必须是另存为

读取

using System.Collections.Generic;
using UnityEngine;
using System.IO;

/// <summary>
/// CSV(XML)的数据管理者
/// </summary>
public class CsvDataManager<T> where T : CsvDataManager<T>, new()
{
    protected CsvDataManager()
    {

    }
    private static T m_instance;
    public static T GetInstance()
    {
        if (null == m_instance)
        {
            m_instance = new T();
            m_instance.Init();
        }
        return m_instance;
    }

    protected virtual void Init()
    {
        if (-1 == m_Count)
        {
            ReadData(ReadSteam());
        }
    }

    /// <summary>
    /// 表格(XML、CSV)文件名字
    /// </summary>
    protected string m_ExlceFileName = "";
    /// <summary>
    /// 数据数量
    /// </summary>
    protected int m_Count = -1;
    /// <summary>
    /// 标题名字
    /// </summary>
    protected Dictionary<string, int> m_TitleName = new Dictionary<string, int>();
    /// <summary>
    /// 数据链表数组
    /// </summary>
    protected List<string[]> m_DataListArr = new List<string[]>();

    /// <summary>
    /// 获得文件路径
    /// </summary>
    /// <param name="filename">文件名字</param>
    /// <returns>文件在应用中的路径</returns>
    public static string GetFilePath(string filename)
    {
        string filePath = filename;
#if WINDOWS || UNITY_EDITOR
        filePath = Application.dataPath + "/" + "StreamingAssets/XueXiFiles" + "/" + filePath;
#elif IPHONE
            filePath = Application.dataPath +"/Raw/"+ filePath;
#elif ANDROID
            filePath = Application.streamingAssetsPath + "/" + filePath;
#endif
        //Debug.Log(filePath);
        return filePath;
    }

    /// <summary>
    /// 读取文件流
    /// </summary>
    /// <returns>返回的数据流</returns>
    protected StreamReader ReadSteam()
    {
        m_Count = 0;
        string path = GetFilePath(m_ExlceFileName);
        //string content = File.ReadAllText(path, System.Text.Encoding.Default);
        //File.WriteAllText(path, content, System.Text.Encoding.UTF8);
        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
#if UNITY_EDITOR
        return new StreamReader(fs, System.Text.Encoding.Default);
#elif WINDOWS
            return new StreamReader(fs, System.Text.Encoding.UTF8);
#endif
    }

    /// <summary>
    /// 读取文件数据
    /// </summary>
    protected virtual void ReadData(StreamReader sr)
    {
        string str = "";
        //Debug.Log(sr.ReadLine());
        for (; (str = sr.ReadLine()) != null; m_Count++)
        {
            if (0 == m_Count)
            {
                continue;
            }
            else if (1 == m_Count)
            {
                string[] arr = str.Split(',');
                for (int i = 0; i < arr.Length; i++)
                {
                    m_TitleName[arr[i]] = i;
                    //Debug.Log(arr[i]);
                }
            }
            else
            {
                m_DataListArr.Add(str.Split(','));
                //Debug.Log(str);
            }
        }
    }

    /// <summary>
    /// 赋值
    /// </summary>
    public virtual void Assignment()
    {

    }
}

这里虽然做了平台判断,但其实IOS普通情况下,比无法解析CSV文件。
脚本中都有较为详细的注释,我就不做多余赘述了。继承该类之后就可以用了。
注意:
函数ReadData中

if (0 == m_Count)
{
    continue;
}
else if (1 == m_Count)
{
    string[] arr = str.Split(',');
    for (int i = 0; i < arr.Length; i++)
    {
    	m_TitleName[arr[i]] = i;
    	//Debug.Log(arr[i]);
    }
}
else
{
    m_DataListArr.Add(str.Split(','));
    //Debug.Log(str);
}

分别判断的就是表格中第一行的序号;第二行的数据标题;以及之后的正式数据。

发布了20 篇原创文章 · 获赞 1 · 访问量 941

猜你喜欢

转载自blog.csdn.net/f_957995490/article/details/103616625