Home > Mobile >  Go one folder up in Unity build
Go one folder up in Unity build

Time:12-01

I am trying to move one folder up in my Unity build such that the structure is as follows:

MainFolder -> UnityApp.exe

The directory structure is:

ParentFolder/MainFolder/UnityApp.exe

Now how do I go to my "ParentFolder"?

private string GetDirectory() {
#if UNITY_EDITOR
        if (RootFolder)
            return Path.Combine ("C:/Users/admin/source/ParentFolder/");
        return RootFolder;

#else
        var oneLevelUp = Application.dataPath   "/../"; //Here is where it is not working
        if (RootFolder)
            return Path.Combine (oneLevelUp);
        return RootFolder;
#endif
    }

I can get the path to work in my editor because I am specifying the path, but what about in my build? That is causing problems. I cannot access it.

CodePudding user response:

The method Path.GetDirectoryName return the parent directory's path :

var path = @"C:\Program Files (x86)\Microsoft Visual Studio\2019";
Console.WriteLine(Path.GetDirectoryName(path));
// Output - C:\Program Files (x86)\Microsoft Visual Studio

This work also with relative path :

var path = @"Level1\Level2\Level3\Level4";
Console.WriteLine(Path.GetDirectoryName(path));
// Output - Level1\Level2\Level3

In your case :

var oneLevelUp = Path.GetDirectoryName(Application.dataPath);

CodePudding user response:

I found the right way to do this. Using Path.Combine():

using System.IO;
 
// Application.dataPath returns something like C:/MyWork/UnityProject/Asset
// back 1 level using "../" to get to project path
 
// C:/MyWork/UnityProject/
string projectFolder = Path.Combine( Application.dataPath, "../" );
 
// C:/MyWork/UnityProject/ProjectSettings
string settingFolder = Path.Combine( Application.dataPath, "../ProjectSettings" );
 
// back 2 levels to out of project path
// C:/MyWork/
string outside = Path.Combine( Application.dataPath, "../../" );
  • Related