@@ -60,7 +60,7 @@ public override void OnInspectorGUI() {
60
60
61
61
// Unless the user canceled, make sure they chose something in the Assets folder.
62
62
if ( ! string . IsNullOrEmpty ( fullPath ) ) {
63
- var relativePath = GetRelativePath ( Application . dataPath , fullPath ) ;
63
+ var relativePath = ExportSettings . ConvertToAssetRelativePath ( fullPath ) ;
64
64
if ( string . IsNullOrEmpty ( relativePath )
65
65
|| relativePath == ".."
66
66
|| relativePath . StartsWith ( ".." + Path . DirectorySeparatorChar ) ) {
@@ -81,39 +81,6 @@ public override void OnInspectorGUI() {
81
81
}
82
82
}
83
83
84
- private string GetRelativePath ( string fromDir , string toDir ) {
85
- // https://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path
86
- // With fixes to handle that fromDir and toDir are both directories (not files).
87
- if ( String . IsNullOrEmpty ( fromDir ) ) throw new ArgumentNullException ( "fromDir" ) ;
88
- if ( String . IsNullOrEmpty ( toDir ) ) throw new ArgumentNullException ( "toDir" ) ;
89
-
90
- // MakeRelativeUri assumes the path is a file unless it ends with a
91
- // path separator, so add one. Having multiple in a row is no problem.
92
- fromDir += Path . DirectorySeparatorChar ;
93
- toDir += Path . DirectorySeparatorChar ;
94
-
95
- // Workaround for https://bugzilla.xamarin.com/show_bug.cgi?id=5921
96
- fromDir += Path . DirectorySeparatorChar ;
97
-
98
- Uri fromUri = new Uri ( fromDir ) ;
99
- Uri toUri = new Uri ( toDir ) ;
100
-
101
- if ( fromUri . Scheme != toUri . Scheme ) { return null ; } // path can't be made relative.
102
-
103
- Uri relativeUri = fromUri . MakeRelativeUri ( toUri ) ;
104
- String relativePath = Uri . UnescapeDataString ( relativeUri . ToString ( ) ) ;
105
-
106
- if ( string . IsNullOrEmpty ( relativePath ) ) {
107
- // The relative path is empty if it's the same directory.
108
- relativePath = "./" ;
109
- }
110
-
111
- if ( toUri . Scheme . Equals ( "file" , StringComparison . InvariantCultureIgnoreCase ) ) {
112
- relativePath = relativePath . Replace ( Path . AltDirectorySeparatorChar , Path . DirectorySeparatorChar ) ;
113
- }
114
-
115
- return relativePath ;
116
- }
117
84
}
118
85
119
86
[ FilePath ( "ProjectSettings/FbxExportSettings.asset" , FilePathAttribute . Location . ProjectFolder ) ]
@@ -148,19 +115,7 @@ public static string GetRelativeSavePath() {
148
115
if ( string . IsNullOrEmpty ( relativePath ) ) {
149
116
relativePath = kDefaultSavePath ;
150
117
}
151
-
152
- // Normalize to the platform path separator.
153
- relativePath = relativePath . Replace ( Path . AltDirectorySeparatorChar ,
154
- Path . DirectorySeparatorChar ) ;
155
-
156
- // Trim off trailing slashes. If all we had was slashes, we're at
157
- // the root of the Application.dataPath so return "."
158
- relativePath = relativePath . TrimEnd ( Path . DirectorySeparatorChar ) ;
159
- if ( string . IsNullOrEmpty ( relativePath ) ) {
160
- relativePath = "." ;
161
- }
162
-
163
- return relativePath ;
118
+ return NormalizeRelativePath ( relativePath ) ;
164
119
}
165
120
166
121
/// <summary>
@@ -183,6 +138,66 @@ public static void SetRelativeSavePath(string newPath) {
183
138
. TrimEnd ( Path . DirectorySeparatorChar ) ;
184
139
}
185
140
141
+ /// <summary>
142
+ /// Convert an absolute path into a relative path like what you would
143
+ /// get from GetRelativeSavePath.
144
+ ///
145
+ /// Returns an empty string if the path is invalid.
146
+ /// The path uses platform path separators, and no trailing or leading
147
+ /// slashes.
148
+ /// </summary>
149
+ public static string ConvertToAssetRelativePath ( string fullPathInAssets )
150
+ {
151
+ return GetRelativePath ( Application . dataPath , fullPathInAssets ) ;
152
+ }
153
+
154
+ private static string GetRelativePath ( string fromDir , string toDir ) {
155
+ // https://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path
156
+ // With fixes to handle that fromDir and toDir are both directories (not files).
157
+ if ( String . IsNullOrEmpty ( fromDir ) ) throw new ArgumentNullException ( "fromDir" ) ;
158
+ if ( String . IsNullOrEmpty ( toDir ) ) throw new ArgumentNullException ( "toDir" ) ;
159
+
160
+ // MakeRelativeUri assumes the path is a file unless it ends with a
161
+ // path separator, so add one. Having multiple in a row is no problem.
162
+ fromDir += Path . DirectorySeparatorChar ;
163
+ toDir += Path . DirectorySeparatorChar ;
164
+
165
+ // Workaround for https://bugzilla.xamarin.com/show_bug.cgi?id=5921
166
+ fromDir += Path . DirectorySeparatorChar ;
167
+
168
+ Uri fromUri = new Uri ( fromDir ) ;
169
+ Uri toUri = new Uri ( toDir ) ;
170
+
171
+ if ( fromUri . Scheme != toUri . Scheme ) { return null ; } // path can't be made relative.
172
+
173
+ Uri relativeUri = fromUri . MakeRelativeUri ( toUri ) ;
174
+ String relativePath = Uri . UnescapeDataString ( relativeUri . ToString ( ) ) ;
175
+
176
+ if ( string . IsNullOrEmpty ( relativePath ) ) {
177
+ // The relative path is empty if it's the same directory.
178
+ relativePath = "." ;
179
+ }
180
+
181
+ return NormalizeRelativePath ( relativePath ) ;
182
+ }
183
+
184
+ private static string NormalizeRelativePath ( string relativePath )
185
+ {
186
+ // Normalize to the platform path separator.
187
+ relativePath = relativePath . Replace (
188
+ Path . AltDirectorySeparatorChar ,
189
+ Path . DirectorySeparatorChar ) ;
190
+
191
+ // Trim off leading and trailing slashes. If all we had was
192
+ // slashes, we're at the root of the Application.dataPath so return
193
+ // "."
194
+ relativePath = relativePath . Trim ( Path . DirectorySeparatorChar ) ;
195
+ if ( string . IsNullOrEmpty ( relativePath ) ) {
196
+ relativePath = "." ;
197
+ }
198
+ return relativePath ;
199
+ }
200
+
186
201
[ MenuItem ( "Edit/Project Settings/Fbx Export" , priority = 300 ) ]
187
202
static void ShowManager ( )
188
203
{
0 commit comments