Hi, I am looking to create a custom menu item under the right-click context menus to create a custom file with certain contents.
First off, I’m on Unity 5 Pro (I know, legacy) and I haven’t been able to find any information on this at all online and it’s getting very annoying having to duplicate a template file and drag it into the project every time.
I’m looking for a way to right-click in the Project window, go to “Create” and then something like “Custom Text File” or whatever, which would then hopefully just place a file in that location with some template contents in, similarly to how it does with creating a new script or shader.
Is it possible to add this functionality, or is it locked down?
The ContextMenu attribute, which applies when right-clicking MonoBehaviour scripts in the inspector.
The MenuItem attribute, which can add a custom menu option to the main toolbar in Unity.
The CreateAssetMenu attribute, which can create an instance of a ScriptableObject in your Assets folder.
It sounds like the CreateAssetMenu option is what you’re looking for, but this only applies to ScriptableObjects, and can’t be used to create something like a plain text file in your example, although you can use ScriptableObjects to contain text information among other things.
If you absolutely need the ability to create assets of different file types and such, the MenuItem attribute is probably what you’re looking for instead, coupled with the System.IO File creation/management features.
For anybody who comes here looking, it would appear that ProjectWindowUtil.CreateAssetWithContent method fills this need - replicating the native Unity create-asset behavior: