fix script search by name.

This commit is contained in:
DCFApixels 2025-03-14 20:20:47 +08:00
parent 88f729b868
commit 2eeffaf74f
2 changed files with 32 additions and 6 deletions

View File

@ -121,7 +121,6 @@ namespace DCFApixels.DragonECS.Unity.Editors
#endregion #endregion
#region Get #region Get
public static bool TryGetScriptAsset(Type type, out MonoScript script) { return TryGetScriptAsset(type.ToMeta(), out script); }
public static bool TryGetScriptAsset(TypeMeta meta, out MonoScript script) public static bool TryGetScriptAsset(TypeMeta meta, out MonoScript script)
{ {
int uniqueID = meta.GetHashCode(); int uniqueID = meta.GetHashCode();
@ -154,25 +153,52 @@ namespace DCFApixels.DragonECS.Unity.Editors
if (script == null) if (script == null)
{ {
//Ищем совпадения имет в ассетах //Ищем совпадения имен в ассетах
string name = meta.TypeName; string name = meta.TypeName;
int genericTypeCharIndex = name.IndexOf('<'); int genericTypeCharIndex = name.IndexOf('<');
if (genericTypeCharIndex >= 0) if (genericTypeCharIndex >= 0)
{ {
name = name.Substring(0, genericTypeCharIndex); name = name.Substring(0, genericTypeCharIndex);
} }
var guids = AssetDatabase.FindAssets($"{name} t:MonoScript"); string[] guids = AssetDatabase.FindAssets($"{name} t:MonoScript");
string[] skipped = Array.Empty<string>();
int skippedCount = 0;
for (var i = 0; i < guids.Length; i++) for (var i = 0; i < guids.Length; i++)
{ {
MonoScript textAsset = AssetDatabase.LoadAssetAtPath<MonoScript>(AssetDatabase.GUIDToAssetPath(guids[i])); string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
if (assetPath.IndexOf("Packages/com.unity.") == 0)
{
if(skippedCount == 0)
{
skipped = new string[guids.Length];
}
skipped[skippedCount++] = assetPath;
continue;
}
MonoScript textAsset = AssetDatabase.LoadAssetAtPath<MonoScript>(assetPath);
if (textAsset != null && textAsset.name == name) if (textAsset != null && textAsset.name == name)
{ {
script = textAsset; script = textAsset;
break; break;
} }
} }
}
if(script == null)
{
foreach (var assetPath in new ReadOnlySpan<string>(skipped, 0, skippedCount))
{
MonoScript textAsset = AssetDatabase.LoadAssetAtPath<MonoScript>(assetPath);
if (textAsset != null && textAsset.name == name)
{
script = textAsset;
break;
}
}
}
}
_scriptsAssets.Add(uniqueID, script); _scriptsAssets.Add(uniqueID, script);
} }
return script != null; return script != null;

View File

@ -261,7 +261,7 @@ namespace DCFApixels.DragonECS.Unity.Docs.Editors
{ {
GUILayout.TextArea(IsUseCustomNames ? meta.Name : meta.TypeName, EditorStyles.boldLabel, GUILayout.ExpandWidth(false)); GUILayout.TextArea(IsUseCustomNames ? meta.Name : meta.TypeName, EditorStyles.boldLabel, GUILayout.ExpandWidth(false));
if (meta.TryGetSourceType(out System.Type targetType) && ScriptsCache.TryGetScriptAsset(targetType, out MonoScript script)) if (meta.TryGetSourceType(out System.Type targetType) && ScriptsCache.TryGetScriptAsset(targetType.ToMeta(), out MonoScript script))
{ {
EcsGUI.Layout.ScriptAssetButton(script, GUILayout.Width(18f)); EcsGUI.Layout.ScriptAssetButton(script, GUILayout.Width(18f));
} }