我試圖在我的 Unity 版本中向上移動一個檔案夾,使其結構如下:
MainFolder -> UnityApp.exe
目錄結構為:
ParentFolder/MainFolder/UnityApp.exe
現在我如何去我的“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
}
我可以在我的編輯器中獲取路徑,因為我指定了路徑,但是在我的構建中呢?那是造成問題的原因。我無法訪問它。
uj5u.com熱心網友回復:
Path.GetDirectoryName方法回傳父目錄的路徑:
var path = @"C:\Program Files (x86)\Microsoft Visual Studio\2019";
Console.WriteLine(Path.GetDirectoryName(path));
// Output - C:\Program Files (x86)\Microsoft Visual Studio
這也適用于相對路徑:
var path = @"Level1\Level2\Level3\Level4";
Console.WriteLine(Path.GetDirectoryName(path));
// Output - Level1\Level2\Level3
在你的情況下:
var oneLevelUp = Path.GetDirectoryName(Application.dataPath);
uj5u.com熱心網友回復:
我找到了正確的方法來做到這一點。使用 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, "../../" );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/371969.html
