Unity Editor: Creating a file and start name editing

I want to create an asset file and let the user rename it.

First: I know about the CreateAssetMenu attribute and do NOT want to use it for two reasons:

  • I want to initialize the file to have some required values. But Unity does not like having a constructor and functions like Awake() and OnEnable() get called multiple times
  • The user should be able to create a file by clicking a button in the inspector. After that, he/she renames the file.

So I created a function using [MenuItem("Assets/Create/My File")] to create a file. This works totally fine but for using AssetDatabase.CreateAsset(file, path) I need to specify a file name already.

So is there any way to instantly start the file name editing after creating the file just like it is done when just using CreateAssetMenu?

Thanks

(Not sure if this is the right topic. Its the only one mentioning the Unity Editor)

Are you aware of the void Reset() method? You can do a lot in here, including even editor-specific stuff like finding other crud in your AssetDatabase to link to.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.Reset.html

Obviously if you use UnityEditor methods in Reset() you need to condition them out with UNITY_EDITOR.

Actually, I’ve not been aware of that! Thanks a lot

1 Like