Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Veehmot · Oct 18, 2011 at 06:15 PM · folderfile-io

Check if folder exists - Unity's way

  • We have the FileUtil class that lets you do move, copy, delete operations over files or directories.

  • We have the AssetDatabase class that lets you create a new folder.

  • Is there any class that lets you check if a folder exists? Or should I use C# **Directory.Exists**?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Steven-Walker · Oct 18, 2011 at 06:58 PM 1
Share

Is there any reason you are opposed to using Directory.Exists? That's what I've always used in my editor scripts.

avatar image $$anonymous$$ · Feb 19, 2012 at 09:59 PM 0
Share

What seems missing here, though, is any way to map between an asset directory in Unity and folders in the actual filesystem (and in a platform-independent way). Yes, I know...you could hard-code the path in your script, but that's just gross... :/

avatar image Steven-Walker · Feb 20, 2012 at 06:28 AM 0
Share

I haven't had any problem mapping the file system to the asset directory using Application.dataPath.

avatar image $$anonymous$$ · Feb 20, 2012 at 07:41 AM 0
Share

Ah, missed that - thanks a lot! :)

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by jahroy · Oct 18, 2011 at 08:04 PM

My guess is functions like AssetDatabase.CreateFolder were implemented because adding a file to a Unity project is different than creating a normal file. There is probably some overhead involved (like registering it with the project or saving its state).

In contrast, checking whether a file exists is the same in all environments (whether you're writing a Unity application or not). My theory is this is why there is no need to create a "Unity version" of the function that checks if a file exists.

I had the same question a week ago, and that's how I've rationalized what I've found (or not found) in the documentation.

Edit - Just came acros this in the docs:

File Operations using the AssetDatabase

Since Unity keeps metadata about asset files, you should never create, move or delete them using the filesystem.

Instead, you can use AssetDatabase.Contains, AssetDatabase.CreateAsset, AssetDatabase.CreateFolder, AssetDatabase.RenameAsset, AssetDatabase.CopyAsset, AssetDatabase.MoveAsset, AssetDatabase.MoveAssetToTrash and AssetDatabase.DeleteAsset.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
15

Answer by e4lime · Mar 06, 2015 at 12:22 PM

You can now do:

 AssetDatabase.IsValidFolder(string path);


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Estecka · May 07, 2017 at 01:23 AM 0
Share

Now does this let us know whetther a file (not a folder) exists in this path ?

avatar image Bunny83 Estecka · May 07, 2017 at 02:38 AM 1
Share

Of course not. The question was about checking if a folder exists. And the method name "IsValidFolder" clearly only returns true when the given path "is a valid folder"

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access persistent data path in Android phone? 3 Answers

Read and Write to text file Online 4 Answers

File.IO WriteAllText not working? 1 Answer

Opening a file explorer in run time? 8 Answers

Synchronous vs asynchronous file reading/writing 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges