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
#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)
{
int uniqueID = meta.GetHashCode();
@ -154,17 +153,43 @@ namespace DCFApixels.DragonECS.Unity.Editors
if (script == null)
{
//Ищем совпадения имет в ассетах
//Ищем совпадения имен в ассетах
string name = meta.TypeName;
int genericTypeCharIndex = name.IndexOf('<');
if (genericTypeCharIndex >= 0)
{
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++)
{
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)
{
script = textAsset;
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;
@ -173,6 +198,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
}
}
}
_scriptsAssets.Add(uniqueID, script);
}
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));
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));
}