LoginSignup
5
1

More than 5 years have passed since last update.

UnityでOpenFileDialog(外部ファイルの読み込み)

Posted at

概要

UnityでWindowsの(ような)外部ファイル読み込み窓(OpenFileDialog)を使う

準備

・System.Windows.Forms.dll
上記ファイルを『(Unityインストールフォルダ)\Editor\Data\Mono\lib\mono\2.0』から
Assetフォルダにコピーする。(unityバージョンでのPlugins云々には触れません)
※Windowsの同名dllでは動きません。

実装(※最低限)

openfiledialog.cs
using System.Windows.Forms;

public class openfiledialog : MonoBehaviour
    void Load(){
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter =  "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        ofd.Title = "テキストファイルを選択してください";
        ofd.ShowDialog();

        string filePath = openFileDialog.FileName;

詳しくはこちら
OpenFileDialog Class (System.Windows.Forms) | Microsoft Docs
ok,キャンセル周りはきっちりやっといたほうが良いと思う。

その他

ぱぱっと実装できるFileDialogではあるけれど
monoで少々デザインが違ってたりはする。
そのままストリームリーダーに投げれたりもするので便利。

デザインが気に入らなければ、アセットストアから買ってくるのもあり。
uGUI上で動くもの、VR内で使えるもの、Windows標準デザインのものもある。
というか標準デザインのもの使いたい。

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1