Select asset for rename

Just a small curiosity as my browsing the docs has yet to reward me with anything regarding this matter.

I have a custom asset type that I am creating in a project. On creation the project window gets focus and the item is selected. However I was wondering if (and if so how) it is possible to have the object automatically select for renaming, much like the default assets do when you right click to create say a new material or shader.

I’m aware I could do the naming through a very basic popup window when the asst is created, but the aim is to have the asset behave as much like a regular unity asset as possible in terms of how you create and modify it.

Any help is appreciated, been looking around for a while but not found anything yet.

While delving into the same issue I found ProjectWindowUtil.CreateAsset(Object asset, string name) in the assemblies. In case anyone is still looking it creates the asset and starts renaming it.

I had the same question. This is how I resolved it. Note: this is basically a hack, I was using ILSpy to look at how unity does it, and it relies on a bunch of internal classes and functions.

What I opted up for, was getting the project window’s reference, selecting the asset I have just created, and sending a keyEvent to the window with F2 as the key. Granted this only works for windows but you can find out which key to send if you need it to work on Mac.

For information on getting the window’s reference, I would refer you to this thread:

http://answers.unity3d.com/questions/267097/editor-programming-experts-regarding-layouts.html

Note that the window name is different between unity 3 & 4 (there is more info on the above thread).

In code it looks like this:

    private static System.Type ProjectWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ObjectBrowser");
    private static EditorWindow projectWindow = null;

    public static void StartRenameSelectedAsset()
    {
        if (projectWindow == null)
        {
            projectWindow = EditorWindow.GetWindow(ProjectWindowType);
        }
        //should never be null but still ;)
        if (projectWindow != null)
        {
            var e = new Event();
            e.keyCode = KeyCode.F2;
            e.type = EventType.KeyDown;
            projectWindow.SendEvent(e);
        }
    }
    public static T CreateAssetAndStartRename<T>(string path = null, string assetName = null, string extension = ".asset") where T : ScriptableObject
    {
        var asset = CreateAsset<T>(path, assetName, extension, false);
        Selection.activeObject = asset;
        EditorUtility.FocusProjectWindow();
        StartRenameSelectedAsset();
        return asset;
    }

So to use, you would call CreateAssetAndStartRename(), which in turn calls CreateAsset (this you should already have implemented from your question, so I didn’t include the code for that), sets the new asset as the active selection, focuses the project window, and then calls StartRenameSelectedAsset(), which makes sure there is a reference to the window, and sends it the event.

As you can tell these are static functions, I just have them in my CustomAssetUtility class. Using the generics here allows this to work with any scriptable object in the game (in my case, you can always extend the support obviously thorugh your own CreateAsset().