- Home /
Get Current Project Window Folder
I have a few editor scripts that make assets for me, but when I right-click and select my context menu item to create the asset I have no idea where to get the current project's path from, so I end up making the asset in the root of the assets folder.
I was hoping there might be some method or static property to return me this - anyone solved this one?
i.e. I have a ScriptableObject for behaviours. I right click in the Project window to create a new behaviour - I'd like that behaviour to be created at the location I right-clicked.
Thanks Bovine
Answer by mhtraylor · Jul 09, 2013 at 10:47 PM
AssetDatabase has some methods for getting asset paths in the project; these might help.
None that I can see. I have looked and unless I've missed something...
You can use AssetDatabase.GetAssetPath()
on Selection.activeObject
. You can make sure it is a directory with System.IO.Directory.Exists()
or something similar. There was a similar question that looped through the selection array, but I'm guessing activeObject
would be better since you are just right-clicking on a folder.
Hope this helps.
Hmmm. I'll take a look but typically I've right-clicked in the folder, so not sure there's anything selected. I guess it might be a partial solution...
Just double-checked this and right-clicking inside the folder does select it (or if you clicked an asset in the folder, you can extract the path from that as well).
Righto I'll give that a whirl then later as it's a pain finding the new asset and should be easy to sort. Looking like a helper function to me :)
Will mark answered if it does as you say
Answer by Wappenull · Feb 26, 2021 at 06:16 AM
Use built-in property
You can use built-in property to generate a "create asset" menu
[CreateAssetMenu( menuName = "Wappen Asset/WaveBankData" )]
public class WaveBankData : ScriptableObject
{
And it will magically added to right click menu -> Create. Has same functionality as Unity's one. It will be created in current browser location, prompt for a rename, etc.
As a bonus
here is how TextMesh pro determine current right-click folder. If you call Selection.assetGUIDs
under MenuItem call back, it will guarantee current displaying folder (the one that you clicked on its empty space)
[MenuItem("Assets/Create/TextMeshPro/Color Gradient", false, 115)]
public static void CreateColorGradient(MenuCommand context)
{
string filePath;
if (Selection.assetGUIDs.Length == 0)
filePath = "Assets/New TMP Color Gradient.asset";
else
filePath = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]);
Your answer
Follow this Question
Related Questions
I can't find my scriptable object though I've creat asset menu... 1 Answer
Create List of custom class types, for use in custom editor 0 Answers
How set order in [MenuItem] attribute like in [CreateAssetMenu] attribute? 2 Answers
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
How do I remove a ScriptableObject reference after the script has been deleted? 0 Answers