当前位置: 首页 > news >正文

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

文章目录

  • 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.csusing 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.csusing 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.csusing 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.csusing 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.csusing 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.csusing 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;
#endifpublic 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.csusing 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.lryc.cn/news/400869.html

相关文章:

  • 根据脚手架archetype快速构建spring boot/cloud项目
  • 安灯系统在电力设备制造业中的应用效果
  • 代码随想录打卡第二十五天
  • openharmony上传图片,并获取返回路径
  • git常用命令及git分支
  • c# 依赖注入-服务的生命周期
  • 一站式短视频矩阵开发,高效托管!
  • 实践致知第16享:设置Word中某一页横着的效果及操作
  • Leetcode—3011. 判断一个数组是否可以变为有序【中等】(__builtin_popcount()、ranges::is_sorted())
  • 盲盒一番赏小程序:开启惊喜之旅,探索无限创意!
  • Linux基础知识之Linux文件系统权限
  • Qt qml详细介绍
  • 深度解析:如何优雅地删除GitHub仓库中的特定commit历史
  • JS之短路操作符
  • 【Linux】安装PHP扩展-redis
  • 内衣洗衣机怎么选?分享五款人气巅峰机型,选对不选贵
  • OpenMesh入门,安装,运行示例Hello World
  • std::env是什么库?|Python一对一教学答疑
  • Go语言--广播式并发聊天服务器
  • Spring MVC 全注解开发
  • MQTT——Mosquitto使用(Linux订阅者+Win发布者)
  • ArcGIS识别不GDB文件地理数据库显示为空?
  • uniapp微信小程序 TypeError: $refs[ref].push is not a function
  • Django任务管理
  • Hive 常见问题
  • 51单片机(STC8H8K64U/STC8051U34K64)_RA8889驱动大屏_硬件SPI4_参考代码(v1.3)
  • 实习随笔【前端技术实现全局添加水印】
  • 【软件测试】编写测试用例篇
  • 转型AI产品经理需要掌握的硬知识(二):AI常见概念和算法梳理
  • mysql-connector-java 8.0.33 反序列化漏洞