Unity CSV文件加载案例

笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。

CSDN视频网址:http://edu.csdn.net/lecturer/144

游戏开发中经常涉及到文本文件的加载处理,游戏常用文件格式非常多,json,xml,csv,二进制等等,在这里给读者封装的是csv文件的加载,其实,在游戏中使用csv文件格式就是将excel另存为csv格式即可,这样非常方便策划对配置文件的修改。配置文件的处理有很多种方式,在这里给读者推荐一种比较简单的加载脚本,代码如下所示:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Utils
{
    public class CsvReader
    {
        //
        // Properties
        //
        public int ColCount
        {
            get
            {
                return m_Col;
            }
        }

        protected List<string[]> Datas
        {
            get
            {
                if (m_Datas == null)
                {
                    m_Datas = new List<string[]>();
                }
                return m_Datas;
            }
        }

        public int RowCount
        {
            get
            {
                if (m_Datas == null)
                {
                    return 0;
                }
                return m_Datas.Count;
            }
        }

        //
        // Methods
        //
        public void Clear()
        {
            if (m_Datas != null)
            {
                m_Datas.Clear();
            }
            m_Col = 0;
        }

        public bool GetBoolData(int r, int c)
        {
            return GetIntData(r, c) != 0;
        }

        public string GetData(int r, int c)
        {
            if (m_Datas == null)
            {
                return string.Empty;
            }
            if (r < 0 || r >= m_Datas.Count)
            {
                return string.Empty;
            }
            if (c < 0 || c >= m_Datas[r].Length)
            {
                return string.Empty;
            }
            return m_Datas[r][c].Trim();
        }

        public float GetFloatData(int r, int c)
        {
            string data = GetData(r, c);
            if (string.IsNullOrEmpty(data))
            {
                return 0f;
            }
            float result;
            if (!float.TryParse(data, out result))
            {
                result = 0f;
            }
            return result;
        }

        public int GetIntData(int r, int c)
        {
            string data = GetData(r, c);
            if (string.IsNullOrEmpty(data))
            {
                return 0;
            }
            int result;
            if (!int.TryParse(data, out result))
            {
                result = 0;
            }
            return result;
        }

        public bool LoadFromFile(string fileName)
        {
            string text = Singleton<ResourceMgr>.Instance.LoadText(fileName, ResourceCacheType.rctNone);
            return text != null && LoadFromString(text);
        }

        public bool LoadFromString(string text)
        {
            Clear();
            if (string.IsNullOrEmpty(text))
            {
                return false;
            }
            string[] array = text.Split(CsvReader._cLineDims);
            if (array != null && array.Length > 0)
            {
                for (int i = 0; i < array.Length; i++)
                {
                    string text2 = array[i];
                    if (!string.IsNullOrEmpty(text2))
                    {
                        string[] array2 = text2.Split(new char[]
						{
							','
						});
                        Datas.Add(array2);
                        m_Col = Math.Max(m_Col, array2.Length);
                    }
                }
            }
            return true;
        }

        public static readonly char _cLineDims = ';';
        private List<string[]> m_Datas = null;
        private int m_Col = 0;
     //   private int m_Row = 0;
    }
}
以上代码可以直接在工程中使用,直接掉用接口:

	public bool LoadFromFile(string fileName)
脚本中也提供了数据清理,获取数据等接口供用户使用。。。。。。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海洋_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值