Search Unity

Path exists (and spelled correctly) but directory not found

Discussion in 'Scripting' started by Velo222, Oct 13, 2015.

  1. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Posting this in case someone else has the same problem I did.

    I had a third-party script that was filling an array based on a "Directory.GetFiles(Insert_file_path_here)" method. It worked perfectly in the Editor, but for some reason would not work in an actual game build (using Unity 5.2.1p2). I noticed in my game build output log that Unity was not detecting the path folder, even though the path was spelled perfectly and did in fact exist. I checked a dozen times to make sure it did.

    It turns out what fixed this is to place the asset I wanted in the "Streaming Assets" folder (located in overall Assets). Then, in my script, I needed to do Directory.GetFiles(Application.dataPath + "/StreamingAssets/MyAsset") instead of just a regular string path to a normal directory.

    I'm not sure if this is a bug with 5.2 (as I've heard there are many right now), or if that's how it's supposed to work. But having things work perfectly in the Editor, but not in the actual game build is really annoying. I wish WYSIWYG in the Editor was WYSIWYG in the game build as well.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    what build target? there are differences to how pc/ios/whatever work...
     
    Kiwasi and angrypenguin like this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Can you give an example of what you were using before that didn't work?
     
  4. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Correct. My example is for pc/mac/linux standalone specifically. If someone wants another platform, they should look at the documentation. I was going to put that in my original post but just left it out. Thank you.

    Yes. Lets say the folder you want to access is named "PresetFiles" and is located in the path: Assets/AlienTerrainPack/Presets/PresetFiles

    The script was using: Directory.GetFiles("Assets/AlienTerrainPack/Presets/PresetFiles"); which worked perfectly in the Editor. But when the game was built and run, it did not work at all, and the output log stated an IO exception and that the directory did not exist -- when it clearly did.

    So then, when I moved the "PresetFiles" folder to the StreamingAssets folder and used: Directory.GetFiles(Application.dataPath + "/StreamingAssets/PresetFiles") -- it was able to find the folder in an actual game build (pc platform for me).
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    So you made that exact directory structure in your build's deployment?
     
    Velo222 likes this.
  6. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    I guess I don't understand that question angrypenguin. That exact directory structure exists in my project folder to start with (in the Editor). Then I click to build the game. Unity does whatever it is that Unity does to build the game, and that piece of code no longer recognizes the path -- I'm assuming due to the way Unity builds the game and whatever folders it creates etc... Can you make Unity build differently than what it does?

    I mean from what you're saying it makes more sense to me now. In that the exact path is not re-created in the game build, and I was just assuming this was the case. But instead, a different structure is created in the build. So the directory command doesn't work. But I guess my argument is why can't Unity detect and recognize it's own Editor structure through to the game build?

    But I think I get what you're saying, in that I, as the developer, have to now create that directory structure in the game build somehow. I was just assuming it would stay intact and be exactly as it is in the Editor.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This is normal and desired behaviour. Assets are not included in the build unless they are referenced in the scene. That way you don't get tons of assets in the build that are never used.

    There are a couple of ways to get around this. The easiest is to use the Resources system. These resources are included in the build. There are a bunch of built in ways to access these resources.

    Streaming Assets copies data directly across to the same folder structure in the build. If you must access files directly, it's the way to go.

    It's all in the docs. ;)
     
    angrypenguin likes this.
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    There's some explanation on why the folder structure is not kept, and how you work with resources here.

    The short version is that Unity does not include those 3gb of unused, raw .psd files you have in your Assets/Textures folder in the build. If you put them in Assets/Resources/Textures, though, they'll all be there.
     
    Kiwasi and angrypenguin like this.
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    It does (as mentioned by others) track what assets are used and offer a Resources system for where stuff falls outside of that.

    There's not really any practical way for it to also somehow figure out which arbitrary files from within that you might want to read via a different system.
     
    Kiwasi likes this.
  10. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Are you talking about using the "Resources" folder though BoredMormon? Because I did try that, and it didn't work. But probably for reasons mentioned above.




    I get that part. And that is great. I like that they do that, because I don't want a bloated game either, or to have it be any bigger in file size than it needs to be.

    Well, all in all, I'm getting a clearer picture from what I was doing wrong. That's why I post on the forums, is so that I can get corrected if I'm doing things wrong. I appreciate you guys setting me straight on this. Thank you. :)
     
    Last edited: Oct 15, 2015
    Kiwasi likes this.
  11. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    lol I guess I deserved that :D
     
  12. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    No, he's talking about using the resource *system*, of which the folder is a part. You don't access its contents via the file system, but via the Resources class.
     
    Kiwasi likes this.
  13. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Ah I see. I will have to study this class further then. Thanks.
     
    angrypenguin likes this.