Unity: Load Assets in Editor

Sometimes you need to load assets in the editor without the comfort of a direct reference. Assets are loaded using the AssetDatabase and it can be done by

  • Path
  • Label
  • Name
  • GUID
  • Instance ID

Instance ID

The instance ID is visible at the top of the inspector when the inspector debug mode is active.

 

Beware, the instance ID  survives enter/exit play mode but will change every time the asset is unloaded. For example when Unity is closed.

Instance ID in the debug inspector

GUID

The GUID of an asset can be found in its meta file.

 

The GUID will only change when the meta file is changed/deleted.

Asset GUID in the corresponding meta-file

Load Specific Asset

I wouldn’t recommend using a specific asset path for loading. It can easily break when someone decides to move the asset. A more future-proof and reliable approach is to load assets based on their GUID.

#if UNITY_EDITOR
using UnityEditor;

public static class AssetLoader
{
	public static T LoadAsset<T>(string guid) where T : class
	{
		string assetPath = AssetDatabase.GUIDToAssetPath(guid);
		var asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(T)) as T;
		return asset;
	}
}
#endif

Load Assets From Folder

If the assets you want to load are placed in a specific location, you can use AssetDatabase.FindAsset to get them. The function takes a list of folders and a search-string as parameters.  It’s a bit more involved than loading by GUID. But the search-string makes the function very versatile because you can use every input that you would type into the project window (search field) to locate assets.

#if UNITY_EDITOR
using UnityEditor;
using System.Collections.Generic;

public static class AssetLoader
{
	/// <summary>
	/// Loads all assets of type <typeparamref name="T"/> in the 
	/// <paramref name="folderPath"/> including subfolders.
	/// </summary>
	/// <typeparam name="T"> Type of the assets to load.</typeparam>
	/// <param name="folderPath"> Path starting at the asset folder.
	/// Example: Assets/Folder/Folder </param>
	/// <returns></returns>
	public static List<T> LoadAssetsFromFolder<T>(string folderPath) where T : class
	{
		List<T> loadedAssets = new List<T>();
		string searchString = $"t:{typeof(T)}";

		string[] assetGUIDs = AssetDatabase.FindAssets(searchString, new[] { folderPath });

		foreach (var assetGUID in assetGUIDs)
		{
			string assetPath = AssetDatabase.GUIDToAssetPath(assetGUID);
			T loadedAsset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(T)) as T;
			loadedAssets.Add(loadedAsset);
		}

		return loadedAssets;
	}
}
#endif

Load Assets with Label

This uses the same approach as the previous function. It just changes the search string for the AssetDatabase.FindAssets.

#if UNITY_EDITOR
using UnityEditor;
using System.Collections.Generic;

public static class AssetLoader
{
	/// <summary>
	/// Loads all labeled assets of type <typeparamref name="T"/> in the 
	/// <paramref name="folderPath"/> including subfolders.
	/// </summary>
	/// <typeparam name="T"> Type of the assets to load.</typeparam>
	/// <param name="folderPath"> Path starting at the asset folder.
	/// Example: Assets/Folder/Folder </param>
	/// <param name="label">Asset label</param>
	/// <returns></returns>
	public static List<T> LoadAssetsByLabel<T>(string folderPath, string label) where T : class
	{
		List<T> loadedAssets = new List<T>();
		string searchString = $"t:{typeof(T)} l:{label}";

		string[] assetGUIDs = AssetDatabase.FindAssets(searchString, new[] { folderPath });

		foreach (var assetGUID in assetGUIDs)
		{
			string assetPath = AssetDatabase.GUIDToAssetPath(assetGUID);
			T loadedAsset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(T)) as T;
			loadedAssets.Add(loadedAsset);
		}

		return loadedAssets;
	}
}
#endif