Unity |ファイルダイアログを開いて、ファイルをバッチで選択します



Unity Open File Dialog Select Files Batch



シーナブログに記事を書きました ファイルダイアログを開いて、ファイルをバッチで選択します 記事ですが、残念ながらSinaブログはコードを書くことができません。ただし、当時は若すぎて、CSDNに遭遇するまでは不便を感じませんでした...えーと、私はそれに参加したくないので、ここで更新して他のメソッドを追加します。

  • オリジナルメソッド1

1.ソースコードは次のとおりです。



System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog() dlg.Multiselect = true dlg.RestoreDirectory = false dlg.ValidateNames = true dlg.FilterIndex = 1 dlg.Filter = 'Image file (*.jpg*.png*.jpeg)|*.jpg*.png*.jpeg|pdf file|*.pdf|All files|*.' //' Text files|*.*|C# files|*.cs|All files|*.*'// 'Image files or PDF files (*.jpg*.png*.jpeg*.pdf)|*. jpg*.png*.jpeg*.pdf' dlg.InitialDirectory = 'C:\' dlg.Title = 'Please select a picture or pdf to be recognized' if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { for (int i = 0 i

2.デメリット:ウィンドウスタイルが古く、パッケージ化後の操作に問題があります(フォルダが表示されません):



  • オリジナルの方法2

1.ソースコードは次のとおりです。

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class OpenFileName { public int structSize public IntPtr dlgOwner public IntPtr instance public string filter public string customFilter public int maxCustFilter public int filterIndex public string file public int maxFile public string fileTitle public int maxFileTitle public string initialDir //Open path null public string title public int flags public short fileOffset public short fileExtension public string defExt public IntPtr custData public IntPtr hook public string templateName public IntPtr reservedPtr public int reservedInt public int flagsEx } void OpenOfnInit() Openofn = new OpenFileName() Openofn.structSize = Marshal.SizeOf(Openofn) Openofn.dlgOwner = DllScript.GetForegroundWindow() Openofn.filter = 'Image file or PDF file (*.jpg*.png*.jpeg*.pdf)*.jpg*.png*.jpeg*.pdf' Openofn.file = new string(new char[1024]) Openofn.maxFile = Openofn.file.Length Openofn.fileTitle = new string(new char[64]) Openofn.maxFileTitle = Openofn.fileTitle.Length Openofn.initialDir = 'C:\' Openofn.title = 'Select image or PDF file' Openofn.defExt = 'PDF' Openofn.flags = 0x00080000 [DllImport('Comdlg32.dll', SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] public static extern bool GetOpenFileName([In, Out] OpenFileName ofn) if (GetOpenFileName(Openofn)) { string[] SplitStr = { '' } string[] strs = Openofn.file.Split(SplitStr, StringSplitOptions.RemoveEmptyEntries) for (int i = 0 i

2.短所:32ビットがリリースされたときに選択できるのは単一選択ファイルのみです:0x00000200を削除します

  • 方法3:Unityエディターウィンドウでのみ使用できます
string[] fliter = {'picture or pdf', 'jpg*.png*.jpeg*.pdf' } string path = UnityEditor.EditorUtility.OpenFilePanelWithFilters('Overwrite with png', 'C:\', fliter)
  • 方法1の場合、フォルダーを開く方法を追加します。
System.Windows.Forms.FolderBrowserDialog _dlg = new System.Windows.Forms.FolderBrowserDialog() if (_dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Debug.Log(_dlg.SelectedPath) }