Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to create an extension for the Unity 3D Editor and have an custom asset type. During the process of creation this I recognized that I would improve the UX/usability if I can specify the path where I want to create the asset by using right click. But I fail to get the context path of the right click and I cannot find any hints on google or at the unity scripting api. Below you can see an image of the process I tried to describe above.

Asset creation process

Here is the code that is responsible for handling the context creation "event".

[MenuItem("Assets/Create/Chat")]
public static void createChatViaMenu() {
    Chat c = Chatter.createDefaultChat();

    // This is the part where I want to insert the relative path.
    AssetDatabase.CreateAsset(c, "Assets/" + c.Title + ".asset");
}

If you need any more information please let me know.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You can get the path by using Selection.activeObject. Here is a small code to get this feature working :)

   string path = AssetDatabase.GetAssetPath (Selection.activeObject);

    if (path == "")
    {
        path = "Assets";
    }
    else if (Path.GetExtension(path) != "")
    {
        path = path.Replace(Path.GetFileName (AssetDatabase.GetAssetPath (Selection.activeObject)), "");
    }
    AssetDatabase.CreateAsset (asset, AssetDatabase.GenerateUniqueAssetPath (path + "/New MyAsset.asset"));
share|improve this answer
    
thank you very much :) –  Wachiwi 2 days ago
    
You are welcome :) –  Hash Buoy 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.