Tanoda
StandaloneFileBrowserEditor.cs
Go to the documentation of this file.
1#if UNITY_EDITOR
2using System;
3using System.Collections.Generic;
4using UnityEditor;
5
6namespace TriLibCore.SFB
7{
8 public class StandaloneFileBrowserEditor : IStandaloneFileBrowser<ItemWithStream>
9 {
10 public IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions,
11 bool multiselect)
12 {
13 var path = "";
14 if (extensions == null)
15 path = EditorUtility.OpenFilePanel(title, directory, "");
16 else
17 path = EditorUtility.OpenFilePanelWithFilters(title, directory,
18 GetFilterFromFileExtensionList(extensions));
19 var itemWithStream = new ItemWithStream
20 {
21 Name = path //,
22 //Stream = File.OpenRead(path)
23 };
24 return new[] {itemWithStream};
25 }
26
27 public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect,
28 Action<IList<ItemWithStream>> cb)
29 {
30 cb(OpenFilePanel(title, directory, extensions, multiselect));
31 }
32
33 public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
34 {
35 var path = EditorUtility.OpenFolderPanel(title, directory, "");
36 var itemWithStream = new ItemWithStream
37 {
38 Name = path //,
39 //Stream = File.OpenRead(path)
40 };
41 return new[] {itemWithStream};
42 }
43
44 public void OpenFolderPanelAsync(string title, string directory, bool multiselect,
45 Action<IList<ItemWithStream>> cb)
46 {
47 cb(OpenFolderPanel(title, directory, multiselect));
48 }
49
50 public ItemWithStream SaveFilePanel(string title, string directory, string defaultName,
51 ExtensionFilter[] extensions)
52 {
53 var ext = extensions != null ? extensions[0].Extensions[0] : "";
54 var name = string.IsNullOrEmpty(ext) ? defaultName : defaultName + "." + ext;
55 var path = EditorUtility.SaveFilePanel(title, directory, name, ext);
56 var itemWithStream = new ItemWithStream
57 {
58 Name = path //,
59 //Stream = File.OpenRead(path)
60 };
61 return itemWithStream;
62 }
63
64 public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions,
65 Action<ItemWithStream> cb)
66 {
67 cb(SaveFilePanel(title, directory, defaultName, extensions));
68 }
69
70 private static string[] GetFilterFromFileExtensionList(ExtensionFilter[] extensions)
71 {
72 var filters = new string[extensions.Length * 2];
73 for (var i = 0; i < extensions.Length; i++)
74 {
75 filters[i * 2] = extensions[i].Name;
76 filters[i * 2 + 1] = string.Join(",", extensions[i].Extensions);
77 }
78
79 return filters;
80 }
81 }
82}
83
84#endif