つまりXNAのWindows環境実行においても同じことになると思います。
UnityでResourcesの中のデータではなく
指定した場所に配置したデータを読み込むといった対応をしたい場合
例えばマイドキュメントの中にデータフォルダを作成し、その中にゲーム中のセーブデータを作成するといった場合
マイドキュメントのパスを取得しないといけません
Win98の時代から使ってきた意見からいえば、
Windowsのバージョンによってパスが違うのですよね
そこでSystem名前空間にあるEnvironmentを使います。
今回の背景は
完成したexeをインストーラーを使ってインストールした場合
Program Filesフォルダに展開すると
書き込み権限として管理者の情報が必要になります。
WindowsVista以降、このような場合に確認のダイアログがでてきて
毎回毎回OKを押さないといけませんね(設定で変更可能ですが)
このフォルダ内にUnityからファイルを作成するといった場合
Unityプログラム自体を管理者権限で実行するようなオプションをつけていないとアクセス権限がないとしてエラーになってしまいます。
めんどくさいので書き込み権限を聞かれないマイドキュメントなどに作成しようというのが目的でした。
では以下確認用の簡単なログ出力プログラム
using System;
と定義しておき、定義されているものを使ってパスをログに出してみましょう
void Start () {
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.Programs));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.Recent));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.SendTo));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.Startup));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.System));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.Templates));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
Debug.Log(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
}
このような内容でログを見てみると以下のようになります。
C:\Users\ユーザー名"\Documents
C:\Users\ユーザー名"\Documents
C:\Users\ユーザー名"\Music
C:\Users\ユーザー名"\Pictures
C:\Program Files (x86)\Common Files
C:\Users\ユーザー名"\AppData\Roaming\Microsoft\Windows\Cookies
C:\Users\ユーザー名"\Desktop
C:\Users\ユーザー名"\Desktop
(空文字が返ってくるため取得できない)
C:\Program Files (x86)
C:\Users\ユーザー名"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
C:\Users\ユーザー名"\AppData\Roaming\Microsoft\Windows\Recent
C:\Users\ユーザー名"\AppData\Roaming\Microsoft\Windows\SendTo
C:\Users\ユーザー名"\AppData\Roaming\Microsoft\Windows\Start Menu
C:\Users\ユーザー名"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
C:\Windows\system32
C:\Users\ユーザー名"\AppData\Roaming\Microsoft\Windows\Templates
C:\Users\ユーザー名"\AppData\Roaming
C:\ProgramData
C:\Users\ユーザー名"\AppData\Local
PersonalとMyDocumentsは同じ内容ですね
実際に定義を確認すると同じになるように定義されていました。
まぁこういうパス関連は覚えておくと便利ですよね
Application.DataPathとか^^
この記事へのコメント