Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AssetDatabase create folders recursively?

Discussion in 'Asset Database' started by jwvanderbeck, Dec 2, 2019.

  1. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    Is there an existing function available, or do I need to write one, to let me create a given folder structure in one go, making all subfolders as needed?

    This is pretty normal for most OS file system APIs, but Unity seems to insist that it can't make a given folder without its immediate parent already existing.

    So if for example you want a folder Assets/Foo/Bar/Final you can't just ask for that, as it will fail. You have to first create Foo, then create Bar, then create Final.

    Unless there is something I am missing?
     
  2. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    I've just gone and written my own method to handles this, but I'm still curious if it exists already and I just missed it.
     
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,061
    There's really no reason to use the AssetDatabase API to create folders. The only upside I can see is that it gives you the folder's GUID and immediately updates the database.

    But there's no problem using System.IO.Directory.CreateDirectory, which will automatically create folders recursively. You can e.g. immediately create an asset inside that folder and Unity won't complain, so I don't see why you'd have to use the AssetDatabase API.
     
    NMJ_GD and dbdenny like this.
  4. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    That is what I thought and I had done that in the past, but when I went to do that with the most recent work I kept running into bugs where Unity did not see the newly created folders. The only way I could reliably get Unity to see the new folders for code that executed as part of the same bit of work was to create the folders using AssetDatabase.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Did you call AssetDatabase.Refresh() after making the directory?
     
    NMJ_GD likes this.
  6. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    Yep. It never seemed to "register" until later. In other words if I am doing some asset work and want to import some files, the code would create the target folders, then call AssetDatabase.CreateAsset() but that second call would error out saying the folder didn't exist. Doing it with AssetDatabase.CreateFolder() does seem to register it though.
     
    dbdenny likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Weird. When you call AssetDatabase.Refresh() does Unity produce the metafiles for those folders? I imagine until you see metafiles appear for the folders, which I assume happened on a .Refresh() but apparently not, the DB won't know about them...
     
  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    It looks like this is not done synchronously, just runs the update in background. Probably if you register an async callback in the editor, it will work.
     
  9. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,061
    Which version of Unity are you on?

    On Unity 2019.2, I can execute the following without problems, Dir/Subdir/Subsubdir and test.mesh get created without errors and appear immediately in the project view:
    Code (CSharp):
    1. var assetPath = "Assets/Dir/Subdir/Subsubdir/test.mesh";
    2. Directory.CreateDirectory(Path.GetDirectoryName(assetPath));
    3. AssetDatabase.CreateAsset(new Mesh(), assetPath);
     
  10. Jadefire16

    Jadefire16

    Joined:
    Jul 14, 2019
    Posts:
    19
    Sorry for a bit of a necro but to force Unity to generate the respective meta files and actually display the folders in editor you can use this:
    Code (CSharp):
    1.  
    2. public static void CreateDirectoryFromAssetPath(string assetPath)
    3.     {
    4.         string directoryPath = Path.GetDirectoryName(assetPath);
    5.         if (Directory.Exists(directoryPath))
    6.             return;
    7.         Directory.CreateDirectory(directoryPath);
    8.         AssetDatabase.Refresh();
    9.     }
    10.  
    You could also just take in the directory path instead of an asset path
     
    daneobyrd likes this.
  11. AntonShakalo

    AntonShakalo

    Joined:
    Jun 22, 2021
    Posts:
    3
    @Jadefire16 way is not working again. Unity 2021.1.7f1