Skip to content

Instantly share code, notes, and snippets.

@kou-yeung
Last active September 22, 2016 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou-yeung/d46b1d88c0e0081cae2edcfb40efe3b7 to your computer and use it in GitHub Desktop.
Save kou-yeung/d46b1d88c0e0081cae2edcfb40efe3b7 to your computer and use it in GitHub Desktop.
FileDialog DLL for Unity
// Unity用DLL作成
// 参考 : https://docs.unity3d.com/ja/current/Manual/UsingDLL.html
// 手順:
// Visual Studio を起動し [ファイル] > [新規作成] > [プロジェクト]を選択し
// VisualC# > Windows > クラスラライブラリ を作成する
// ※.今回は NET Framework3.5 にしました
//
// System.Windows.Forms の参照を追加
// ソリューションエクスプローラーに 参照を右クリックし参照の追加を選択する
// ※検索を使って System.Windows.Forms を検索すると見つかりやすいです
// 以下のコードを追加し、ビルドする
// ビルド終わったら binフォルダに dll が出力されます
namespace DLLTest
{
public class FileDialog
{
public static string Open()
{
var ofd = new OpenFileDialog();
// サンプルコードではダイアログ表示時のパラメータを省いてます。
if (ofd.ShowDialog() == DialogResult.OK)
{
return ofd.FileName;
}
return null;
}
}
}
// Unityから DLLを使用する
// 作成したDLLをUnityに追加する(Assets/Plugins/ に置く)
using DLLTest; // プラグインの using を追加
public class Sample : MonoBehaviour
{
public void OnOpenDialog()
{
var filepath = FileDialog.Open(); // これを実行するとダイアログを開き選択したファイルパスを取れます
}
}
// System.Windows.Forms を使ってるため、
// Build Settings > Player Settings > API Compatibility Level を .Net 2.0 にする必要があります。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment