UnityEditor系を使わずに、という意味だったんですね。
でしたら、OSX環境なら、こんな感じでしょうか。
lang
1 using UnityEngine;
2 using System.Collections;
3 using System.Diagnostics;
4 using System.Text;
5
6 public class FileChooser : MonoBehaviour
7 {
8 public const string CHOOSE_FILE =
9 "-e 'POSIX path of (get choose file of application \"System Events\" activate)'";
10 public const string CHOOSE_FOLDER =
11 "-e 'POSIX path of (get choose folder of application \"System Events\" activate)'";
12 public const string CHOOSE_NEW_FILE =
13 "-e 'POSIX path of (get choose file name of application \"System Events\" activate)'";
14 public const string SIMPLE_DIALOG =
15 "-e 'tell application \"System Events\"\nactivate\ndisplay dialog \"clean\" buttons {\"OK\"}\nend tell'";
16 public const string FORCE_ACTIVATE =
17 "-e 'tell application \"System Events\" to activate'";
18
19 void Start()
20 {
21 StartCoroutine (FileChooseDialog (CHOOSE_FILE, (string path) => {
22 UnityEngine.Debug.Log ("this is " + path);
23 }));
24 }
25
26 /// <summary>
27 /// osascript -e 'tell application "System Events" to POSIX path of (active choose file)'
28 /// or
29 /// osascript -e 'POSIX path of (get choose file of application "System Events" activate)'
30 /// </summary>
31 /// <returns>The choose dialog.</returns>
32 public IEnumerator FileChooseDialog (string chooseType, System.Action<string> onClosed)
33 {
34 Process fileDialog = new Process ();
35 fileDialog.StartInfo = new ProcessStartInfo ()
36 {
37 FileName = "osascript",
38 Arguments = chooseType,
39 CreateNoWindow = true,
40 UseShellExecute = false,
41 RedirectStandardOutput = true,
42 RedirectStandardError = true,
43 };
44 StringBuilder result = new StringBuilder ();
45 fileDialog.OutputDataReceived += (object sender, DataReceivedEventArgs e) => {
46 result.Append (e.Data);
47 };
48 fileDialog.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => {
49 UnityEngine.Debug.LogError (e.Data);
50 };
51 fileDialog.Start ();
52 fileDialog.BeginOutputReadLine ();
53 fileDialog.BeginErrorReadLine ();
54 yield return null;
55 Process.Start (new ProcessStartInfo () { FileName = "osascript", Arguments = FORCE_ACTIVATE });
56 while (fileDialog.HasExited == false) {
57 yield return null;
58 }
59 fileDialog.Close ();
60 fileDialog.Dispose ();
61 onClosed.Invoke (result.ToString ());
62 }
63 }
osascriptというコマンドがあるので、これを使えば標準機能なら概ね操作できます。
ただ、ファイル選択ダイアログ周りは急に反応が無くなったりもするので、注意です。
反応が無い場合は、シンプルなダイアログ(サンプルならSIMPLE_DIALOGを使って)を表示してやれば上手くいったりします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/02/25 10:38
2015/02/25 12:42
2015/02/25 12:47
2018/05/04 08:59