Search Unity

How to get project folder path?in editor Mode

Discussion in 'Editor & General Support' started by JohnSonLi, Mar 17, 2015.

  1. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
  2. tuanzi88

    tuanzi88

    Joined:
    Sep 10, 2015
    Posts:
    19
    Same question ...
     
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
  4. tuanzi88

    tuanzi88

    Joined:
    Sep 10, 2015
    Posts:
    19
    oops. silly me. thanks fffMaizbier.
     
  5. UnusAutomationSystems

    UnusAutomationSystems

    Joined:
    Jul 15, 2019
    Posts:
    49
    Azerion_Noblauch likes this.
  6. UnusAutomationSystems

    UnusAutomationSystems

    Joined:
    Jul 15, 2019
    Posts:
    49

    Answer is System.IO.Directory.GetCurrentDirectory()
     
    Kokowolo, sharatachary, Tnrsn and 8 others like this.
  7. nasir_41

    nasir_41

    Joined:
    Dec 21, 2015
    Posts:
    9
    Following works:

    Code (CSharp):
    1.         #if UNITY_EDITOR
    2.         string p1 = Application.dataPath;
    3.         p1 = p1.Replace("/Assets", "");
    4.        // p1 is path to project folder...
    5.         #endif
     
    masterton, NRTnarathip and Whitebrim like this.
  8. mmortall

    mmortall

    Joined:
    Dec 28, 2010
    Posts:
    89
    Never use examples like this. :rolleyes: Just think more about the consequences and edge cases.
    What if the project folder name is AssetsOfMyMom or project is inside folders with Assets substring at the beginning. And /Assets will be on Mac or Linux, on windows, it will be \Assets.
     
  9. ChillPhantom12

    ChillPhantom12

    Joined:
    Jul 14, 2020
    Posts:
    1
    how do i get a working path for a macbook air??? \_(0.0)_/
     
  10. Wappenull

    Wappenull

    Joined:
    Oct 29, 2013
    Posts:
    51
    Hi, I came from future. Just to drop in another copy-paste for internet travellers.
    Code (CSharp):
    1. using System.IO;
    2.  
    3. // Application.dataPath returns something like C:/MyWork/UnityProject/Asset
    4. // back 1 level using "../" to get to project path
    5.  
    6. // C:/MyWork/UnityProject/
    7. string projectFolder = Path.Combine( Application.dataPath, "../" );
    8.  
    9. // C:/MyWork/UnityProject/ProjectSettings
    10. string settingFolder = Path.Combine( Application.dataPath, "../ProjectSettings" );
    11.  
    12. // back 2 levels to out of project path
    13. // C:/MyWork/
    14. string outside = Path.Combine( Application.dataPath, "../../" );
    15.  
    Additional read

    Additionally you could use
    Path.GetFullPath
    to clean up and make clean full path after append with "../"

    for example: settingFolder above, if Debug.log it, will become
    C:/MyWork/UnityProject/Asset\../ProjectSettings

    which is usable to any file related API. But kinda messy to user eyes. The function below will clean up and remake the path.

    Code (CSharp):
    1. // from C:/MyWork/UnityProject/Asset\../ProjectSettings
    2. // to C:\MyWork\UnityProject\ProjectSettings
    3. settingFolder = Path.GetFullPath( settingFolder );
    Notice the all backslash "\". If Path.GetFullPath ran on Windows its output will backslash. If you want to convert to slash, you could run string Replace function on it easily.
     
    Last edited: Sep 29, 2020
    idyakonov, RaveBox, k76 and 10 others like this.
  11. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,087

    Path.GetDirectoryName( Application.dataPath )
     
  12. CodePingu

    CodePingu

    Joined:
    Nov 24, 2020
    Posts:
    1
    Just wondering about this, is there a way to choose a specific folder, so like the position of the folder where the game is stored + (name of other folder in file location)?
     
    GleaveGames likes this.
  13. GreedyVox

    GreedyVox

    Joined:
    Nov 18, 2015
    Posts:
    33
    For the win!
     
    mattstromaneveri, Zamaroht and bart42 like this.
  14. unity_i5sdgJ-ZYtKgOQ

    unity_i5sdgJ-ZYtKgOQ

    Joined:
    Dec 14, 2020
    Posts:
    1
    Pretty important to do relative pathing with the static path gotten from Application.dataPath or an equivalent when releasing a build, say on Itch.io. Better off doing it from the start to avoid trouble later!
     
    ivaylo5ev likes this.
  15. Dev-4U

    Dev-4U

    Joined:
    Nov 2, 2021
    Posts:
    23
    Can you explain that?

    I can't see how this can be a problem since Application.dataPath always points to where the *_Data folder is in a build, no matter where on the user's computer the built app is stored/executed on the user's machine. The only issue I can think of it when you store a static path string in your code rather than always getting it via Application.dataPath. Which makes that the problem, not the path itself.
     
  16. M4Mahdi

    M4Mahdi

    Joined:
    Dec 17, 2018
    Posts:
    2
    Code (CSharp):
    1. using System.IO;
    2. //...
    3.  
    4. //Method 1 (I prefer)
    5. string projectPath1 = Directory.GetParent(Application.dataPath).ToString();
    6.  
    7. //Method 2
    8. string projectPath2 = Directory.GetCurrentDirectory();
    9.  
    10. /*
    11. Out:
    12. N:\unity_workspace\My Glorious Project
    13. */
     
    Last edited: Mar 30, 2023