Skip to content

Instantly share code, notes, and snippets.

@DmitriyYukhanov
Created January 4, 2017 12:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DmitriyYukhanov/e3fe8fb9ee3675eaa227c8f43fcedac1 to your computer and use it in GitHub Desktop.
Save DmitriyYukhanov/e3fe8fb9ee3675eaa227c8f43fcedac1 to your computer and use it in GitHub Desktop.
How to select asset in Project Browser without loading Asset.
// use to select asset at the Project Browser when you know path and asset
// has no objects inside, e.g. AssetDatabase.LoadMainAssetAtPath() returns null
// path - relative to Project folder, e.g. Assets/StrangeFile.asset
public static void SelectNonObjectFile(string path)
{
string guid = AssetDatabase.AssetPathToGUID(path);
// in perfect world, all reflection should be cached (including method delegate)
Type assetDatabaseType = typeof(AssetDatabase);
MethodInfo mi = assetDatabaseType.GetMethod("GetInstanceIDFromGUID", BindingFlags.NonPublic | BindingFlags.Static);
if (mi == null)
{
Debug.LogError("AssetDatabase.GetInstanceIDFromGUID() not found via reflection!");
return;
}
int instanceId = (int)mi.Invoke(null, new object[]{ guid });
Selection.activeInstanceID = instanceId;
}
@mfandreich
Copy link

Sometime we will cache averything... sometime...
Nice solution! =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment