• Unity 读取CSV与Excel


      前几天看到我们在游戏中需要动态加载某些角色的游戏策划值,关于这个问题怎么解决呢?其实办法很多种,归根到底,就是数据的读取。我们可以想到的存储数据的载体有很多。例如:txt,xml,csv,excel。甚至可以使用Sqlite,Mysql,Sqlserver等!这都不是问题!那么我们今天学习下CSV文件和Excel的读取。废话不多说了,开始了!

    1.建个空的项目!

    2 建议test.csv的文件并录入数据。

    录入数据:

     3,读取csv文件。

        方法一:将CSV文件强制转换为txt格式,在Unity中使用TextAsset直接读取文本信息。

    using UnityEngine;
    using System.Collections;
    using System.IO;
    
    public class read : MonoBehaviour
    {
        public TextAsset txtCSV;
        public GUIText guitext;
        void Start()
        {
            guitext.text = txtCSV.text;
            print(txtCSV.text);
    
        }
    }

    运行结果:

    第一种方式很简单,那么第二种方式读取,添加脚本 test.cs

    using UnityEngine;
    using System.Collections;
    using System.IO;
    
    public class read : MonoBehaviour
    {public GUIText guitext;
        void Start()
        {
            readCSV();
        }
        /// <summary>
        /// 读取CSV文件
        /// </summary>
        void readCSV()
        {
            //读取csv二进制文件
            TextAsset binAsset = Resources.Load("csv", typeof(TextAsset)) as TextAsset;
            //显示在GUITexture中
            guitext.text = binAsset.text;
    
            string[] data = binAsset.text.Split("|"[0]);
            foreach (var dat in data)
            {
                Debug.Log(dat);
            }
    
            ////读取每一行的内容
            string[] lineArray = binAsset.text.Split("
    "[0]);
            ////按‘|’进行拆分
            string[] lineArray1 = binAsset.text.Split("|"[0]);
    
            //创建二维数组
            string[][] Array = new string[lineArray.Length][];
    
            //把csv中的数据储存在二位数组中
            for (int i = 0; i < lineArray.Length; i++)
            {
                Array[i] = lineArray[i].Split("
    "[0]);
                Debug.Log(Array[i][0].ToString());
    
            }
        }
    }

     运行结果:

     OK 文本方式读取就结束了。下面进行Excel的读取。

      需要插件: 稍后共享!

      

     1 using UnityEngine;
     2 using System.Collections;
     3 using System;
     4 using System.Collections.Generic;
     5 using System.Linq;
     6 using System.Text;
     7 using System.Text.RegularExpressions;
     8 using System.IO;
     9 using Excel;
    10 using System.Data;
    11 using UnityEngine.UI;
    12 
    13 public class NewBehaviourScript : MonoBehaviour 
    14 {
    15     public Text readData;
    16     void Start () 
    17     {        
    18         XLSX();
    19     }
    20     
    21     void XLSX()
    22     {
    23         FileStream stream = File.Open(Application.dataPath + "/UserLevel.xlsx", FileMode.Open, FileAccess.Read);
    24         IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
    25     
    26         DataSet result = excelReader.AsDataSet();
    27     
    28         int columns = result.Tables[0].Columns.Count;
    29         int rows = result.Tables[0].Rows.Count;
    30         
    31         
    32         for(int i = 0;  i< rows; i++)
    33         {
    34             for (int j = 0; j < columns; j++)
    35             {
    36                 string nvalue = result.Tables[0].Rows[i][j].ToString();
    37                 Debug.Log(nvalue);
    38                 if (i > 0)
    39                 {
    40                     readData.text += "		" + nvalue;
    41                 }
    42                 else
    43                 {
    44                     readData.text +="   	" + nvalue;                    
    45                 }
    46             }
    47             readData.text += "
    ";
    48         }    
    49     }
    50 
    51 }
    View Code

    搞定收工!

    PS:可以以数据集的形式存储读取到的二维表格,然后可直接以二维数组的形式获取各个元组的信息!

    作为数据集进行存储

    DataSet result = excelReader.AsDataSet(); 

    取得数据集中第一张表格的行的数目
    int rows = result.Tables[0].Rows.Count;

    取得数据集中第一张表格的列的数目

    int columns = result.Tables[0].Columns.Count;

    直接对行列操作:

    result.Tables[0].Rows[i][j].

    百度网盘:http://pan.baidu.com/s/1kTGIGS3

  • 相关阅读:
    [NOIP2013]花匠
    [NOIP2013]货车运输
    [NOIP2013]火柴排队
    [NOIP2012]疫情控制
    雷动WEBRTC产品
    WebRTC学习笔记_Demo收集
    Red5的直播与点播的压力测试(并发数的测试)
    Apache Tomcat8必备知识
    Servlet3.0学习总结(一)——使用注解标注Servlet
    一张图讲清楚高可用、高性能、可扩展的WEB系统架构
  • 原文地址:https://www.cnblogs.com/wuzhang/p/wuzhang20150511.html
Copyright © 2020-2023  润新知