zoukankan      html  css  js  c++  java
  • Unity 读取Excel

    游戏有大多数配置文件,比如玩家等级,游戏商店信息等等.通常情况下把这些放入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));
        }
    
    
    
    
    }
    如果你感兴趣,你可以把你妹妹介绍给我
  • 相关阅读:
    Django使用xadmin集成富文本编辑器Ueditor(方法二)
    Django-xadmin后台配置富文本编辑器(方法一)
    求解函数不等式[给定具体函数]
    区间断想
    函数方程和函数不等式
    对函数的再理解
    多题一解
    破解函数性质中的表达难点
    [数学模型]应用举例
    函数的迭代
  • 原文地址:https://www.cnblogs.com/plateFace/p/4337310.html
Copyright © 2011-2022 走看看