Unity 读取Excel

摘要:
游戏有大多数配置文件,如玩家级别、游戏商店信息等。通常,将这些文件放入Excel中读取第一个解决方案:xlsx-˃csv-˃更改为UTF-8或Unicode代码-˃将后缀名修改为.txt-˃读取资源。加载并更改为TextAsset-˃pass,并将其转换为csv(软件使用记事本++)项目图:usingUnityEngine;使用System.Collec

游戏有大多数配置文件,比如玩家等级,游戏商店信息等等.通常情况下把这些放入excel中来读取

第一种解决方案: 

xlsx –> csv –> 改变成UTF-8 或者Unicode编码 –> 修改后缀名成.txt  -> 通过Resources.Load读取转换成TextAsset –> 通过,方式开区分开来

image

转换成csv(软件采用notepad++)

image

项目图:

image

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Text;

public class Test : MonoBehaviour {

    private string[] lineArray;
    private CSVTable table;

    public void OnGUI() 
    {
        if (GUI.Button(new Rect(0, 0, 100, 100), "读取Excel"))
        {
            TextAsset text = (TextAsset)Resources.Load("data", typeof(TextAsset));
            lineArray = text.text.Split("
"[0]);

            table = new CSVTable();
            for (int i = 0; i < lineArray.Length; i++)
            {
                table.AddRow(lineArray[i]);
            }

            //输出每行的信息
            for (int i = 0; i < table.rows.Count; i++)
            {
                Debug.Log(table.rows[i].ToString());
            }

            Debug.Log("==========================");
            

            //还是输出每行的信息
            for (int i = 0; i < table.rows.Count; i++)
            {
                Debug.Log(table.rows[i][0] + "-" + table.rows[i][1]);

            }


            #region 信息输出
            /*
            string[] row;
            string info = string.Empty;
            for (int j = 0; j < lineArray.Length; j++)
            {
                row = lineArray[j].Split(',');
                for (int i = 0; i < row.Length; i++)
                {
                    info += "-" + row[i];
                }

                info = info.TrimStart('-');
                Debug.Log(info);
                info = string.Empty;
            }*/
            #endregion

        }
    }

}

/// <summary>
/// 表示一行
/// </summary>
public struct Row
{
    public string rowText;              
    public List<Coll> colls;

    public Row(string line) 
    {
        rowText = line;
        string [] tempColls = line.Split(',');

        colls = new List<Coll>();
        for (int i = 0; i < tempColls.Length; i++)
        {
            colls.Add(new Coll(tempColls[i]));
        }
    }

    public string GetCell(int index) 
    {
        return colls[index].ToString();
    }

    public string ToString() 
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < colls.Count; i++)
        {
            sb.Append("-" + colls[i].ToString());
            
        }
        //去掉最后的"-"
        return sb.ToString().TrimStart('-');
    }

    /// <summary>
    /// 让结构可以直接[下标值] 来获取或设置 单元格的字符串
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public string this[int index]
    {
        get 
        {
            return colls[index].ToString();
        }
        set 
        {
            this.colls[index].SetText(value);
        }
    }

}

/// <summary>
/// 每一个单元格
/// </summary>
public struct Coll
{
    public string cellText;

    public Coll(string cell) 
    {
        cellText = cell;
    }

    /// <summary>
    /// 写一个ToString()方便获取字符串
    /// </summary>
    /// <returns></returns>
    public string ToString() 
    {
        return cellText;
    }

    /// <summary>
    /// 设置单元格的字符串
    /// </summary>
    /// <param name="text"></param>
    public void SetText(string text) 
    {
        cellText = text;
    }


}

/// <summary>
/// 一张表
/// </summary>
public struct CSVTable
{
    /// <summary>
    /// 行集合
    /// </summary>
    public List<Row> rows;

    /// <summary>
    /// 增加一行数据
    /// </summary>
    /// <param name="line">以","分割的一行文本</param>
    public void AddRow(string line) 
    {
        if (rows == null) 
        {
            rows = new List<Row>();
        }
        rows.Add(new Row(line));
    }




}

免责声明:文章转载自《Unity 读取Excel》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇蜕变成蝶~Linux设备驱动之异步通知和异步I/Omysql出现2003——can't connect to mysql server on localhost(10061)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用Unity实现VR中在黑板上写字(初级篇)

一、工具 1.开发用的是Unity 5.6.2版本 2.VR中的物理交互用的是VRTK插件,这个插件集成了比较好的物理交互功能; 3.HTC Vive 二、概述 实现的功能: 在一个白板上,用不同颜色的笔,在白板画出任何想要的图形; 因为只是一个初级篇所以只是用两个脚本简单的实现,而且并没有黑板擦等功能 ,也不能两个笔同时画画,这些功能将会在未来的升级篇...

Unity资源内存管理--webstream控制

一 使用前提 1,需要使用资源热更新 2,使用Assetbundle资源热更(AssetBundle是产生webstream的元凶) 二 为什么要用AssetBundle AssetBundle本质上就是一个压缩算法,只不过比起zip等一些压缩多了一些信息,比如平台信息(Ios,android),依赖信息等,既然是压缩,那就很好理解了,AssetBundl...

Unity应用架构设计(2)——使用中介者模式解耦ViewModel之间通信

当你开发一个客户端应用程序的时候,往往一个单页会包含很多子模块,在不同的平台下,这些子模块又被叫成子View(视图),或者子Component(组件)。越是复杂的页面,被切割出来的子模块就越多,子模块越多,彼此之间需要同步的数据和状态就越频繁,即易产生耦合。那么如何保证在复杂业务情况下,各个子模块之间可以随意通信并保持弱耦合关系,这正是本文所讨论的。 耦合...

「Unity」键盘、鼠标,检测按键输入事件

必须在Update()方法中调用 void Update() { //识别键盘输入 //键盘A键按住 //第一种方式 "a" 必须是小写字母 if (Input.GetKey("a")) { } //第二种方式 KeyCode的枚举类型...

Unity CombineChildren和MeshCombineUtility

原理 Unity3D如何通过CombineChildren和MeshCombineUtility优化场景? 首先解释下联结的原理和意思:文档里说,显卡对于一个含100个面片的物体的和含1500个面片的物体的渲染消耗几乎是等价的。所以如果你有N个同一材质的东西,那么把他们联成同一个物体再统一用一个material那么对于显卡的渲染消耗就要降低N倍。 方法 1...

unity3d中dllimport方法的使用,以接入腾讯平台为例!!!

   转载自:http://momowing.diandian.com/post/2012-12-14/40047766982   如果有侵权,请通知我尽快删除! 说到有关dllimport方法可能还有很多人比较陌生,其实我自己也说不太清楚,大概说说什么时候要用它。 事实上功能类似于调用android的第三包,我们想要使用苹果上特定的api或者第三方平台的...