1.9.5: Modified FilePathAttribute to take RelativeToProjectFolder, RelativeToStreamingFolder and Absolute
This commit is contained in:
parent
764027b615
commit
ea527c9b33
|
|
@ -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,6 +44,39 @@ 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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,18 +5,18 @@ namespace Module.Inspector
|
|||
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
|
||||
public sealed class FilePathAttribute : DrawerPropertyAttribute
|
||||
{
|
||||
public readonly bool useAbsolute;
|
||||
public readonly EFilePathType pathType;
|
||||
public readonly string extension = "*";
|
||||
|
||||
public FilePathAttribute(bool useAbsolute = false)
|
||||
public FilePathAttribute(EFilePathType pathType = EFilePathType.RelativeToProjectFolder)
|
||||
{
|
||||
this.useAbsolute = useAbsolute;
|
||||
this.pathType = pathType;
|
||||
}
|
||||
|
||||
public FilePathAttribute(string extension, bool useAbsolute = false)
|
||||
public FilePathAttribute(string extension, EFilePathType pathType = EFilePathType.RelativeToProjectFolder)
|
||||
{
|
||||
this.extension = extension;
|
||||
this.useAbsolute = useAbsolute;
|
||||
this.pathType = pathType;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Runtime/Enums/EFilePathType.cs
Normal file
9
Runtime/Enums/EFilePathType.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
namespace Module.Inspector
|
||||
{
|
||||
public enum EFilePathType
|
||||
{
|
||||
RelativeToProjectFolder,
|
||||
RelativeToStreamingFolder,
|
||||
Absolute
|
||||
}
|
||||
}
|
||||
3
Runtime/Enums/EFilePathType.cs.meta
Normal file
3
Runtime/Enums/EFilePathType.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7b865edb300f46409da22acb64876969
|
||||
timeCreated: 1725182290
|
||||
|
|
@ -1,12 +1,11 @@
|
|||
{
|
||||
"name": "com.module.inspector",
|
||||
"version": "1.9.4",
|
||||
"version": "1.9.5",
|
||||
"displayName": "Module.Inspector",
|
||||
"description": "Custom inspector with various useful property drawers",
|
||||
"unity": "2019.2",
|
||||
"unityRelease": "17f1",
|
||||
"dependencies": {
|
||||
},
|
||||
"dependencies": {},
|
||||
"keywords": [
|
||||
"Inspector"
|
||||
],
|
||||
|
|
@ -14,5 +13,6 @@
|
|||
"name": "Anders Ejlersen",
|
||||
"email": "anders@ejlersen.info",
|
||||
"url": "https://www.ejlersen.info"
|
||||
}
|
||||
},
|
||||
"_fingerprint": "764027b615a49f3d06cf0a8627ce2ad7ac6e217a"
|
||||
}
|
||||
Loading…
Reference in a new issue