- Home /
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.
I don't know a coding solution, but F2 is the keyboard shortcut for 'rename currently selected thing' (works in Windows file explorer, too; might be windows-specific). If you can't make it automatic at least it can be convenient :)
Answer by TokiZR · Apr 18, 2014 at 05:51 AM
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.
How do we get access to this method? Can we use it?
I found the answer. Header: using UnityEditor.ProjectWindowCallBack and using UnityEditorInternal.
Answer by skalev · Sep 05, 2013 at 09:43 PM
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().
Your answer
Follow this Question
Related Questions
Custom Editor: show asset folders in ObjectField 2 Answers
Is it possible to rename an asset in V5.5? 2 Answers
Change angle of camera in prefab preview 4 Answers
Access or remove background color of AssetPreview 2 Answers
Working with the editor Selection 0 Answers