Creating ScriptableObject assets with custom names

I need to create an ScriptableObject asset with custom name (like when I create material, I can type the name).
However, this code allows me to only create assets with fixed name:

public static void CreateAsset<T>() where T : ScriptableObject
    {
        T asset = ScriptableObject.CreateInstance<T>();

        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        if (path == "")
        {
            path = "Assets";
        }
        else if (Path.GetExtension(path) != "")
        {
            path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
        }

       string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/New " + typeof(T).ToString() + ".asset");
        AssetDatabase.
        AssetDatabase.CreateAsset(asset, assetPathAndName);

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = asset;
    }
}

How could I make this name the name I write like when creating materials, scripts, folders etc?

[edit] I can change the name simply by clicking it, but if someone knows clean solution which allows me to make it behave like build in objects, it would still be apperciated.

Use rigidbody.velocity in local space. I think it might help you http://answers.unity3d.com/questions/193398/velocity-relative-to-local.html

–

1 Answer

1

@MichalOP Tagging the type with the CreateAssetMenu makes sure it shows up in the Create Asset Menu.

[CreateAssetMenu(fileName = "DefaultFileName", menuName = "Create Menu Display Name", order = 42)]
public class CustomAssetName : ScriptableObject {
    public string customDataFieldOne;
    public float[] customDataFieldTwo;
}

If you just want to rename existing assets, the only thing I can think of is to use

AssetDatabase.RenameAsset(string pathName, string newName);

This would work for your case if you use name data from a custom text field typed in by the user before invoking the CreateAsset method that you already have, and then modify CreateAsset to take in a name parameter and apply it to the created asset.