2024-07-15 Unity插件 Odin Inspector3 —— Button Attributes

news2024/9/22 19:17:23

文章目录

  • 1 说明
  • 2 Button 特性
    • 2.1 Button
    • 2.2 ButtonGroup
    • 2.3 EnumPaging
    • 2.4 EnumToggleButtons
    • 2.5 InlineButton
    • 2.6 ResponsiveButtonGroup

1 说明

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

2 Button 特性

2.1 Button

依据方法,在 Inspector 窗口上生成可点击的按钮。点击一次,方法执行一次。

  1. 无参方法

    • string name

      按钮名称,默认为方法名。

    • ButtonSizes buttonSize

      按钮大小(枚举)。

    • ButtonStyle parameterBtnStyle

      按钮样式。

    • int buttonSize

      按钮自定义大小。

    • SdfIconType icon

      按钮图标。

    • IconAlignment iconAlignment

      按钮图标对齐方式。

image-20240715003113518
// ButtonExamplesComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

public class ButtonExamplesComponent : MonoBehaviour
{
    public string ButtonName = "Dynamic button name";

    public bool Toggle;

    [Button("$ButtonName")]
    private void DefaultSizedButton() {
        this.Toggle = !this.Toggle;
    }

    [Button("@\"Expression label: \" + DateTime.Now.ToString(\"HH:mm:ss\")")]
    public void ExpressionLabel() {
        this.Toggle = !this.Toggle;
    }

    [Button("Name of button")]
    private void NamedButton() {
        this.Toggle = !this.Toggle;
    }

    [Button(ButtonSizes.Small)]
    private void SmallButton() {
        this.Toggle = !this.Toggle;
    }

    [Button(ButtonSizes.Medium)]
    private void MediumSizedButton() {
        this.Toggle = !this.Toggle;
    }

    [DisableIf("Toggle")]
    [HorizontalGroup("Split", 0.5f)]
    [Button(ButtonSizes.Large), GUIColor(0.4f, 0.8f, 1)]
    private void FanzyButton1() {
        this.Toggle = !this.Toggle;
    }

    [HideIf("Toggle")]
    [VerticalGroup("Split/right")]
    [Button(ButtonSizes.Large), GUIColor(0, 1, 0)]
    private void FanzyButton2() {
        this.Toggle = !this.Toggle;
    }

    [ShowIf("Toggle")]
    [VerticalGroup("Split/right")]
    [Button(ButtonSizes.Large), GUIColor(1, 0.2f, 0)]
    private void FanzyButton3() {
        this.Toggle = !this.Toggle;
    }

    [Button(ButtonSizes.Gigantic)]
    private void GiganticButton() {
        this.Toggle = !this.Toggle;
    }

    [Button(90)]
    private void CustomSizedButton() {
        this.Toggle = !this.Toggle;
    }

    [Button(Icon = SdfIconType.Dice1Fill, IconAlignment = IconAlignment.LeftOfText)]
    private void IconButton01() {
        this.Toggle = !this.Toggle;
    }

    [Button(Icon = SdfIconType.Dice2Fill, IconAlignment = IconAlignment.LeftOfText)]
    private void IconButton02() {
        this.Toggle = !this.Toggle;
    }
}
  1. 有参方法

    • bool Expanded = false

      如果按钮包含参数,可通过将其设置为 true 来禁用折叠显示。

image-20240715003550010
// ButtonWithParametersExamplesComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

public class ButtonWithParametersExamplesComponent : MonoBehaviour
{
    [Button]
    private void Default(float a, float b, GameObject c) { }

    [Button]
    private void Default(float t, float b, float[] c) { }

    [Button(ButtonSizes.Medium, ButtonStyle.FoldoutButton)]
    private int FoldoutButton(int a = 2, int b = 2) {
        return a + b;
    }

    [Button(ButtonSizes.Medium, ButtonStyle.FoldoutButton)]
    private void FoldoutButton(int a, int b, ref int result) {
        result = a + b;
    }

    [Button(ButtonStyle.Box)]
    private void Full(float a, float b, out float c) {
        c = a + b;
    }

    [Button(ButtonSizes.Large, ButtonStyle.Box)]
    private void Full(int a, float b, out float c) {
        c = a + b;
    }

    [Button(ButtonStyle.CompactBox, Expanded = true)]
    private void CompactExpanded(float a, float b, GameObject c) { }

    [Button(ButtonSizes.Medium, ButtonStyle.Box, Expanded = true)]
    private void FullExpanded(float a, float b) { }
}

2.2 ButtonGroup

将 Button 分组显示。

  • string group = "_DefaultGroup"

    组名。

  • float order = 0.0f

    顺序。

  • int ButtonHeight

    按钮组中所有按钮的高度。

image-20240715003708316
// ButtonGroupExamplesComponent.cs

using System;
using Sirenix.OdinInspector;
using UnityEngine;

public class ButtonGroupExamplesComponent : MonoBehaviour
{
    public IconButtonGroupExamples iconButtonGroupExamples;

    [ButtonGroup]
    private void A() { }

    [ButtonGroup]
    private void B() { }

    [ButtonGroup]
    private void C() { }

    [ButtonGroup]
    private void D() { }

    [Button(ButtonSizes.Large)]
    [ButtonGroup("My Button Group")]
    private void E() { }

    [GUIColor(0, 1, 0)]
    [ButtonGroup("My Button Group")]
    private void F() { }

    [Serializable, HideLabel]
    public struct IconButtonGroupExamples
    {
        [ButtonGroup(ButtonHeight = 25), Button(SdfIconType.ArrowsMove, "")]
        void ArrowsMove() { }

        [ButtonGroup, Button(SdfIconType.Crop, "")]
        void Crop() { }

        [ButtonGroup, Button(SdfIconType.TextLeft, "")]
        void TextLeft() { }

        [ButtonGroup, Button(SdfIconType.TextRight, "")]
        void TextRight() { }

        [ButtonGroup, Button(SdfIconType.TextParagraph, "")]
        void TextParagraph() { }

        [ButtonGroup, Button(SdfIconType.Textarea, "")]
        void Textarea() { }
    }
}

​ 可以与 TitleGroup 组合使用。

image-20240715003758748
// BigTitleGroupExampleComponent.cs
using Sirenix.OdinInspector;
using UnityEngine;

public class BigTitleGroupExampleComponent : MonoBehaviour
{
    [BoxGroup("Titles", ShowLabel = false)]
    [TitleGroup("Titles/First Title")]
    public int A;
    
    [BoxGroup("Titles/Boxed")]
    [TitleGroup("Titles/Boxed/Second Title")]
    public int B;
    
    [TitleGroup("Titles/Boxed/Second Title")]
    public int C;
    
    [TitleGroup("Titles/Horizontal Buttons")]
    [ButtonGroup("Titles/Horizontal Buttons/Buttons")]
    public void FirstButton() { }
    
    [ButtonGroup("Titles/Horizontal Buttons/Buttons")]
    public void SecondButton() { }
}

2.3 EnumPaging

为枚举添加“下一步”和“上一步”按钮选择器,循环查看枚举属性的可用值。

image-20240715003928053
// EnumPagingExamplesComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

public class EnumPagingExamplesComponent : MonoBehaviour
{
    [EnumPaging]
    public SomeEnum SomeEnumField;

    public enum SomeEnum
    {
        A, B, C
    }

#if UNITY_EDITOR // UnityEditor.Tool is an editor-only type, so this example will not work in a build
    [EnumPaging, OnValueChanged("SetCurrentTool")]
    [InfoBox("Changing this property will change the current selected tool in the Unity editor.")]
    public UnityEditor.Tool sceneTool;

    private void SetCurrentTool() {
        UnityEditor.Tools.current = this.sceneTool;
    }
#endif
}

2.4 EnumToggleButtons

在水平按钮组中绘制枚举,而不是下拉列表。

image-20240715004031983
// EnumToggleButtonsExamplesComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

public class EnumToggleButtonsExamplesComponent : MonoBehaviour
{
    [Title("Default")]
    public SomeBitmaskEnum DefaultEnumBitmask;

    [Title("Standard Enum")]
    [EnumToggleButtons]
    public SomeEnum SomeEnumField; // 单选枚举

    [EnumToggleButtons, HideLabel]
    public SomeEnum WideEnumField; // 单选枚举

    [Title("Bitmask Enum")]
    [EnumToggleButtons]
    public SomeBitmaskEnum BitmaskEnumField; // 多选枚举

    [EnumToggleButtons, HideLabel]
    public SomeBitmaskEnum EnumFieldWide; // 多选枚举

    [Title("Icon Enum")]
    [EnumToggleButtons, HideLabel]
    public SomeEnumWithIcons EnumWithIcons;

    [EnumToggleButtons, HideLabel]
    public SomeEnumWithIconsAndNames EnumWithIconsAndNames;

    public enum SomeEnum
    {
        First, Second, Third, Fourth, AndSoOn
    }

    public enum SomeEnumWithIcons
    {
        [LabelText(SdfIconType.TextLeft)]   TextLeft,
        [LabelText(SdfIconType.TextCenter)] TextCenter,
        [LabelText(SdfIconType.TextRight)]  TextRight,
    }

    public enum SomeEnumWithIconsAndNames
    {
        [LabelText("Align Left", SdfIconType.TextLeft)]
        TextLeft,

        [LabelText("Align Center", SdfIconType.TextCenter)]
        TextCenter,

        [LabelText("Align Right", SdfIconType.TextRight)]
        TextRight,
    }

    [System.Flags]
    public enum SomeBitmaskEnum
    {
        A   = 1 << 1,
        B   = 1 << 2,
        C   = 1 << 3,
        All = A | B | C
    }
}

2.5 InlineButton

在属性右侧绘制按钮。

  • string action

    点击按钮时执行的方法。

  • SdfIconType icon

    按钮图标。

  • string label = null

  • 按钮显示名称。

image-20240715004118860
// InlineButtonExamplesComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

public class InlineButtonExamplesComponent : MonoBehaviour
{
    // Inline Buttons:
    [InlineButton("A")]
    public int InlineButton;

    [InlineButton("A")]
    [InlineButton("B", "Custom Button Name")]
    public int ChainedButtons;

    [InlineButton("C", SdfIconType.Dice6Fill, "Random")]
    public int IconButton;

    private void A() {
        Debug.Log("A");
    }

    private void B() {
        Debug.Log("B");
    }

    private void C() {
        Debug.Log("C");
    }
}

2.6 ResponsiveButtonGroup

将按钮绘制在组内,该组将响应其可用的水平空间。

  • string group = "_DefaultResponsiveButtonGroup"

    组名。

image-20240715004200320
// ResponsiveButtonGroupExampleComponent.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 ResponsiveButtonGroupExampleComponent : MonoBehaviour
{
#if UNITY_EDITOR // Editor-related code must be excluded from builds
    [Button(ButtonSizes.Large), GUIColor(0, 1, 0)]
    private void OpenDockableWindowExample()
    {
        var window = UnityEditor.EditorWindow.GetWindow<MyDockableGameDashboard>();
        window.WindowPadding = new Vector4();
    }
#endif
    
    [OnInspectorGUI] private void Space1() { GUILayout.Space(20); }
    
    [ResponsiveButtonGroup] public void Foo() { }
    [ResponsiveButtonGroup] public void Bar() { }
    [ResponsiveButtonGroup] public void Baz() { }
    
    [OnInspectorGUI] private void Space2() { GUILayout.Space(20); }
    
    [ResponsiveButtonGroup("UniformGroup", UniformLayout = true)] public void Foo1() { }
    [ResponsiveButtonGroup("UniformGroup")] public void Foo2() { }
    [ResponsiveButtonGroup("UniformGroup")] public void LongesNameWins() { }
    [ResponsiveButtonGroup("UniformGroup")] public void Foo4() { }
    [ResponsiveButtonGroup("UniformGroup")] public void Foo5() { }
    [ResponsiveButtonGroup("UniformGroup")] public void Foo6() { }
    
    [OnInspectorGUI] private void Space3() { GUILayout.Space(20); }
    
    [ResponsiveButtonGroup("DefaultButtonSize", DefaultButtonSize = ButtonSizes.Small)] public void Bar1() { }
    [ResponsiveButtonGroup("DefaultButtonSize")] public void Bar2() { }
    [ResponsiveButtonGroup("DefaultButtonSize")] public void Bar3() { }
    [Button(ButtonSizes.Large), ResponsiveButtonGroup("DefaultButtonSize")] public void Bar4() { }
    [Button(ButtonSizes.Large), ResponsiveButtonGroup("DefaultButtonSize")] public void Bar5() { }
    [ResponsiveButtonGroup("DefaultButtonSize")] public void Bar6() { }
    
    [OnInspectorGUI] private void Space4() { GUILayout.Space(20); }
    
    [FoldoutGroup("SomeOtherGroup")]
    [ResponsiveButtonGroup("SomeOtherGroup/SomeBtnGroup")] public void Baz1() { }
    [ResponsiveButtonGroup("SomeOtherGroup/SomeBtnGroup")] public void Baz2() { }
    [ResponsiveButtonGroup("SomeOtherGroup/SomeBtnGroup")] public void Baz3() { }
}

​ 与 TabGroup 结合使用:

image-20240715004248291
// BigTabGroupExampleComponent.cs

using Sirenix.OdinInspector;
using UnityEngine;

public class BigTabGroupExampleComponent : MonoBehaviour
{
    [TitleGroup("Tabs")]
    [HorizontalGroup("Tabs/Split", Width = 0.5f)]
    [TabGroup("Tabs/Split/Parameters", "A")]
    public string NameA, NameB, NameC;

    [TabGroup("Tabs/Split/Parameters", "B")]
    public int ValueA, ValueB, ValueC;

    [TabGroup("Tabs/Split/Buttons", "Responsive")]
    [ResponsiveButtonGroup("Tabs/Split/Buttons/Responsive/ResponsiveButtons")]
    public void Hello() { }

    [ResponsiveButtonGroup("Tabs/Split/Buttons/Responsive/ResponsiveButtons")]
    public void World() { }

    [ResponsiveButtonGroup("Tabs/Split/Buttons/Responsive/ResponsiveButtons")]
    public void And() { }

    [ResponsiveButtonGroup("Tabs/Split/Buttons/Responsive/ResponsiveButtons")]
    public void Such() { }

    [Button]
    [TabGroup("Tabs/Split/Buttons", "More Tabs")]
    [TabGroup("Tabs/Split/Buttons/More Tabs/SubTabGroup", "A")]
    public void SubButtonA() { }

    [Button]
    [TabGroup("Tabs/Split/Buttons/More Tabs/SubTabGroup", "A")]
    public void SubButtonB() { }

    [Button(ButtonSizes.Gigantic)]
    [TabGroup("Tabs/Split/Buttons/More Tabs/SubTabGroup", "B")]
    public void SubButtonC() { }
}

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

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

相关文章

使用DHCP动态管理主机地址

前言&#xff1a;本博客仅作记录学习使用&#xff0c;部分图片出自网络&#xff0c;如有侵犯您的权益&#xff0c;请联系删除 一、动态主机地址管理协议 动态主机配置协议&#xff08;DHCP&#xff09;是一种基于UDP协议且仅限于在局域网内部使用的网络协议&#xff0c;主要用…

书生大模型第一关-Linux基础知识

1.闯关任务&#xff1a;关任务 完成SSH连接与端口映射并运行hello_world.py 首先SSH 命令连接进行转发 ssh -p 33704 rootssh.intern-ai.org.cn -CNg -L {本地机器_PORT}:127.0.0.1:{开发机_PORT} -o StrictHostKeyCheckingno在远程服务器上&#xff08;开发机&#xff09;运…

操作系统杂项(二)

目录 一、简述GDB常见的调试命令&#xff0c;什么是条件断点&#xff0c;多进程下如何调试 1、GDB调试 2、命令格式 3、条件断点 4、多进程下如何调试 二、简述什么是“大端小端”及如何判断 1、小端模式 2、大端模式 3、如何判断 三、简述进程调度算法有哪些 1、分…

iSCSI 网络存储服务部署

一、介绍 iSCSI &#xff08;Internet Small Computer System Interface&#xff09;&#xff0c;互联网小型计算机系统接口&#xff1b;iSCSI 是SCSI接口 与以太网技术相结合的新型存储技术&#xff0c;属于ip san的一种&#xff0c;可以用来在网络中传输 SCSI 接口的命令和数…

用户进入网站之后看一眼就跳走,你知道原因吗?

用户进入网站后立即离开的原因可能有很多&#xff0c;以下是一些可能的原因和相应的规避办法&#xff1a; 页面加载速度慢&#xff1a; 如果网站加载速度过慢&#xff0c;用户可能会感到不耐烦并离开。可以通过优化网站的代码和资源&#xff0c;使用浏览器缓存、CDN加速等技术…

Docker缩小镜像体积与搭建LNMP架构

镜像加速地址 {"registry-mirrors": ["https://docker.m.daocloud.io","https://docker.1panel.live"] } daemon.json 配置文件里面 bip 配置项中可以配置docker 的网段 {"graph": "/data/docker", #数据目录&#xff0…

QTreeWidget

一、基本要点 1、QTreeWidgetItem 是 Qt 开发框架中的一个重要组件&#xff0c;它主要用于表示树形视图中的节点。在 QTreeWidget 这样的控件中&#xff0c;每个节点都是 QTreeWidgetItem 的实例&#xff0c;可以包含文本、图标以及其他数据。 2、connect 是Qt中的一个重要函…

Crackmapexec一键检测网络环境(KALI工具系列四十四)

目录 1、KALI LINUX 简介 2、Crackmapexec工具介绍 3、信息收集 3.1 目标IP 3.2 kali的IP 4、操作步骤 4.1 帮助命令 4.2 扫描网段 4.3 验证访问 5、总结 1、KALI LINUX 简介 Kali Linux 是一个功能强大、多才多艺的 Linux 发行版 &#xff0c;广泛用于网络安全社区。它…

蓝卓油气行业解决方案

我国是全球最大的能源消费国&#xff0c;保障国家能源安全是我国能源发展的首要任务&#xff0c;油气作为我国能源体系的重要组成部分&#xff0c;是支撑我国工业和经济社会发展的基础和“压舱石&#xff0c;也是必须筑牢的能源安全底线。 蓝卓根据油气田行业发展趋势&#xf…

Linux中六种常见工具

一、软件包管理器yum 1、yum概念 yum是一个软件下载安装管理的客户端&#xff0c;例如手机上的小米一应用商城。 那为什么我们推荐在Linux中用yum下载软件呢&#xff1f; 其实软件的安装有三种方式&#xff1a;源代码安装&#xff0c;rpm包安装&#xff0c;yum安装。 a、源…

STM32 HRTIM生成PWM时遇到无法输出PWM脉冲波形问题

在使用HRTIM生成PWM时&#xff0c;当把周期寄存器更新的设置放到while循环中时&#xff0c;无法输出PWM脉冲波形&#xff0c;即使增加计数延时也无法输出&#xff0c;最终只能放到中断函数中执行后期寄存器值更新才能够生成PWM脉冲波形。

【XSS】

文章目录 0x01 简介0x02 XSS Payload用法XSS攻击平台及调试JavaScript 0x03 XSS构造技巧XSS漏洞防御策略 跨站脚本攻击&#xff0c;Cross Site Script。&#xff08;重点在于脚本script&#xff09; 分类 反射型、存储型DOM型 漏洞原理&#xff1a;通过插入script篡改“HTML”…

单片机设计_自行车码表(AT89C51, LCD1602, DS1302,霍尔传感器)

想要更多项目私wo!!! 一、电路设计 系统采用51单片机LCD1602液晶DS1302时钟模块霍尔传感器电机按键模块蜂鸣器报警模块设计而成。 产品自带单片机上电复位电路、手动复位电路&#xff08;复位按键&#xff09;、晶振电路&#xff08;给单片机提供时钟周期&#xff09;。 …

下载安装VSCode并添加插件作为仓颉编程入门编辑器

VSCode下载地址&#xff1a;下载 Visual Studio Code - Mac、Linux、Windows 插件下载&#xff1a;GitCode - 全球开发者的开源社区,开源代码托管平台 仓颉社区中下载解压 cangjie.vsix 插件 打开VSCode 按 Ctrl Shift X 弹出下图 按照上图步骤依次点击选中我们下…

网络编程+文件上传操作的理解

前言&#xff1a; 概述:在网络通信协议下,不同计算机上运行的程序,进行数据传输 比如:通信,视频通话,网游,邮件等 只要是计算机之间通过网络进行数据传输,就有网络编程的存在 &#xff08;下面单纯是在Java基础中了解了一下网络编程&#xff0c;感觉理…

深度学习 | CNN 基本原理

目录 1 什么是 CNN2 输入层3 卷积层3.1 卷积操作3.2 Padding 零填充3.3 处理彩色图像 4 池化层4.1 池化操作4.2 池化的平移不变性 5 全连接层6 输出层 前言 这篇博客不够详细&#xff0c;因为没有介绍卷积操作的具体计算&#xff1b;但是它介绍了 CNN 各层次的功能…

08 模型演化根本 深度学习推荐算法的五大范式

易经》“九三&#xff1a;君于终日乾乾&#xff1b;夕惕若&#xff0c;厉无咎”。九三是指阳爻在卦中处于第三位&#xff0c;已经到达中位&#xff0c;惕龙指这个阶段逐渐理性&#xff0c;德才已经显现&#xff0c;会引人注目&#xff1b;但要反思自己的不足&#xff0c;努力不…

昇思25天学习打卡营|MQ(mindquantum)编程实践

学AI还能赢奖品&#xff1f;每天30分钟&#xff0c;25天打通AI任督二脉 (qq.com) 安装 Packages # 安装 mindquantum, networkx !pip install mindquantum -i https://pypi.mirrors.ustc.edu.cn/simple !pip install networkx -i https://pypi.mirrors.ustc.edu.cn/simple 安装…

脸书登录指南:如何在同一台设备登录多个Facebook账号?

海外社媒营销人员和跨境卖家现在越来越依赖社交媒体平台来拓展业务和接触潜在客户&#xff0c;尤其是Facebook。然而&#xff0c;在进行脸书登录时&#xff0c;你可能会问&#xff1a;如何在同一台设备上登录多个Facebook账号&#xff0c;而不违反Facebook的使用条款&#xff1…

指针与数组笔试题解析

文章目录 1.一维数组1.1 整型数组1.2 字符数组 2. 二维数组3.指针笔试题3.1 练习13.2 练习23.3 练习3 数组名的意义&#xff1a; 1.sizeof(数组名)&#xff0c;这里的数组表示整个数组&#xff0c;计算的整个数组的大小 2.&数组名&#xff0c;这里的数组名表示整个数组的&a…