I’m writing an editor script that needs a directory location. It works fine for everything I want, and remembers the last location to Editor.prefs.
Where I’m having trouble is how to capture an error if a path is invalid.
This is the code snippet that’s giving the trouble:
if (absolutePath == "") {
if (EditorPrefs.HasKey ("AbsolutePath")) {
absolutePath = EditorPrefs.GetString ("AbsolutePath");
}
}
if (absolutePath == "") {
// Generic absolutePath that will default to the current project folder.
absolutePath = EditorUtility.SaveFolderPanel("Select Directory to Save PreFabs","","");
// You may hard code your initial absolutePath path here: (Please note that any spaces in the absolutePath could result in errors.)
// absolutePath = EditorUtility.SaveFolderPanel("Select Directory to Save PreFabs","/Volumes/MyHDD/MyUnityProjects/MyCurrentProject/Assets/Elements/PreFabs","");
} else {
absolutePath = EditorUtility.SaveFolderPanel("Select Directory to Save PreFabs", absolutePath,"");
}
if (!string.IsNullOrEmpty(absolutePath)) {
pathToCreatePrefabs = absolutePath.Substring(absolutePath.IndexOf("Assets/"));
}
if (pathToCreatePrefabs == "") {
pathToCreatePrefabs = "No Directory Selected";
}
EditorPrefs.SetString ("AbsolutePath", absolutePath);
}
This works fine unless the saved target directory has been moved or deleted, making the absolutePath invalid… then it defaults to the system’s root directory… not even the current project file which is what you get from this line:
absolutePath = EditorUtility.SaveFolderPanel(“Select Directory to Save PreFabs”,“”,“”);
What I want to logically test is “if (absolutePath is Valid)…”, but I can’t find anything like this in the docs.
Does anyone have a workaround or suggestion on what to do to test in invalid path?