设置相关功能
This commit is contained in:
@@ -1,133 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public static class TypeCache
|
||||
{
|
||||
private static readonly Dictionary<Type, List<Type>> _subTypeCache = new Dictionary<Type, List<Type>>();
|
||||
private static readonly Dictionary<Type, List<Type>> _attributeCache = new Dictionary<Type, List<Type>>();
|
||||
private static readonly Dictionary<string, Type> _typeNameCache = new Dictionary<string, Type>();
|
||||
private static Type[] _filteredTypes;
|
||||
|
||||
// 目标命名空间
|
||||
private static readonly string[] TargetNamespaces = { "NBF", "NBC" };
|
||||
|
||||
// 初始化缓存
|
||||
static TypeCache()
|
||||
{
|
||||
InitializeCache();
|
||||
}
|
||||
|
||||
// 初始化缓存(仅缓存指定命名空间的类型)
|
||||
private static void InitializeCache()
|
||||
{
|
||||
// 获取Unity默认的主程序集Assembly-CSharp
|
||||
var mainAssembly = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.FirstOrDefault(a => a.GetName().Name == "Assembly-CSharp");
|
||||
|
||||
if (mainAssembly == null)
|
||||
{
|
||||
Debug.LogError("找不到Assembly-CSharp程序集");
|
||||
_filteredTypes = Array.Empty<Type>();
|
||||
return;
|
||||
}
|
||||
|
||||
// 过滤出NBF和NBC命名空间的类型
|
||||
_filteredTypes = mainAssembly.GetTypes()
|
||||
.Where(t => TargetNamespaces.Any(ns => t.Namespace != null && t.Namespace.StartsWith(ns)))
|
||||
.ToArray();
|
||||
|
||||
// 预缓存类型名
|
||||
foreach (var type in _filteredTypes)
|
||||
{
|
||||
if (type.FullName != null) _typeNameCache[type.FullName] = type;
|
||||
}
|
||||
|
||||
Debug.Log($"TypeCache初始化完成,缓存了{_filteredTypes.Length}个类型(仅限NBF和NBC命名空间)");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有已缓存的类型
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Type[] GetAllTypes()
|
||||
{
|
||||
if (_filteredTypes == null)
|
||||
{
|
||||
InitializeCache();
|
||||
}
|
||||
|
||||
return _filteredTypes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过基类或接口获取所有子类/实现类
|
||||
/// </summary>
|
||||
/// <typeparam name="TBase"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static List<Type> GetTypesWithBaseType<TBase>() where TBase : class
|
||||
{
|
||||
return GetTypesWithBaseType(typeof(TBase));
|
||||
}
|
||||
|
||||
public static List<Type> GetTypesWithBaseType(Type baseType)
|
||||
{
|
||||
if (_subTypeCache.TryGetValue(baseType, out var cachedTypes))
|
||||
{
|
||||
return cachedTypes;
|
||||
}
|
||||
|
||||
var types = GetAllTypes()
|
||||
.Where(t => baseType.IsAssignableFrom(t) && t != baseType && !t.IsAbstract && !t.IsInterface)
|
||||
.ToList();
|
||||
|
||||
_subTypeCache[baseType] = types;
|
||||
return types;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过特性获取所有类型
|
||||
/// </summary>
|
||||
/// <typeparam name="TAttribute"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static List<Type> GetTypesWithAttribute<TAttribute>() where TAttribute : Attribute
|
||||
{
|
||||
var attributeType = typeof(TAttribute);
|
||||
if (_attributeCache.TryGetValue(attributeType, out var cachedTypes))
|
||||
{
|
||||
return cachedTypes;
|
||||
}
|
||||
|
||||
var types = GetAllTypes()
|
||||
.Where(t => t.IsDefined(attributeType, false))
|
||||
.ToList();
|
||||
|
||||
_attributeCache[attributeType] = types;
|
||||
return types;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过类型名获取类型
|
||||
/// </summary>
|
||||
/// <param name="typeName"></param>
|
||||
/// <returns></returns>
|
||||
public static Type GetTypeByName(string typeName)
|
||||
{
|
||||
return _typeNameCache.GetValueOrDefault(typeName);
|
||||
}
|
||||
|
||||
// 清除缓存
|
||||
public static void ClearCache()
|
||||
{
|
||||
_subTypeCache.Clear();
|
||||
_attributeCache.Clear();
|
||||
_typeNameCache.Clear();
|
||||
_filteredTypes = null;
|
||||
Debug.Log("TypeCache已清除");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 12e6673112ac446b8d4658b32d38a5b3
|
||||
timeCreated: 1748510780
|
||||
Reference in New Issue
Block a user