Unity中读取.CSV文件

很多时候我们需要使用策划的Excel表来做游戏的静态数据配置, 而不是采用自己定义的xml或者U3D的scriptobject。

1、创建csv文件

自己写一个excel表格,然后另存为csv文件,文件名是test.csv。

2、改成txt格式

既然unity3d不认识csv,那么我们人工处理一下,很简单,像上一篇文章一样,简单粗暴的把csv的扩展名改成txt吧,谁让unity3d比较亲txt呢,更改后是test.txt,打开后是这个样子的:

3、读取txt文件

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

public class Test4 : MonoBehaviour
{
    List<FruitData> fruitDataList = new List<FruitData>();
    void Start()
    {
        //读取csv二进制文件
        TextAsset binAsset = Resources.Load<TextAsset>("test");
        Debug.Log(binAsset);

        //读取每一行的内容
        string[] lineArray = binAsset.text.Split("\r"[0]);

        AddData(lineArray);
        Debug.Log(fruitDataList[0].intro);
    }

    /// <summary>
    /// 添加数据
    /// </summary>
    /// <param name="lineArray"></param>
    public void AddData(string[] lineArray)
    {
        string[] fieldNames = lineArray[0].Replace("\r", "").Split(","[0]);
        for (int i = 1; i < lineArray.Length - 1; i++)
        {
            string[] Value = lineArray[i].Replace("\r", "").Split(","[0]);
            Dictionary<string, string> paramters = new Dictionary<string, string>();
            for (int j = 0; j < fieldNames.Length; j++)
            {
                paramters.Add(fieldNames[j], Value[j]);
            }
            FruitData fruitData = new FruitData(paramters);
            fruitDataList.Add(fruitData);
        }
    }
}

下面创建一个FruitData,将从csv读取的文件放到对象中

using System.Collections.Generic;
using System.Linq;
using System.Reflection;

public class FruitData 
{
    public int id;
    public string name;
    public string intro;

    public FruitData(Dictionary<string, string> paramters)
    {
        FieldInfo[] fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);

        foreach (var item in paramters.Keys)
        {
            FieldInfo[] curfields = fields.Where(t => t.Name == item).ToArray();

            if (curfields != null && curfields.Length == 1)
            {
                //Debug.Log("key:" + item + "value:" + paramters[item]);

                FieldInfo curField = curfields[0];

                if (curField.FieldType == typeof(int))
                {
                    curField.SetValue(this, int.Parse(paramters[item]));
                }
                else if (curField.FieldType == typeof(string))
                {
                    curField.SetValue(this, paramters[item]);
                }
                else if (curField.FieldType == typeof(float))
                {
                    curField.SetValue(this, float.Parse(paramters[item]));
                }
            }
        }
    }

}

打印如下:

参考自:unity3d学习笔记(十七)--unity3d读取csv文件_Aimar_Johnny的博客-CSDN博客_unity读取csv

Unity 处理策划的 Excel - 灵魂重新 - 博客园

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值