Browse Source

获取类型

master
coffee 6 months ago
parent
commit
dfbb43ee1e
  1. 36
      Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.ReadExcel.cs
  2. 11
      Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteDataCode.cs
  3. 55
      Assets/Unity-Tools/Core/Util/TypeUtil.cs
  4. 3
      Assets/Unity-Tools/Core/Util/TypeUtil.cs.meta

36
Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.ReadExcel.cs

@ -11,11 +11,11 @@ namespace Tools.ExcelResolver.Editor
{ {
private enum TableType private enum TableType
{ {
SingleKeyTable, // 单主键表 SingleKeyTable, // 单主键表
UnionMultiKeyTable, // 多主键表(联合索引) UnionMultiKeyTable, // 多主键表(联合索引)
MultiKeyTable, // 多主键表(独立索引) MultiKeyTable, // 多主键表(独立索引)
NotKetTable, // 无主键表 NotKetTable, // 无主键表
ColumnTable, // 纵表 ColumnTable, // 纵表
} }
@ -38,10 +38,7 @@ namespace Tools.ExcelResolver.Editor
} }
var type = CheckTableType(worksheet); var type = CheckTableType(worksheet);
foreach (var i in keyIndex) GetFieldData(worksheet);
{
Debug.Log($"{type} {i}");
}
} }
} }
@ -50,7 +47,7 @@ namespace Tools.ExcelResolver.Editor
var startColumn = worksheet.Dimension.Start.Column; // 起始列 var startColumn = worksheet.Dimension.Start.Column; // 起始列
var endColumn = worksheet.Dimension.End.Column; // 结束列 var endColumn = worksheet.Dimension.End.Column; // 结束列
string config = worksheet.Cells[1, 1].Value.ToString(); string config = worksheet.Cells[1, 1].Text;
var type = TableType.SingleKeyTable; var type = TableType.SingleKeyTable;
if (config.Contains("SingleKeyTable")) if (config.Contains("SingleKeyTable"))
{ {
@ -98,6 +95,10 @@ namespace Tools.ExcelResolver.Editor
{ {
type = TableType.ColumnTable; type = TableType.ColumnTable;
} }
else
{
Debug.LogError("配置错误");
}
return type; return type;
@ -117,5 +118,20 @@ namespace Tools.ExcelResolver.Editor
return keyIndex; return keyIndex;
} }
} }
private void GetFieldData(ExcelWorksheet worksheet)
{
var startColumn = worksheet.Dimension.Start.Column; // 起始列
var endColumn = worksheet.Dimension.End.Column; // 结束列
Debug.Log(endColumn);
for (int col = 2; col <= endColumn; col++)
{
FieldData fieldData = new FieldData();
fieldData.name = worksheet.Cells[2, col].Text;
fieldData.description = worksheet.Cells[4, col].Text;
fieldData.type = TypeUtil.GetType(worksheet.Cells[3, col].Text);
Debug.Log(fieldData.type);
}
}
} }
} }

11
Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteDataCode.cs

@ -1,7 +1,16 @@
using Tools.Editor.CodeGenKit; using System;
using Tools.Editor.CodeGenKit;
namespace Tools.ExcelResolver.Editor namespace Tools.ExcelResolver.Editor
{ {
internal class FieldData
{
public string name;
public Type type;
public string description;
}
public sealed partial class ExcelResolverEditorWindow public sealed partial class ExcelResolverEditorWindow
{ {
private int[] keyIndex; private int[] keyIndex;

55
Assets/Unity-Tools/Core/Util/TypeUtil.cs

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
namespace Tools
{
public static class TypeUtil
{
public static Type GetTypeByString(string typeText)
{
return typeText switch
{
"int" => typeof(int),
"float" => typeof(float),
"string" => typeof(string),
"bool" => typeof(bool),
"List<int>" => typeof(List<int>),
"List<float>" => typeof(List<float>),
"List<string>" => typeof(List<string>),
"List<bool>" => typeof(List<bool>),
"List<List<int>>" => typeof(List<List<int>>),
"List<List<float>>" => typeof(List<List<float>>),
"List<List<string>>" => typeof(List<List<string>>),
"List<List<bool>>" => typeof(List<List<bool>>),
"enum" => typeof(Enum),
"DateTime" => typeof(DateTime),
_ => GetType(typeText)
};
}
/// <summary>
/// 参考:https://learn.microsoft.com/zh-cn/dotnet/api/system.type.gettype?view=net-8.0
/// </summary>
/// <param name="typeText"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static Type GetType(string typeText)
{
// 首先尝试使用Type.GetType
Type type = Type.GetType($"System.{typeText}", false, true);
if (type != null) return type;
// 如果失败,尝试在UnityEngine命名空间下查找
// 参数一:"[命名空间.类型名], [程序集名]"
// 参数二:是否抛出异常
// 参数三:是否区分大小写
type = Type.GetType($"UnityEngine.{typeText}, UnityEngine", false, true);
if (type != null) return type;
throw new ArgumentException($"Unsupported type: {typeText}");
}
}
}

3
Assets/Unity-Tools/Core/Util/TypeUtil.cs.meta

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5e00e18dfb2244dbb54a891cd3505c9c
timeCreated: 1736011944
Loading…
Cancel
Save