MenuItem script to add a specific prefab to the scene

Hi all!

Just wondering if there’s a way to make a menu item script that would add a specific prefab to the scene? I just have a few prefabs that I use all the time throughout the project and I’d like to make an easy way to pop them into your scene. Like how you can press the plus button in the hierarchy and choose 3dObject > Cube or something like that but with my own custom objects.

Is there a way to do that? The only way I know to reference project items is to put the in reference folders, which isn’t ideal for this case.

Thanks!
Pete

With UnityEditor.MenuItem if you start the path with GameObject it will appear in the hierarchy menu.

So this should work:

#if UNITY_EDITOR
using UnityEditor;

public static class MyPrefabMenus
{
    [MenuItem("GameObject/MyPrefabs/XYZ Prefab", false, 0)]
    public static void AddXYZPrefab()
    {
    }
}
#endif

As to how to retrieve the asset, you’re working in the editor so you have access to AssetDatabase as means to find them.

2 Likes

Okay cool, thanks @spiney199 !