1.9.5: Modified FilePathAttribute to take RelativeToProjectFolder, RelativeToStreamingFolder and Absolute

This commit is contained in:
Anders Ejlersen 2024-09-01 11:38:59 +02:00
parent 764027b615
commit ea527c9b33
5 changed files with 60 additions and 14 deletions

View file

@ -27,13 +27,14 @@ namespace Module.Inspector.Editor
if (GUI.Button(rect1, "Find"))
{
string path = EditorUtility.OpenFilePanel("File", "Assets/", att.extension);
string folderPath = GetInitialFolderPath(att.pathType);
string path = EditorUtility.OpenFilePanel("File", folderPath, att.extension);
if (!string.IsNullOrEmpty(path))
path = TransformAbsolutePathTo(path, att.pathType);
if (!string.IsNullOrEmpty(path))
{
if (!att.useAbsolute && path.StartsWith(Application.dataPath))
path = path.Remove(0, Application.dataPath.Length - 6);
property.stringValue = path;
property.serializedObject.ApplyModifiedProperties();
}
@ -43,7 +44,40 @@ namespace Module.Inspector.Editor
return true;
}
private static string GetInitialFolderPath(EFilePathType pathType)
{
if (pathType == EFilePathType.RelativeToStreamingFolder)
return "Assets/StreamingAssets/";
return "Assets/";
}
private string TransformAbsolutePathTo(string absolutePath, EFilePathType pathType)
{
absolutePath = absolutePath.Replace('\\', '/');
var dataPath = Application.dataPath.Replace('\\', '/');
if (pathType == EFilePathType.RelativeToProjectFolder)
{
if (absolutePath.StartsWith(dataPath))
return absolutePath.Remove(0, dataPath.Length - 6);
Debug.LogWarning("Path didn't include 'Assets'-folder");
return string.Empty;
}
if (pathType == EFilePathType.RelativeToStreamingFolder)
{
if (absolutePath.StartsWith($"{dataPath}/StreamingAssets"))
return absolutePath.Remove(0, dataPath.Length + 17);
Debug.LogWarning("Path didn't include 'StreamingAssets'-folder");
return string.Empty;
}
return absolutePath;
}
public override string GetErrorMessage(SerializedProperty property)
{
return "Only supports strings";