Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

AssetDatabase create folders recursively?

Discussion in 'Scripting' started by jwvanderbeck, Dec 2, 2019.

  1. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    777
    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:
    777
    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:
    686
    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.
     
    dbdenny likes this.
  4. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    777
    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:
    18,369
    Did you call AssetDatabase.Refresh() after making the directory?
     
  6. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    777
    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:
    18,369
    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,739
    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:
    686
    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);
     
unityunity