Alpaca Tech Blog

ゲーム作る系 草食動物の備忘録

【Unity】using System.Windows.Formsを利用する(Windowsモジュール限定)

概要

UnityでWindowsのフォームやダイアログを利用する場合、

using System.Windows.Forms;

を宣言すると利用できます。

 

エラー

そのまま宣言しても、

The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?

というエラーが出ます。

 

System.Windows.Formsを利用するには

1.Api Compatibility Levelを .Net 2.0にする

メニューのEdit -> Project Settings -> Player -> Other Settings

 

f:id:alpacatech:20180725010542p:plain

Api Compatibility Levelを .Net 2.0にします(Subsetじゃないほう)

 

2.PluginsフォルダにSystem.Windows.Forms.dllをコピー

AssetsフォルダにPluginsフォルダを生成します。

f:id:alpacatech:20180725010511p:plain

 

C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0

から、

System.Windows.Forms.dll
をコピーします。

必要であれば、
System.Drawing.dll
System.Data.dll

等もコピーします。

 

これで利用できます。

 

おまけ

ファイルを開くダイアログ
    var dlg = new OpenFileDialog();
    dlg.Filter = "json(*.json)|*.json|All files(*.*)|*.*";
    dlg.CheckFileExists = false;
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        //  ...
        MessageBox.Show(dlg.FileName);
    }
ファイルを保存ダイアログ
    var dlg = new SaveFileDialog();
    dlg.Filter = "json(*.json)|*.json|All files(*.*)|*.*";
    dlg.CheckFileExists = false;
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        //  ...
        MessageBox.Show(dlg.FileName);
    }

 

 

参考

設定

scribble/README.md at master · i-saint/scribble · GitHub

 

ファイルを開くダイアログ

[Unity] OpenFileDialogをWindowsアプリ(exe)で使う