使用CSV存储游戏数据

本文介绍了一个用于Unity项目的CSV文件读取类,该类提供了多种方法来获取CSV文件中的数据,包括按行列索引、ID和名称等进行数据检索。


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


public class CSVFile  {

    public string [][]Array;
    
    public string GetDataByRowAndCol(int nRow, int nCol)    //通过 行号和列号 获得值
    {
        if (Array.Length <= 0 || nRow >= Array.Length)
            return "";
        if (nCol >= Array[0].Length)
            return "";
        
        return Array[nRow][nCol];
    }
    
    public string GetDataByIdAndName(int nId, string strName)  //通过id 和 字符名 获得值
    {
        if (Array.Length <= 0)
            return "";
        
        int nRow = Array.Length;
        int nCol = Array[0].Length;        
        for (int i = 1; i < nRow; ++i) {
            //string strId = string.Format("\n{0}", nId);
            string strId = string.Format("{0}", nId);
            if (Array[i][0] == strId) {
                for (int j = 0; j < nCol; ++j) {
                    if (Array[0][j] == strName) {
                        return Array[i][j];
                    }
                }
            }
        }
        
        return "";
    }
    public string GetDataByRowandName(int x,string strName)   // 依次返回该字符串名下的所有值
    {
        
        int curcol = -1 ;
        if (Array.Length <= 0)
            return "";

        int nRow = Array.Length;
        int nCol = Array[0].Length;
        for (int j = 0; j < nCol; j++)
        {

            if (Array[0][j] == strName)
            {  
                curcol =j;
               
            }
        
        }
    
        for (int i = x; i < nRow; i++)
        {
            
             return Array[i][curcol];
               
        }

        return "";
    }


    public string GetDataByStringIdAndName(string nId, string strName)   //通过 id 和字符串名 返回数据
    {
        if (Array.Length <= 0)
            return "";
        
        int nRow = Array.Length;
        int nCol = Array[0].Length;        
        for (int i = 1; i < nRow; ++i) {
            //string strId = string.Format("\n{0}", nId);
            string strId = string.Format(nId);
            if (Array[i][0] == strId) {
                for (int j = 0; j < nCol; ++j) {
                    if (Array[0][j] == strName) {
                        return Array[i][j];
                    }
                }
            }
        }
        
        return "";
    }

    public int GetDataByIdAndNameToInt(int nId, string strName){
        return int.Parse (GetDataByIdAndName (nId, strName));
    }
    
    public float GetDataByIdAndNameToFloat(int nId, string strName){
        return float.Parse (GetDataByIdAndName (nId, strName));
    }
}
public class CVS  {     //单例

    private static CVS _instance;

    public static CVS Instance
    {
        get{
            if (_instance == null) {
                _instance = new CVS();
            }
            return _instance;
        }
    }

    private Hashtable files = new Hashtable();

    public CSVFile Read (string fileName)    //输出csv文件的名字
    {
        CSVFile file = (CSVFile)files[fileName];
        if (file != null)
            return file;

        file = new CSVFile ();

        string filePath =Application.dataPath +"/Resources/"+ fileName+".csv";
        Debug.Log("filePath--------------------------------->>"+ filePath);
        if(!File.Exists(filePath)){
             filePath = Application.dataPath + "/Resources/config/" + fileName + ".csv";
        }

        string text = File.ReadAllText(filePath);

        /*
        FileStream  fs1 = new FileStream( filePath,FileMode.Open); 
        StreamReader sw = new StreamReader(fs1);
        string text = sw.ReadToEnd();

        sw.Close();
        sw.Dispose();
        */

        //读取csv二进制文件
        /*
        TextAsset binAsset = Resources.Load (fileName, typeof(TextAsset)) as TextAsset;        
        string text = binAsset.text;
        */
        //读取每一行的内容
        //string [] lineArray = binAsset.text.Split ("\r"[0]);
        string [] lineArray = text.Trim().Replace(",",";").Replace("\"","").Split ("\n"[0]);
        
        //创建二维数组
        file.Array = new string [lineArray.Length][];
        
        //把csv中的数据储存在二位数组中
        for(int i =0;i < lineArray.Length; i++)
        {
            //Array[i] = lineArray[i].Split (',');
            //lineArray[i] = lineArray[i].Trim();
            file.Array[i] = lineArray[i].Trim().Split (';');
        }

        files [fileName] = file;
        return file;
    }

    public CSVFile getFile(string fileName ="config/role"){
        return Read (fileName);
    }

    // Use this for initialization
    void Start () {
        //测试  将 MyText.csv文件导入到正确路径filePath

        //调用上面写好的方法,也可以根据需求自己添加方法;
        Read ("MyText");
        Debug.Log (Read ("Text").GetDataByIdAndName (1, "name")); //根据id和字符串名字获取值

        //以下为MyText.csv的内容

            id,name,level,attack,del,type
            1,多蓝盾,10,0,0,2

            2,多兰剑,20,10,5,2

            3,多兰戒,30,5,10,2

      



            


        









    }

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值