Skip to content

Commit 5f2e438

Browse files
committed
refactor: Removed the old AssetSearcher.FindObjectsWithValue() in favor of new ProjectWideSearcher
1 parent a54a6ea commit 5f2e438

File tree

6 files changed

+59
-378
lines changed

6 files changed

+59
-378
lines changed

Editor/Helpers/AssetHelper.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace SolidUtilities.Editor
2+
{
3+
using System;
4+
using Editor;
5+
using JetBrains.Annotations;
6+
using SolidUtilities;
7+
using UnityEditor;
8+
9+
public static class AssetHelper
10+
{
11+
/// <summary>
12+
/// Gets the GUID of an asset where the type is located.
13+
/// </summary>
14+
/// <param name="type">Type to search for in assets.</param>
15+
/// <param name="GUID">GUID of the asset where the type is located, or <c>null</c> if the asset was not found.</param>
16+
/// <param name="monoScript">MonoScript of the asset where the type is located, or <c>null</c> if the asset was not found.</param>
17+
/// <returns><c>true</c> if the asset with the specified type was found.</returns>
18+
[PublicAPI]
19+
[ContractAnnotation("=> true, GUID: notnull; => false, GUID: null")]
20+
public static bool GetAssetDetails(Type type, [CanBeNull] out string GUID, out MonoScript monoScript)
21+
{
22+
GUID = string.Empty;
23+
monoScript = null;
24+
25+
if (type == null)
26+
return false;
27+
28+
if (type.IsGenericType)
29+
type = type.GetGenericTypeDefinition();
30+
31+
string typeNameWithoutSuffix = type.Name.StripGenericSuffix();
32+
33+
foreach (string guid in AssetDatabase.FindAssets($"t:MonoScript {typeNameWithoutSuffix}"))
34+
{
35+
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
36+
var asset = AssetDatabase.LoadAssetAtPath<MonoScript>(assetPath);
37+
38+
if (asset is null || asset.GetClassType(typeNameWithoutSuffix) != type)
39+
continue;
40+
41+
GUID = guid;
42+
monoScript = asset;
43+
return true;
44+
}
45+
46+
return false;
47+
}
48+
49+
[NotNull]
50+
public static string GetClassGUID(Type type) =>
51+
GetAssetDetails(type, out string guid, out MonoScript _) ? guid : string.Empty;
52+
53+
public static MonoScript GetMonoScriptFromType(Type type)
54+
{
55+
GetAssetDetails(type, out string _, out MonoScript monoScript);
56+
return monoScript;
57+
}
58+
}
59+
}

Editor/Helpers/AssetSearch.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)