Unity Tip: Working with Resources.Load() Strings

Resources.Load() is the way to load ‘baked’ data (ie data that was shipped with Unity at compile-time). This is done via strings. Messy, messy strings. And what format are these strings?

The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.

This path value is surprisingly easy to get wrong! You can forget a folder, you can misspell the name, etc. So here is a simple script that you can use to copy the correct value to the clipboard with right-click or with cmd-alt-c. You can then paste this value into whatever you need, and not worry about it again! Enjoy.

copy_ref_to_clipboard


using UnityEngine;
using UnityEditor;
public static class CopyRefUtil
{
[MenuItem ("Assets/Copy Ref To Clipboard %&c")]
public static void GetReferencesString()
{
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if(path.IndexOf("Resources") > -1)
{
path = path.Substring(path.LastIndexOf("Resources/") + 10);
path = path.Substring(0, path.LastIndexOf("."));
EditorGUIUtility.systemCopyBuffer = path;
}else{
Debug.LogError("[Copy Ref To Clipboard] Not an object in a resources folder.");
}
}
}

view raw

CopyRefUtil.cs

hosted with ❤ by GitHub