Tanoda
FileChooser.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.IO;
10using UnityEditor;
11using UnityEngine;
12
13namespace Leap.Unity.Attributes {
14
15 public enum FileDialogType { Open, Save, Folder };
16
19 string extension = null) : base(FileDialogType.Open,
21 }
22
25 string extension = null) : base(FileDialogType.Save,
27 }
28
31 string extension = null) : base(FileDialogType.Folder,
33 }
34
37 {
38
40 public bool preserveExistingFileName = false;
42 public string extension = null;
43
45 bool preserveExistingFileName = false,
46 string extension = null)
47 {
48 this.dialogType = dialogType;
49 this.preserveExistingFileName = preserveExistingFileName;
50 this.extension = extension;
51 }
52
53 #if UNITY_EDITOR
54
55 public void Draw(Rect rect, SerializedProperty property) {
56 var existingValue = property.stringValue;
57 var pipeSyntaxPath = PipeFileSyntax.Parse(property.stringValue);
58 existingValue = pipeSyntaxPath.path;
59
60 string currentDir = null;
61 if (!string.IsNullOrEmpty(existingValue)) {
62 currentDir = Path.GetDirectoryName(existingValue);
63 }
64
65 string chosenFile = null;
66 if (GUI.Button(rect, "...")) {
67 if (dialogType == FileDialogType.Folder) {
68 chosenFile = EditorUtility.OpenFolderPanel("Choose Folder", currentDir, null);
69 if (!string.IsNullOrEmpty(chosenFile)) {
70 chosenFile += Path.DirectorySeparatorChar;
71 if (!string.IsNullOrEmpty(existingValue) && preserveExistingFileName) {
72 var existingName = Path.GetFileName(existingValue);
73 chosenFile = Path.Combine(chosenFile, existingName);
74 }
75 }
76 }
77 else if (dialogType == FileDialogType.Open) {
78 chosenFile = EditorUtility.OpenFilePanel("Choose File", currentDir,
79 null);
80 }
81 else { // dialogType == FileDialogType.Save
82 chosenFile = EditorUtility.SaveFilePanel("Output File", currentDir,
83 "", null);
84 }
85 }
86
87 if (!string.IsNullOrEmpty(chosenFile)) {
88 property.stringValue = pipeSyntaxPath.ChangePath(chosenFile).ToString();
89 }
90 }
91
92 #endif
93
94 public float GetWidth() {
95 return 24;
96 }
97 }
98
99}
FileChooserAttribute(FileDialogType dialogType, bool preserveExistingFileName=false, string extension=null)
Definition: FileChooser.cs:44
string extension
Expected file extension .
Definition: FileChooser.cs:42
FolderChooserAttribute(bool preserveExistingFileName=false, string extension=null)
Definition: FileChooser.cs:30
ReadFileChooserAttribute(bool preserveExistingFileName=false, string extension=null)
Definition: FileChooser.cs:18
WriteFileChooserAttribute(bool preserveExistingFileName=false, string extension=null)
Definition: FileChooser.cs:24
string path
The file path, cleaned of any pipe syntax.
static PipeFileSyntax Parse(string pathMaybeWithPipes)