Octopoid
Octopoid

Reputation: 3688

Create new Unity asset in Editor rename mode

I'm adding a custom create action for a ScriptableObject like this:

[MenuItem("Assets/Create/FlowEngine/Character")]
private static void CreateCharacter()
{
    string path = AssetDatabase.GetAssetPath(Selection.activeObject);
    string assetPath = path + "/New Character.asset";

    FlowEngineCharacterData character = ScriptableObject.CreateInstance<FlowEngineCharacterData>();

    AssetDatabase.CreateAsset(character, assetPath);
    AssetDatabase.SaveAssets();

    Selection.activeObject = character;
}

This works exactly as expected, but there is one difference between this and all of the built in Assets. When you create those it automatically starts in rename 'mode'.

The only thing I can find to do with renaming the assets is a method which directly renames the asset rather than starting the UI rename operation.

Is there any way I can replicate this behavior?

Upvotes: 1

Views: 2385

Answers (1)

Octopoid
Octopoid

Reputation: 3688

I've found an answer for this tucked away in one of the other namespaces:

[MenuItem("Assets/Create/FlowEngine/Character")]
private static void CreateCharacter()
{
    string path = AssetDatabase.GetAssetPath(Selection.activeObject);
    string assetPath = path + "/New Character.asset";

    FlowEngineCharacterData character = ScriptableObject.CreateInstance<FlowEngineCharacterData>();

    ProjectWindowUtil.CreateAsset(character, assetPath);
}

Note that is ProjectWindowUtil rather than AssetDatabase. Using that starts the create operation off in 'rename mode'.

Upvotes: 3

Related Questions