Unity 处理策划的 Excel

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

   因为很多数据都是策划处理的,而策划最喜欢的就是excel,也只会用这个。如果不用excel,意味着数据需要你自己输入,纯属浪费时间。

   所以很多时间我们和策划约定好VO类的字段,直接从Excel映射出VO类。

   

   比如下面的这张表

另存为.csv 然后转换为.txt 注意编码都改为UTF-8 

 

做好配置文件后,VO类映射就可以了,举个例子:

Vo 类

 1     public class HeroVo
 2     {
 3         public int heroId;
 4 
 5         public string heroName;///武将名称
 6 
 7         //public int heroStar;///品质
 8         public int bingZhongId;///职业
 9 
10         public string heroIcon;///icon(头像)
11 
12         public string heroFace;///face(形象)
13 
14         public string heroDescription;///描述
15 
16         public float hp;
17 
18         public float grownHp;
19 
20         public float attack;
21 
22         public float grownAttack;
23 
24         public float defense;
25 
26         public float grownDefense;
27 
28         public float luckRate; /// 暴击率
29 
30         public float missRate;///闪避
31 
32         public int skill1_Id;
33 
34         public int skill2_Id;
35 
36         public HeroVo (Dictionary<string,string> paramters)
37         {
38             FieldInfo[] fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
39 
40             foreach (var item in paramters.Keys)
41             {
42                 FieldInfo[] curfields =  fields.Where(t => t.Name == item).ToArray();
43                 
44                 if (curfields != null && curfields.Length == 1)
45                 {
46                     //Debug.Log("key:" + item + "value:" + paramters[item]);
47 
48                     FieldInfo curField = curfields[0];
49 
50                     if (curField.FieldType == typeof(int))
51                     {
52                         curField.SetValue(this, int.Parse(paramters[item]));
53                     }
54                     else if (curField.FieldType == typeof(string))
55                     {
56                         curField.SetValue(this, paramters[item]);
57                     }
58                     else if (curField.FieldType == typeof(float))
59                     {
60                         curField.SetValue(this, float.Parse(paramters[item]));
61                     }
62                 }
63             }
64         }
65 
66 
67         public override string ToString()
68         {
69             StringBuilder builder = new StringBuilder();
70 
71             FieldInfo[] fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
72             foreach (var item in fields)
73             {
74                 builder.Append(" " + item.Name + " : " + item.GetValue(this) + " ");
75             }
76 
77             return builder.ToString();
78         }
79     }

 

Vo 类集合解析

 1 public class StaticHeroDataPool
 2 {
 3     public static string ResourceDataPath = "battle/Data/Hero/StaticHeroDataModel";
 4 
 5     public List<HeroVo> heros = new List<HeroVo>();
 6 
 7     static StaticHeroDataPool _instatnce = null;
 8     public static StaticHeroDataPool Instatnce
 9     {
10         get
11         {
12             if (_instatnce == null)
13             {
14                 _instatnce = new StaticHeroDataPool();
15 
16                 _instatnce.AddData(LoadData(ResourceDataPath));
17             }
18             return _instatnce;
19         }
20     }
21 
22     private static string[] LoadData(string loadUrl)
23     {
24         TextAsset binAsset = Resources.Load(loadUrl, typeof(TextAsset)) as TextAsset;
25         
26         string[] lineArray = binAsset.text.Split("\n"[0]);
27       
28         /// 去掉最后一空行
29         int length = lineArray.Length;
30         string[] newlineArray = lineArray.Where((t, index) => (index < length - 1)).ToArray();
31 
32         return newlineArray;
33     }
34 
35     private void AddData(string[] lineArray)
36     {
37         string[] fieldNames = lineArray[0].Replace("\r", "").Split(";"[0]);
38 
39         for (int i = 1; i < lineArray.Length; i++)
40         {
41             string[] Values = lineArray[i].Replace("\r", "").Split(";"[0]);
42 
43             Dictionary<string, string> paramters = new Dictionary<string, string>();
44             for (int j = 0; j < fieldNames.Length; j++)
45             {
46                 paramters.Add(fieldNames[j], Values[j]);
47             }
48 
49             HeroVo heroVo = new HeroVo(paramters);
50             heros.Add(heroVo);
51         }
52     }
53 }

 

转载于:https://www.cnblogs.com/chongxin/p/4454232.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中读取Excel文件,你可以使用第三方库来实现。下面是一种常用的方法: 1. 下载并导入NPOI库(https://github.com/tonyqus/npoi)到你的Unity项目中。 2. 创建一个C#脚本,用于读取Excel文件的数据。下面是一个简单的示例: ```csharp using UnityEngine; using System.IO; using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; public class ExcelReader : MonoBehaviour { void Start() { // Excel文件路径 string filePath = Application.dataPath + "/your_excel_file.xls"; // 使用HSSFWorkbook类加载Excel文件 HSSFWorkbook workbook; using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { workbook = new HSSFWorkbook(fileStream); } // 获取第一个工作表 ISheet sheet = workbook.GetSheetAt(0); // 获取行数和列数 int rowCount = sheet.LastRowNum + 1; int columnCount = sheet.GetRow(0).LastCellNum; // 遍历每一行 for (int i = 0; i < rowCount; i++) { IRow row = sheet.GetRow(i); // 遍历每一列 for (int j = 0; j < columnCount; j++) { ICell cell = row.GetCell(j); // 获取单元格的值 string cellValue = cell.ToString(); // 在这里处理每个单元格的数据 Debug.Log("Cell[" + i + "," + j + "]: " + cellValue); } } } } ``` 确保将 `your_excel_file.xls` 替换为你的Excel文件的路径和名称。 3. 将脚本附加到一个游戏对象上,运行游戏即可读取Excel文件的数据。 这样就可以在Unity中读取Excel文件了。请注意,该示例使用的是NPOI库来读取.xls格式的文件,如果你需要读取.xlsx格式的文件,可以使用NPOI的另一个版本 NPOI.XSSF。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值