Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 46 additions & 45 deletions SkillsForUnity/Editor/Skills/SkillRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -340,54 +341,54 @@ public static void Initialize()
var skills = new Dictionary<string, SkillInfo>(StringComparer.OrdinalIgnoreCase);
var trackedSkills = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

var allTypes = SkillsCommon.GetAllLoadedTypes();

foreach (var type in allTypes)
// 使用 Unity 编辑器索引直接查询 Skill 方法,避免在 Domain Reload 后枚举全部程序集和类型。
var methods = TypeCache.GetMethodsWithAttribute<UnitySkillAttribute>();
foreach (var method in methods)
{
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
if (!method.IsPublic || !method.IsStatic)
continue;

UnitySkillAttribute attr;
try { attr = method.GetCustomAttribute<UnitySkillAttribute>(); }
catch { continue; }
if (attr != null)
{
UnitySkillAttribute attr;
try { attr = method.GetCustomAttribute<UnitySkillAttribute>(); }
catch { continue; }
if (attr != null)
var name = attr.Name ?? ToSnakeCase(method.Name);
var parameters = method.GetParameters();
var parameterNames = parameters.Select(p => p.Name).ToArray();
var allowedSet = new HashSet<string>(parameterNames, StringComparer.OrdinalIgnoreCase);
allowedSet.UnionWith(_reservedBodyParameters);
if (!allowedSet.Contains(EntityIdParameterName) && SupportsSyntheticEntityId(parameterNames))
allowedSet.Add(EntityIdParameterName);
skills[name] = new SkillInfo
{
var name = attr.Name ?? ToSnakeCase(method.Name);
var parameters = method.GetParameters();
var parameterNames = parameters.Select(p => p.Name).ToArray();
var allowedSet = new HashSet<string>(parameterNames, StringComparer.OrdinalIgnoreCase);
allowedSet.UnionWith(_reservedBodyParameters);
if (!allowedSet.Contains(EntityIdParameterName) && SupportsSyntheticEntityId(parameterNames))
allowedSet.Add(EntityIdParameterName);
skills[name] = new SkillInfo
{
Name = name,
Description = attr.Description ?? "",
Method = method,
Parameters = parameters,
TracksWorkflow = attr.TracksWorkflow,
Category = attr.Category,
Operation = attr.Operation,
Tags = attr.Tags,
Outputs = attr.Outputs,
RequiresInput = attr.RequiresInput,
ReadOnly = attr.ReadOnly,
MutatesScene = attr.MutatesScene,
MutatesAssets = attr.MutatesAssets,
MayTriggerReload = attr.MayTriggerReload,
MayEnterPlayMode = attr.MayEnterPlayMode,
SupportsDryRun = attr.SupportsDryRun,
RiskLevel = attr.RiskLevel ?? "low",
RequiresPackages = attr.RequiresPackages,
Mode = attr.Mode,
ParameterNames = parameterNames,
AllowedParameterSet = allowedSet,
NameLower = name.ToLowerInvariant(),
DescriptionLower = (attr.Description ?? "").ToLowerInvariant(),
TagsLower = attr.Tags?.Select(t => t.ToLowerInvariant()).ToArray()
};
if (attr.TracksWorkflow)
trackedSkills.Add(name);
}
Name = name,
Description = attr.Description ?? "",
Method = method,
Parameters = parameters,
TracksWorkflow = attr.TracksWorkflow,
Category = attr.Category,
Operation = attr.Operation,
Tags = attr.Tags,
Outputs = attr.Outputs,
RequiresInput = attr.RequiresInput,
ReadOnly = attr.ReadOnly,
MutatesScene = attr.MutatesScene,
MutatesAssets = attr.MutatesAssets,
MayTriggerReload = attr.MayTriggerReload,
MayEnterPlayMode = attr.MayEnterPlayMode,
SupportsDryRun = attr.SupportsDryRun,
RiskLevel = attr.RiskLevel ?? "low",
RequiresPackages = attr.RequiresPackages,
Mode = attr.Mode,
ParameterNames = parameterNames,
AllowedParameterSet = allowedSet,
NameLower = name.ToLowerInvariant(),
DescriptionLower = (attr.Description ?? "").ToLowerInvariant(),
TagsLower = attr.Tags?.Select(t => t.ToLowerInvariant()).ToArray()
};
if (attr.TracksWorkflow)
trackedSkills.Add(name);
}
}

Expand Down
37 changes: 19 additions & 18 deletions SkillsForUnity/Editor/UI/UnitySkillsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,28 +226,29 @@ public void RefreshLocalization()
public void RefreshSkillsList()
{
_skillsByCategory = new Dictionary<string, List<SkillInfo>>();
var allTypes = SkillsCommon.GetAllLoadedTypes();
// 与路由器使用相同的 Unity 编辑器索引,避免窗口刷新时再次全量枚举类型。
var methods = TypeCache.GetMethodsWithAttribute<UnitySkillAttribute>();

foreach (var type in allTypes)
foreach (var method in methods)
{
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
{
UnitySkillAttribute attr;
try { attr = method.GetCustomAttribute<UnitySkillAttribute>(); }
catch { continue; }
if (attr == null) continue;
if (!method.IsPublic || !method.IsStatic || method.DeclaringType == null)
continue;

var category = type.Name.Replace("Skills", "");
if (!_skillsByCategory.ContainsKey(category))
_skillsByCategory[category] = new List<SkillInfo>();
UnitySkillAttribute attr;
try { attr = method.GetCustomAttribute<UnitySkillAttribute>(); }
catch { continue; }
if (attr == null) continue;

_skillsByCategory[category].Add(new SkillInfo
{
Name = attr.Name ?? method.Name,
Description = attr.Description ?? "",
Method = method
});
}
var category = method.DeclaringType.Name.Replace("Skills", "");
if (!_skillsByCategory.ContainsKey(category))
_skillsByCategory[category] = new List<SkillInfo>();

_skillsByCategory[category].Add(new SkillInfo
{
Name = attr.Name ?? method.Name,
Description = attr.Description ?? "",
Method = method
});
}
}

Expand Down