Excel 转 unity 配置脚本
.net core 3.1
介绍
支持excel 公式,且只支持 .xlsx 格式文档
支持的数据类型
数据类型 | 表格 |
---|---|
String | string |
Double | double |
Float | float |
Int | int |
Vector2 | v2 |
Vector3 | v3 |
Bool | bool |
excel 配置![excel 配置](https://i-blog.csdnimg.cn/blog_migrate/65049412e4335f1e1e04ae294be12bbc.png#pic_center)
类型为空 则自动引用前一个类型
相同变量名称为同一数组
/****************************************************
*
* 本文件由CSV 文件生成工具自动生成
* LevelFaceCfg.csv LevelFaceCfg.cs
*
*****************************************************/
public class LevelFaceCfg : CSVBaseConfig<LevelFaceCfg,LevelFaceCfg.LevelFaceCfgData>
{
public class LevelFaceCfgData
{
public int Level;
public string Theme;
public string HairType;
public string Base_Hair;
public string[] Hair;
}
protected override void Init()
{
Data.Add(new LevelFaceCfgData()
{
Level = 0,
Theme = "Happy",
HairType = "Model",
Base_Hair = "Hair_Model_01_001",
Hair = new string[3]{
"None",
"None",
"None"
}
});
Data.Add(new LevelFaceCfgData()
{
Level = 1,
Theme = "Happy",
HairType = "Model",
Base_Hair = "Hair_Model_03_001",
Hair = new string[3]{
"Hair_Model_01_001",
"Hair_Model_02_001",
"Hair_Model_06_001"
}
});
}
}
生成的配置文件
命令行参数
CSVBaseConfig.cs
/****************************************************
* FileName: GunConfig
* CreateTime: 2021-05-11 17:49:33
* Version: 1.0
* UnityVersion: 2020.3.5f1c1
* Description: Nothing
*
*****************************************************/
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// SCV 基本数据读取
/// </summary>
public class CSVBaseConfig<T, C> : Single<T> where T : CSVBaseConfig<T, C>, new()
where C : new()
{
protected List<C> Data = new List<C>();
/// <summary>
/// 源数据
/// </summary>
public List<C> GetData { get => Data; }
protected override void Init() { }
public C Find(Predicate<C> func)
{
return Data.Find(func);
}
protected bool ToBool(string[] str, int index)
{
if (str.Length > index)
{
if (bool.TryParse(str[index], out bool res))
return res;
}
return false;
}
protected string ToString(string[] str, int index)
{
if (str.Length > index)
return str[index];
return string.Empty;
}
protected Color ToColor(string[] str, int index)
{
if (str.Length > index)
{
if (ColorUtility.TryParseHtmlString("#" + str[index], out Color color))
return color;
}
return Color.white;
}
protected int ToInt(string[] str, int index)
{
if (str.Length > index)
if (int.TryParse(str[index], out int f))
return f;
return 0;
}
protected float ToFloat(string[] str, int index)
{
if (str.Length > index)
if (float.TryParse(str[index], out float f))
return f;
return 0;
}
}
Single.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Single<T> where T : Single<T>, new()
{
private static T _instance;
public static T Ins
{
get
{
if (null == _instance)
{
_instance = new T();
_instance.Init();
}
return _instance;
}
}
/// <summary>
/// 类实例化
/// </summary>
protected virtual void Init()
{
//TODO 类实例化
}
}