Odin Attributes 大全之 Type Specifics 系列 | (8)File Path 特性

📂 Unity常用插件 | 总目录

📂 Unity 3D资源 | 目录索引

Odin Attributes 为Unity开发者提供了更多的自定义选项,使得开发过程更加高效和愉悦。通过使用这些特性,开发者可以创建更加专业和用户友好的编辑器界面,从而提升整个开发团队的工作效率。

在这里插入图片描述

【Odin Inspector and Serializer 最新版 (0积分)免费下载


File Path 特性用于字符串属性,并在Inspector面板对应的属性值旁绘制一个文件夹按钮,便于快读定位文件,并提供文件路径的接口。

1.【FilePath】直接使用

在这里插入图片描述

2.【FilePath】默认提供一个相对于Unity的路径

    // 默认情况下,FolderPath提供了一个相对于Unity项目的路径。
    [FilePath]
    public string UnityProjectPath;

在这里插入图片描述

3.【ParentFolder】可以提供自定义父路径。父路径可以是相对于Unity项目的,也可以是绝对的。

    // 可以提供自定义父路径。父路径可以是相对于Unity项目的,也可以是绝对的。
    [FilePath(ParentFolder = "Assets/Plugins/Sirenix")]
    public string RelativeToParentPath;

在这里插入图片描述

如果找不到最对应的路径,还可以提供一个相对于目标路径的路径

    // 使用父路径,FilePath还可以提供相对于resources文件夹的路径。
    [FilePath(ParentFolder = "Assets/Resources")]
    public string ResourcePath;

在这里插入图片描述

4.【Extensions】根据扩展名来筛选文件

    // 提供一个逗号分隔的允许扩展列表。点(.)是可选的。
    [FilePath(Extensions = "cs, lua")]
    [BoxGroup("Conditions")]
    public string ScriptFiles;

在这里插入图片描述

5.【AbsolutePath】设置是否显示绝对路径

    // 通过将AbsolutePath设置为true,文件路径将提供一个绝对路径。
    [PropertySpace(40,40)]
    [FilePath(AbsolutePath = true)]
    public string AbsolutePath;

在这里插入图片描述

6.【RequireExistingPath】如果提供的路径无效,还可以将FilePath配置为显示错误信息。

    // 如果提供的路径无效,还可以将FilePath配置为显示错误信息。
    [FilePath(RequireExistingPath = true)]
    public string ExistingPath;

7.【UseBackslashes】默认情况下,FilePath将强制使用前斜杠。还可以将其配置为使用反斜杠

    // 默认情况下,FilePath将强制使用前斜杠。还可以将其配置为使用反斜杠
    [PropertySpace(40, 40)]
    [FilePath(UseBackslashes = true)]
    public string Backslashes;

在这里插入图片描述

8.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";

在这里插入图片描述

9.FilePath还支持列表和数组。

    // FilePath还支持列表和数组。
    [FilePath(ParentFolder = "Assets/Scenes/Odin-Inspector-Chinese-Tutorial/")]
    [BoxGroup("Lists")]
    public string[] ListOfFiles;

在这里插入图片描述

完整示例代码

using Sirenix.OdinInspector;
using UnityEngine;

public class FilePathAttributeExample : MonoBehaviour
{
    // 默认情况下,FolderPath提供了一个相对于Unity项目的路径。
    [FilePath]
    public string UnityProjectPath;

    // 可以提供自定义父路径。父路径可以是相对于Unity项目的,也可以是绝对的。
    [FilePath(ParentFolder = "Assets/Plugins/Sirenix")]
    public string RelativeToParentPath;

    // 使用父路径,FilePath还可以提供相对于resources文件夹的路径。
    [FilePath(ParentFolder = "Assets/Resources")]
    public string ResourcePath;

    // 提供一个逗号分隔的允许扩展列表。点(.)是可选的。
    [FilePath(Extensions = "cs, lua")]
    [BoxGroup("Conditions")]
    public string ScriptFiles;

    // 通过将AbsolutePath设置为true,文件路径将提供一个绝对路径。
    [PropertySpace(40,40)]
    [FilePath(AbsolutePath = true)]
    public string AbsolutePath;

    // 如果提供的路径无效,还可以将FilePath配置为显示错误信息。
    [FilePath(RequireExistingPath = true)]
    public string ExistingPath;

    // 默认情况下,FilePath将强制使用前斜杠。还可以将其配置为使用反斜杠
    [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还支持列表和数组。
    [FilePath(ParentFolder = "Assets/Scenes/Odin-Inspector-Chinese-Tutorial/")]
    [BoxGroup("Lists")]
    public string[] ListOfFiles;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity打怪升级

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值