2024-07-16 Unity插件 Odin Inspector5 —— Conditional Attributes

news2024/9/23 11:15:46

文章目录

  • 1 说明
  • 2 条件特性
    • 2.1 DisableIf / EnableIf
    • 2.2 DisableIn / EnableIn / ShowIn / HideIn
    • 2.3 DisableInEditorMode / HideInEditorMode
    • 2.4 DisableInInlineEditors / ShowInInlineEditors / HideInInlineEditors
    • 2.5 DisableInPlayMode / HideInPlayMode
    • 2.6 ShowIf / HideIf
    • 2.7 ShowIfGroup / HideIfGroup

1 说明

​ 本文介绍 Odin Inspector 插件中条件特性的使用方法。

2 条件特性

2.1 DisableIf / EnableIf

如果解析的字符串计算结果为指定值,则禁用 / 启用 Inspector 窗口中的属性更改。

  • string condition

    检查值条件的解析字符串,如成员名称、表达式等。

  • object optionalValue

    要检查的值。

image-20240715225210588
// DisableIfExamplesComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

public class DisableIfExamplesComponent : MonoBehaviour
{
    public UnityEngine.Object SomeObject;

    [EnumToggleButtons]
    public InfoMessageType SomeEnum;

    public bool IsToggled;

    [DisableIf("SomeEnum", InfoMessageType.Info)]
    public Vector2 Info;

    [DisableIf("SomeEnum", InfoMessageType.Error)]
    public Vector2 Error;

    [DisableIf("SomeEnum", InfoMessageType.Warning)]
    public Vector2 Warning;

    [DisableIf("IsToggled")]
    public int DisableIfToggled;

    [DisableIf("SomeObject")]
    public Vector3 EnabledWhenNull;

    [DisableIf("@this.IsToggled && this.SomeObject != null || this.SomeEnum == InfoMessageType.Error")]
    public int DisableWithExpression;
}

2.2 DisableIn / EnableIn / ShowIn / HideIn

当该对象所在的脚本挂载在何种预制体上,其修饰的对象禁止 / 允许在 Inspector 窗口中的更改 / 显示。

  • PrefabKind prefabKind

    预制体的种类。

    1. None

      所有预制体,都不满足条件。

    2. InstanceInScene

      场景中的预制体实例。

    3. InstanceInPrefab

      嵌套在其他预制体中的预制件实例。

    4. Regular

      常规预制体。

    5. Variant

      预制体资产。

    6. NonPrefabInstance

      非预制体或场景中的游戏对象实例。

    7. PrefabInstance = InstanceInPrefab | InstanceInScene

      常规预制体的实例,以及场景中或嵌套在其他预制体中的预制体。

    8. PrefabAsset = Variant | Regular

      常规预制体和预制体资产。

    9. PrefabInstanceAndNonPrefabInstance = PrefabInstance | NonPrefabInstance

      预制体以及非预制实例。

    10. All = PrefabInstanceAndNonPrefabInstance | PrefabAsset

      所有预制体。

image-20240715231309872
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;

public class Test : MonoBehaviour
{
    [DisableIn(PrefabKind.PrefabAsset)]
    public int i;
}

2.3 DisableInEditorMode / HideInEditorMode

在不处于播放模式时禁用 / 隐藏对象。只希望在播放模式下可编辑 / 显示时,可使用此选项。

image-20240715233626355
// DisableInEditorModeExamplesComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

public class DisableInEditorModeExamplesComponent : MonoBehaviour
{
    [Title("Disabled in edit mode")]
    [DisableInEditorMode]
    public GameObject A;

    [DisableInEditorMode]
    public Material B;
}

2.4 DisableInInlineEditors / ShowInInlineEditors / HideInInlineEditors

如果对象被 InlineEditor 特性绘制,则该对象在 Inspector 窗口中禁用更改 / 显示 / 隐藏。

补充 InlineEditor 特性:将继承 UnityEngine.Object 的类(如 ScriptableObject)的详细信息显示在 Inspector 窗口。

image-20240715234820556

​ 数据结构类:

using UnityEngine;

#nullable disable
namespace Sirenix.OdinInspector.Editor.Examples
{
  public class DisabledInInlineEditorScriptableObject : ScriptableObject
  {
    public string AlwaysEnabled;
    [DisableInInlineEditors]
    public string DisabledInInlineEditor;
  }
}

​ 挂载的脚本:

// DisableInInlineEditorExampleComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

#if UNITY_EDITOR // Editor namespaces can only be used in the editor.
using Sirenix.OdinInspector.Editor.Examples;
#endif

public class DisableInInlineEditorExampleComponent : MonoBehaviour
{
#if UNITY_EDITOR // DisabledInInlineEditorScriptableObject is an example type and only exists in the editor
    [InfoBox("Click the pen icon to open a new inspector window for the InlineObject too see the difference this attribute makes.")]
    [InlineEditor(Expanded = true)]
    public DisabledInInlineEditorScriptableObject InlineObject;
#endif

#if UNITY_EDITOR // Editor-related code must be excluded from builds
    [OnInspectorInit]
    private void CreateData() {
        InlineObject = ExampleHelper.GetScriptableObject<DisabledInInlineEditorScriptableObject>("Inline Object");
    }

    [OnInspectorDispose]
    private void CleanupData() {
        if (InlineObject != null) Object.DestroyImmediate(InlineObject);
    }
#endif
}

2.5 DisableInPlayMode / HideInPlayMode

在处于播放模式时禁用 / 隐藏对象。只希望在编辑模式下可编辑 / 显示时,可使用此选项。

image-20240715235402700
// DisableInPlayModeExamplesComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

public class DisableInPlayModeExamplesComponent : MonoBehaviour
{
    [Title("Disabled in play mode")]
    [DisableInPlayMode]
    public int A;

    [DisableInPlayMode]
    public Material B;
}

2.6 ShowIf / HideIf

如果解析的字符串计算结果为指定值,则在 Inspector 窗口中显示 / 隐藏该对象。

  • string condition

    检查值条件的解析字符串,如成员名称、表达式等。

  • object optionalValue

    要检查的值。

  • bool animate = true

    状态改变时是否启用滑入和滑出动画显示。

image-20240715235942366
// ShowAndHideIfExamplesComponent.cs
using Sirenix.OdinInspector;
using UnityEngine;

public class ShowAndHideIfExamplesComponent : MonoBehaviour
{
    // Note that you can also reference methods and properties or use @expressions. You are not limited to fields.
    public UnityEngine.Object SomeObject;
    
    [EnumToggleButtons]
    public InfoMessageType SomeEnum;
    
    public bool IsToggled;
    
    [ShowIf("SomeEnum", InfoMessageType.Info)]
    public Vector3 Info;
    
    [ShowIf("SomeEnum", InfoMessageType.Warning)]
    public Vector2 Warning;
    
    [ShowIf("SomeEnum", InfoMessageType.Error)]
    public Vector3 Error;
    
    [ShowIf("IsToggled")]
    public Vector2 VisibleWhenToggled;
    
    [HideIf("IsToggled")]
    public Vector3 HiddenWhenToggled;
    
    [HideIf("SomeObject")]
    public Vector3 ShowWhenNull;
    
    [ShowIf("SomeObject")]
    public Vector3 HideWhenNull;
    
    [EnableIf("@this.IsToggled && this.SomeObject != null || this.SomeEnum == InfoMessageType.Error")]
    public int ShowWithExpression;
}

2.7 ShowIfGroup / HideIfGroup

满足某个条件,则显示 / 隐藏指定组。

  • string path

    指定组的路径。

  • object value

    与路径末尾的标签重名的属性等于该值时,则满足条件。

  • bool animate = true

    状态改变时是否启用滑入和滑出动画显示。

image-20240716000503730
// HideIfGroupExampleComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

public class HideIfGroupExampleComponent : MonoBehaviour
{
    public bool Toggle = true;

    [HideIfGroup("Toggle")]
    [BoxGroup("Toggle/Shown Box")]
    public int A, B;

    [BoxGroup("Box")]
    public InfoMessageType EnumField = InfoMessageType.Info;

    [BoxGroup("Box")]
    [HideIfGroup("Box/Toggle")]
    public Vector3 X, Y;

    // Like the regular If-attributes, HideIfGroup also supports specifying values.
    // You can also chain multiple HideIfGroup attributes together for more complex behaviour.
    [HideIfGroup("Box/Toggle/EnumField", Value = InfoMessageType.Info)]
    [BoxGroup("Box/Toggle/EnumField/Border", ShowLabel = false)]
    public string Name;

    [BoxGroup("Box/Toggle/EnumField/Border")]
    public Vector3 Vector;

    // HideIfGroup will by default use the name of the group,
    // but you can also use the MemberName property to override this.
    [HideIfGroup("RectGroup", Condition = "Toggle")]
    public Rect Rect;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1934061.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

docker安装mysql突然无法远程连接

docker安装mysql突然莫名其妙的无法远程连接 docker安装mysql突然无法远程访问问题背景发现问题排查问题解决问题总结 docker安装mysql突然无法远程访问 问题背景 大概一年前在服务器中通过docker安装mysql5.7端口映射关系是3308->3306 前期在服务器上开方了3308端口 fir…

Python用Pyqt5制作音乐播放器

具体效果如下 需要实现的功能主要的几个有&#xff1a; 1、搜索结果更新至当前音乐的列表&#xff0c;这样播放下一首是搜素结果的下一首 2、自动播放 3、滚动音乐文本 4、音乐进度条 5、根据实际情况生成音乐列表。我这里的是下面的情况&#xff0c;音乐文件的格式是 歌…

图——图的遍历(DFS与BFS算法详解)

前面的文章中我们学习了图的基本概念和存储结构&#xff0c;大家可以通过下面的链接学习&#xff1a; 图的定义和基本术语 图的类型定义和存储结构 这篇文章就来学习一下图的重要章节——图的遍历。 目录 一&#xff0c;图的遍历定义&#xff1a; 二&#xff0c;深度优先…

【MySQL】:学习数据库必须要知道的背景知识

客户端—服务器 客户端是一个“客户端—服务器”结构的程序 C&#xff08;client&#xff09;—S&#xff08;server&#xff09; 客户端和服务器是两个独立的程序&#xff0c;这两个程序之间通过“网络”进行通信&#xff08;相当于是两种角色&#xff09; 客户端 主动发起网…

CV12_ONNX转RKNN模型(谛听盒子)

暂时简单整理一下&#xff1a; 1.在边缘设备上配置相关环境。 2.配置完成后&#xff0c;获取模型中间的输入输出结果&#xff0c;保存为npy格式。 3.将onnx格式的模型&#xff0c;以及中间输入输出文件传送到边缘设备上。 4.编写一个python文件用于转换模型格式&#xff0c…

对某根域的一次渗透测试

前言 两个月之前的一个渗透测试项目是基于某网站根域进行渗透测试&#xff0c;发现该项目其实挺好搞的&#xff0c;就纯粹的没有任何防御措施与安全意识所以该项目完成的挺快&#xff0c;但是并没有完成的很好&#xff0c;因为有好几处文件上传没有绕过&#xff08;虽然从一个…

linux|多线程(一)

主要介绍了为什么要有线程 和线程的调用 和简单的对线程进行封装。 背景知识 a.重谈地址空间 我们知道物理内存的最小单元大小是4kB 物理内存是4G那么这样的单元友1M个 操作系统先描述再组织struct page[1M] 对于32位数据字长的机器&#xff0c;页表有2^32条也就是4G条&#…

springboot的JWT令牌

生成JWT令牌 依赖 <!--jwt令牌--> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>javax.xml.bind<…

怎样在 PostgreSQL 中优化对大数据量的分页查询?

&#x1f345;关注博主&#x1f397;️ 带你畅游技术世界&#xff0c;不错过每一次成长机会&#xff01;&#x1f4da;领书&#xff1a;PostgreSQL 入门到精通.pdf 文章目录 《PostgreSQL 中大数据量分页查询的优化之道》一、理解分页查询的基本原理二、优化分页查询的策略&…

2024年06月CCF-GESP编程能力等级认证C++编程七级真题解析

本文收录于专栏《C等级认证CCF-GESP真题解析》&#xff0c;专栏总目录&#xff1a;点这里。订阅后可阅读专栏内所有文章。 一、单选题&#xff08;每题 2 分&#xff0c;共 30 分&#xff09; 第 1 题 下列C代码的输出结果是&#xff08; &#xff09;。 #include <iostr…

SwiftUI 6.0(Xcode 16)新 PreviewModifier 协议让预览调试如虎添翼

概览 用 SwiftUI 框架开发过应用的小伙伴们都知道&#xff0c;SwiftUI 中的视图由各种属性和绑定“扑朔迷离”的缠绕在一起&#xff0c;自成体系。 想要在 Xcode 预览中泰然处之的调试 SwiftUI 视图有时并不是件容易的事。其中&#xff0c;最让人秃头码农们头疼的恐怕就要数如…

Spring Cloud Gateway 自定义断言以及过滤器

1.Spring Cloud gateway介绍 Spring Cloud Gateway 是一个基于 Spring Framework 和 Spring Boot 的 API 网关服务&#xff0c;它利用了 Spring WebFlux 来提供响应式非阻塞式Web请求处理能力。它的核心功能是路由&#xff0c;即根据请求的特定规则将请求转发到后端服务&#…

DP(1500-1700)(刷题)

1.状态机模型&#xff1a;https://codeforces.com/contest/1984/problem/C2 记一下max与min状态转移即可&#xff0c;下面是AC代码&#xff1a; #include<bits/stdc.h> using namespace std; typedef long long ll; ll a[200010],t,n; ll dp[200010][2];//dp[i][0]表示…

啊?现在不懂 AI ,相当于十年前不懂电脑?

最近有关萝卜快跑的新闻铺天盖地&#xff0c;一篇篇都在唱衰&#xff0c;好像千万滴滴师傅立马就要失业了一样。 还没等多久&#xff0c;在朋友圈看到这样一句话&#xff0c;“现在不懂 AI &#xff0c;相当于十年前不懂电脑”。 我想了许久&#xff0c;最终不得不承认这个事实…

深度学习入门——误差反向传播

要正确理解误差反向传播法&#xff0c;我个人认为有两种方法&#xff1a;一种是基于数学式&#xff1b;另一种是基于计算图&#xff08;computational graph&#xff09; 前者是比较常见的方法&#xff0c;机器学习相关的图书中多数都是以数学式为中心展开论述的。因为这种方法…

达梦数据库的系统视图v$sqltext

达梦数据库的系统视图v$sqltext 在达梦数据库&#xff08;DM Database&#xff09;中&#xff0c;V$SQLTEXT 是一个系统视图&#xff0c;用于显示当前正在执行或最近执行的SQL语句的文本信息。这个视图对于监控和分析数据库中的SQL活动非常有用&#xff0c;尤其是在需要调试性…

【python】OpenCV—Coordinates Sorted Clockwise

文章目录 1、需求介绍2、算法实现3、完整代码 1、需求介绍 调用 opencv 库&#xff0c;绘制轮廓的矩形边框&#xff0c;坐标顺序为右下→左下→左上→右上&#xff0c;我们实现一下转化为熟悉的 左上→右上→右下→左下 形式 按照这样的顺序组织边界框坐标是执行透视转换或匹…

13. C++继承 | 详解 | 虚拟继承及底层实现

目录 1.定义 1.1继承的概念 1.2 继承的定义 2. 对象赋值转换 3. 继承中的作用域 a. 隐藏/重定义 (Hiding/Redefinition) b. 重载 (Overloading) c. 重写/覆盖 (Overriding) d. 编译报错 (Compilation Error) 4. 派生类的默认成员函数 构造 拷贝构造 运算符重载 析…

处理uniapp刷新后,点击返回按钮跳转到登录页的问题

在使用uniapp的原生返回的按钮时&#xff0c;如果没有刷新会正常返回到对应的页面&#xff0c;如果刷新后会在当前页反复横跳&#xff0c;或者跳转到登录页。那个时候我第一个想法时&#xff1a;使用浏览器的history.back()方法。因为浏览器刷新后还是可以通过右上角的返回按钮…

Vscode+Pyside6开发之虚拟环境配置以及错误解决

Pyside开发之虚拟环境配置以及错误解决 开发环境一、项目创建以及虚拟环境设置1.创建项目2. 新建py文件,新建虚拟环境3.激活虚拟环境二、项目位置改变pip命令报错1.删除原来的虚拟环境2. 产生包列表文件requirements.txt3.重新创建虚拟环境4.重新安装包文件5.其他错误开发环境…