はじめに
今回はUnity Editor上でプロジェクト直下・Packages・UserSettings・Logsといったパスの取得方法を紹介したいと思います。
やり方
以下はUnity Editor上での取得方法です。
// コメントはMac上での出力例になっています。 // Assets直下 : /Users/[UserName]/[ProjectName]/Assets Debug.Log(Application.dataPath); Debug.Log(Path.GetFullPath(Application.dataPath)); // Project直下 : /Users/[UserName]/[ProjectName] Debug.Log(Path.Combine(Application.dataPath, "..")); Debug.Log(Path.GetFullPath(Path.Combine(Application.dataPath, ".."))); // Packages : /Users/[UserName]/[ProjectName]//Packages Debug.Log(Path.Combine(Application.dataPath, "..", "Packages")); Debug.Log(Path.GetFullPath(Path.Combine(Application.dataPath, "..", "Packages"))); // UserSettings : /Users/[UserName]/[ProjectName]/UserSettings Debug.Log(Path.Combine(Application.dataPath, "..", "UserSettings")); Debug.Log(Path.GetFullPath(Path.Combine(Application.dataPath, "..", "UserSettings"))); // Logs : /Users/[UserName]/[ProjectName]/Logs Debug.Log(Path.Combine(Application.dataPath, "..", "Logs")); Debug.Log(Path.GetFullPath(Path.Combine(Application.dataPath, "..", "Logs"))); // Environment.CurrentDirectoryは変わる可能性があるので利用してはダメ Environment.CurrentDirectory = Path.GetFullPath(Application.dataPath);
Path.Combine
をうまく利用することでそれぞれのパスを簡単に取得することができます。
またSystem.Environment.CurrentDirectory
を利用するとプロジェクトルートのパスを取得することができますが、ランタイム上でSystem.Environment.CurrentDirectory
の返り値が変わる可能性があるので推奨しません。
Application.dataPathについて
プラットフォームによって取得される値が異なりますが、Unity Editor上ではAssets直下を返すように設定されています。
Unity Editor: <path to project folder>/Assets
Mac プレイヤー: <path to player app bundle>/Contents
iOS プレイヤー: <path to player app bundle>/<AppName.app>/Data (このフォルダーは読み込み専用で Application.persistentDataPath を使用してデータを保存します。)
Win/Linux player: <path to executablename_ Data folder> (ほとんどの Linux では大文字と小文字は区別されるので注意してください。)
WebGL: 再生プレイヤーのデータが含まれているフォルダーの絶対パス (URL には実際のデータファイル名は含まれずフォルダー名までです)
Android: Normally it points directly to the APK. If you are running a split binary build, it points to the OBB instead.
Windows Store Apps: The absolute path to the player data folder (this folder is read only, use Application.persistentDataPath to save data)