File Path Attribute特性:用于字符串屬性,并在Inspector面板對應(yīng)的屬性值旁繪制一個文件夾按鈕,便于快讀定位文件,并提供文件路徑的接口。
【FilePath】直接使用,默認(rèn)提供一個相對于Unity的路徑
// 默認(rèn)情況下,F(xiàn)olderPath提供了一個相對于Unity項目的路徑。
[FilePath]
public string UnityProjectPath;
【ParentFolder】可以提供自定義父路徑。父路徑可以是相對于Unity項目的,也可以是絕對的。
// 可以提供自定義父路徑。父路徑可以是相對于Unity項目的,也可以是絕對的。
[FilePath(ParentFolder = "Assets/Plugins/Sirenix")]
public string RelativeToParentPath;
如果找不到最對應(yīng)的路徑,還可以提供一個相對于目標(biāo)路徑的路徑
// 使用父路徑,F(xiàn)ilePath還可以提供相對于resources文件夾的路徑。
[FilePath(ParentFolder = "Assets/Resources")]
public string ResourcePath;
【Extensions】根據(jù)擴(kuò)展名來篩選文件
// 提供一個逗號分隔的允許擴(kuò)展列表。點(.)是可選的。
[FilePath(Extensions = "cs, lua")]
[BoxGroup("Conditions")]
public string ScriptFiles;
【AbsolutePath】設(shè)置是否顯示絕對路徑
// 通過將AbsolutePath設(shè)置為true,文件路徑將提供一個絕對路徑。
[PropertySpace(40,40)]
[FilePath(AbsolutePath = true)]
public string AbsolutePath;
【RequireExistingPath】如果提供的路徑無效,還可以將FilePath配置為顯示錯誤信息。
// 如果提供的路徑無效,還可以將FilePath配置為顯示錯誤信息。
[FilePath(RequireExistingPath = true)]
public string ExistingPath;
【UseBackslashes】默認(rèn)情況下,F(xiàn)ilePath將強(qiáng)制使用前斜杠。還可以將其配置為使用反斜杠
// 默認(rèn)情況下,F(xiàn)ilePath將強(qiáng)制使用前斜杠。還可以將其配置為使用反斜杠
[PropertySpace(40, 40)]
[FilePath(UseBackslashes = true)]
public string Backslashes;
FilePath還支持使用$符號的成員引用。
// FilePath還支持使用$符號的成員引用。
[FilePath(ParentFolder = "$DynamicParent", Extensions = "$DynamicExtensions")]
[BoxGroup("Member referencing")]
public string DynamicFilePath;
[BoxGroup("Member referencing")]
public string DynamicParent = "Assets/Plugin/Sirenix";
[BoxGroup("Member referencing")]
public string DynamicExtensions = "cs, unity, jpg";
FilePath還支持列表和數(shù)組。
// FilePath還支持列表和數(shù)組。
[FilePath(ParentFolder = "Assets/Scenes/Odin-Inspector-Chinese-Tutorial/")]
[BoxGroup("Lists")]
public string[] ListOfFiles;
完整示例代碼
using Sirenix.OdinInspector;
using UnityEngine;
public class FilePathAttributeExample : MonoBehaviour
{
// 默認(rèn)情況下,F(xiàn)olderPath提供了一個相對于Unity項目的路徑。
[FilePath]
public string UnityProjectPath;
// 可以提供自定義父路徑。父路徑可以是相對于Unity項目的,也可以是絕對的。
[FilePath(ParentFolder = "Assets/Plugins/Sirenix")]
public string RelativeToParentPath;
// 使用父路徑,F(xiàn)ilePath還可以提供相對于resources文件夾的路徑。
[FilePath(ParentFolder = "Assets/Resources")]
public string ResourcePath;
// 提供一個逗號分隔的允許擴(kuò)展列表。點(.)是可選的。
[FilePath(Extensions = "cs, lua")]
[BoxGroup("Conditions")]
public string ScriptFiles;
// 通過將AbsolutePath設(shè)置為true,文件路徑將提供一個絕對路徑。
[PropertySpace(40,40)]
[FilePath(AbsolutePath = true)]
public string AbsolutePath;
// 如果提供的路徑無效,還可以將FilePath配置為顯示錯誤信息。
[FilePath(RequireExistingPath = true)]
public string ExistingPath;
// 默認(rèn)情況下,F(xiàn)ilePath將強(qiáng)制使用前斜杠。還可以將其配置為使用反斜杠
[PropertySpace(40, 40)]
[FilePath(UseBackslashes = true)]
public string Backslashes;
// FilePath還支持使用$符號的成員引用。
[FilePath(ParentFolder = "$DynamicParent", Extensions = "$DynamicExtensions")]
[BoxGroup("Member referencing")]
public string DynamicFilePath;
[BoxGroup("Member referencing")]
public string DynamicParent = "Assets/Plugin/Sirenix";
[BoxGroup("Member referencing")]
public string DynamicExtensions = "cs, unity, jpg";
// FilePath還支持列表和數(shù)組。
[FilePath(ParentFolder = "Assets/Scenes/Odin-Inspector-Chinese-Tutorial/")]
[BoxGroup("Lists")]
public string[] ListOfFiles;
}