Excel 转 unity 配置脚本

该博客介绍了如何使用.NET Core 3.1脚本来将Excel数据转换为Unity配置文件。支持.xlsx格式,包含字符串、浮点数、整数、向量等数据类型,并能处理公式。生成的配置文件用于游戏等级和角色面部定制,支持数组和自动引用类型。博客还提供了一个命令行参数的示例以及关键转换函数。
摘要由CSDN通过智能技术生成

Excel 转 unity 配置脚本

.net core 3.1

介绍

支持excel 公式,且只支持 .xlsx 格式文档

仓库地址

支持的数据类型
数据类型表格
Stringstring
Doubledouble
Floatfloat
Intint
Vector2v2
Vector3v3
Boolbool

excel 配置excel 配置

类型为空 则自动引用前一个类型
相同变量名称为同一数组

在这里插入图片描述

/****************************************************
*
*         本文件由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 类实例化
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值