10 changed files with 118 additions and 115 deletions
@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2 |
||||
guid: e67c6d5e54b9de94ab4e5d1865feb935 |
||||
guid: 5e0421ee85549f24b833a7ea9e0161d7 |
||||
folderAsset: yes |
||||
DefaultImporter: |
||||
externalObjects: {} |
@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 5d9d9d9b6a4a85b468a248932c5ffc68 |
||||
guid: 1dbc2c949eb53844c818697026072761 |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 11400000 |
@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 20719aba661cd244f9d4e3b1f45a3e14 |
||||
guid: 623b6ecbcd0b3db48a888fc162488552 |
||||
NativeFormatImporter: |
||||
externalObjects: {} |
||||
mainObjectFileID: 11400000 |
@ -1,8 +0,0 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 0640585ce4cc1ab43ad546d3258130dc |
||||
folderAsset: yes |
||||
DefaultImporter: |
||||
externalObjects: {} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
@ -0,0 +1,46 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using UnityEngine; |
||||
|
||||
namespace Tools.ExcelResolver.Editor |
||||
{ |
||||
internal static partial class ExcelResolverUtil |
||||
{ |
||||
/// <summary> |
||||
/// 更具类型字符串获取 TType |
||||
/// </summary> |
||||
/// <param name="typeText"></param> |
||||
/// <returns></returns> |
||||
internal static TType GetTTypeByString(string typeText) |
||||
{ |
||||
return typeText switch |
||||
{ |
||||
"int" => new TInt(), |
||||
"float" => new TFloat(), |
||||
"string" => new TString(), |
||||
"bool" => new TBool(), |
||||
// "Vector2" => typeof(Vector2), |
||||
// "Vector3" => typeof(Vector3), |
||||
// |
||||
// "List<int>" => typeof(List<int>), |
||||
// "List<float>" => typeof(List<float>), |
||||
// "List<string>" => typeof(List<string>), |
||||
// "List<bool>" => typeof(List<bool>), |
||||
// "List<Vector2>" => typeof(List<Vector2>), |
||||
// "List<Vector3>" => typeof(List<Vector3>), |
||||
// |
||||
// "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>>), |
||||
// "List<List<Vector2>>" => typeof(List<List<Vector2>>), |
||||
// "List<List<Vector3>>" => typeof(List<List<Vector3>>), |
||||
// |
||||
// "enum" => typeof(Enum), |
||||
// "DateTime" => typeof(DateTime), |
||||
_ => null, |
||||
}; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 0a88f86b983a4118983088d69a474898 |
||||
timeCreated: 1737274824 |
@ -1,140 +1,102 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using UnityEngine; |
||||
|
||||
namespace Tools.ExcelResolver.Editor |
||||
{ |
||||
internal static partial class ExcelResolverUtil |
||||
{ |
||||
|
||||
internal static TType GetTTypeByString(string typeText) |
||||
{ |
||||
return typeText switch |
||||
{ |
||||
"int" => new TInt(), |
||||
"float" => new TFloat(), |
||||
"string" => new TString(), |
||||
"bool" => new TBool(), |
||||
// "Vector2" => typeof(Vector2), |
||||
// "Vector3" => typeof(Vector3), |
||||
// |
||||
// "List<int>" => typeof(List<int>), |
||||
// "List<float>" => typeof(List<float>), |
||||
// "List<string>" => typeof(List<string>), |
||||
// "List<bool>" => typeof(List<bool>), |
||||
// "List<Vector2>" => typeof(List<Vector2>), |
||||
// "List<Vector3>" => typeof(List<Vector3>), |
||||
// |
||||
// "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>>), |
||||
// "List<List<Vector2>>" => typeof(List<List<Vector2>>), |
||||
// "List<List<Vector3>>" => typeof(List<List<Vector3>>), |
||||
// |
||||
// "enum" => typeof(Enum), |
||||
// "DateTime" => typeof(DateTime), |
||||
// _ => GetType(typeText) |
||||
}; |
||||
} |
||||
|
||||
internal static Type GetTypeByString(string typeText) |
||||
{ |
||||
return typeText switch |
||||
{ |
||||
"int" => typeof(int), |
||||
"float" => typeof(float), |
||||
"string" => typeof(string), |
||||
"bool" => typeof(bool), |
||||
"Vector2" => typeof(Vector2), |
||||
"Vector3" => typeof(Vector3), |
||||
|
||||
"List<int>" => typeof(List<int>), |
||||
"List<float>" => typeof(List<float>), |
||||
"List<string>" => typeof(List<string>), |
||||
"List<bool>" => typeof(List<bool>), |
||||
"List<Vector2>" => typeof(List<Vector2>), |
||||
"List<Vector3>" => typeof(List<Vector3>), |
||||
|
||||
"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>>), |
||||
"List<List<Vector2>>" => typeof(List<List<Vector2>>), |
||||
"List<List<Vector3>>" => typeof(List<List<Vector3>>), |
||||
|
||||
"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> |
||||
internal 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}"); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// 类型缓存,避免重复反射查找 |
||||
/// StringComparer.OrdinalIgnoreCase: 忽略键的大小写 |
||||
/// </summary> |
||||
internal static readonly Dictionary<string, Type> TypeCache = new(StringComparer.OrdinalIgnoreCase); |
||||
static readonly Dictionary<string, Type> TypeCache = new(StringComparer.OrdinalIgnoreCase); |
||||
|
||||
/// <summary> |
||||
/// 通过类名(含命名空间)获取 Type,并缓存 |
||||
/// </summary> |
||||
internal static Type GetOrCacheTypeByName(string typeName) |
||||
{ |
||||
if (TypeCache.TryGetValue(typeName, out Type cachedType)) |
||||
typeName = typeName.Trim(); |
||||
|
||||
// 如果缓存中存在,直接返回 |
||||
if (TypeCache.TryGetValue(typeName, out var result)) |
||||
{ |
||||
return cachedType; |
||||
return result; |
||||
} |
||||
|
||||
string namespacedType = $"Tools.ExcelResolver.{typeName}"; |
||||
Type type = Type.GetType(namespacedType, false, true) ?? GetTypeFromAllAssemblies(namespacedType); |
||||
|
||||
if (type != null) |
||||
// 尝试从 Tools.ExcelResolver 命名空间下查找 |
||||
string fullTypeName = $"Tools.ExcelResolver.{typeName}"; |
||||
result = Type.GetType(fullTypeName, false, true) ?? GetTypeFromNecessaryAssemblies(fullTypeName); |
||||
if (result != null) |
||||
{ |
||||
TypeCache[typeName] = type; |
||||
return result; |
||||
} |
||||
|
||||
return type; |
||||
|
||||
throw new ArgumentException($"Unsupported type: {typeName}"); |
||||
} |
||||
|
||||
internal static Type GetTypeFromAllAssemblies(string typeName) |
||||
/// <summary> |
||||
/// 从所有程序集中查找类型 |
||||
/// </summary> |
||||
/// <param name="fullTypeName"></param> |
||||
/// <returns></returns> |
||||
/// <exception cref="ArgumentException"></exception> |
||||
static Type GetTypeFromNecessaryAssemblies(string fullTypeName) |
||||
{ |
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) |
||||
var assemblies = AppDomain.CurrentDomain.GetAssemblies() |
||||
.Where(a => a.GetName().Name |
||||
is "UnityEngine" |
||||
or "Assembly-CSharp" |
||||
or "Assembly-CSharp-firstpass" |
||||
or "Assembly-CSharp-Editor" |
||||
or "Assembly-CSharp-Editor-firstpass"); |
||||
|
||||
foreach (var assembly in assemblies) |
||||
{ |
||||
var type = assembly.GetType(typeName); |
||||
var type = assembly.GetType(fullTypeName); |
||||
if (assembly.GetName().Name is "Assembly-CSharp-Editor" or "Assembly-CSharp-Editor-firstpass") |
||||
{ |
||||
throw new ArgumentException($"不支持Editor目录下的'{fullTypeName}'类型"); |
||||
} |
||||
if (type != null) |
||||
{ |
||||
TypeCache[typeName] = type; |
||||
TypeCache[fullTypeName] = type; |
||||
return type; |
||||
} |
||||
} |
||||
|
||||
throw new ArgumentException($"Unsupported type: {typeName}"); |
||||
|
||||
return null; |
||||
} |
||||
|
||||
internal static void Dispose() |
||||
{ |
||||
TypeCache.Clear(); |
||||
} |
||||
|
||||
/* |
||||
/// <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> |
||||
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}"); |
||||
} |
||||
*/ |
||||
} |
||||
} |
Loading…
Reference in new issue