📂 Unity 开发技能 | 目录索引
📂 Unity 常用插件 | 总目录
📂 Unity 开发资源 | 目录索引
📂 Unity 源码工程 | 总目录
Odin Attributes 为Unity开发者提供了更多的自定义选项,使得开发过程更加高效和愉悦。通过使用这些特性,开发者可以创建更加专业和用户友好的编辑器界面,从而提升整个开发团队的工作效率。
Button 特性:用于修饰函数方法,在 Inspector 中绘制一个调用该方法的按钮。
使用方法
1.【Button】在 Inspector 上绘制按钮,如果含有参数,默认以折页的方式展示
[Button]
private void Default()
{
}
[Button]
private void Default(float a, float b, GameObject c)
{
}
[Button]
private void Default(float t, float b, float[] c)
{
}
2. 为这个button指定一个名称,而且也可以通过特殊字符$引用一个字段作为名称
public string ButtonName = "Dynamic button name";
public bool Toggle;
[Button("$ButtonName")]
private void DefaultSizedButton()
{
this.Toggle = !this.Toggle;
}
3. 可以通过特殊字符@ 写入方法体调用
[Button("@\"Expression label: \" + DateTime.Now.ToString(\"HH:mm:ss\")")]
public void ExpressionLabel()
{
this.Toggle = !this.Toggle;
}
4. 【ButtonSizes】指定不同尺寸的按钮
[Button(ButtonSizes.Small), GUIColor(0.3f, 0.8f, 1)]
private void SmallButton()
{
this.Toggle = !this.Toggle;
}
[Button(ButtonSizes.Medium), GUIColor(0.4f, 0.4f, 1)]
private void MediumSizedButton()
{
this.Toggle = !this.Toggle;
}
[Button(ButtonSizes.Large), GUIColor(0.5f, 0.8f, 0.5f)]
private void LargeButton()
{
this.Toggle = !this.Toggle;
}
[Button(ButtonSizes.Gigantic),GUIColor(0.6f, 0.8f, 0)]
private void GiganticButton()
{
this.Toggle = !this.Toggle;
}
5. 自定义尺寸
[Button(90)]
private void CustomSizedButton()
{
this.Toggle = !this.Toggle;
}
6. 【ButtonStyle】指定展示样式
[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(ButtonSizes.Large, ButtonStyle.Box)]
private void Box(float a, float b, out float c)
{
c = a + b;
}
[Button(ButtonSizes.Large, ButtonStyle.Box)]
private void Box(int a, float b, out float c)
{
c = a + b;
}
[Button(ButtonSizes.Large, ButtonStyle.CompactBox)]
public void CompactBox(int a, float b, out float c)
{
c = a + b;
}
7. 【Expanded】如果有参数的控制是否强制展开折页(去除折页方式)
[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)
{
}
完整示例代码
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonAttributeExample : MonoBehaviour
{
[Button]
private void Default()
{
}
[Button]
private void Default(float a, float b, GameObject c)
{
}
[Button]
private void Default(float t, float b, float[] c)
{
}
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), GUIColor(0.3f, 0.8f, 1)]
private void SmallButton()
{
this.Toggle = !this.Toggle;
}
[Button(ButtonSizes.Medium), GUIColor(0.4f, 0.4f, 1)]
private void MediumSizedButton()
{
this.Toggle = !this.Toggle;
}
[Button(ButtonSizes.Large), GUIColor(0.5f, 0.8f, 0.5f)]
private void LargeButton()
{
this.Toggle = !this.Toggle;
}
[Button(ButtonSizes.Gigantic),GUIColor(0.6f, 0.8f, 0)]
private void GiganticButton()
{
this.Toggle = !this.Toggle;
}
[Button(90)]
private void CustomSizedButton()
{
this.Toggle = !this.Toggle;
}
[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(ButtonSizes.Large, ButtonStyle.Box)]
private void Box(float a, float b, out float c)
{
c = a + b;
}
[Button(ButtonSizes.Large, ButtonStyle.Box)]
private void Box(int a, float b, out float c)
{
c = a + b;
}
[Button(ButtonSizes.Large, ButtonStyle.CompactBox)]
public void CompactBox(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)
{
}
}
🍉🍉🍉 如果觉得这篇文对你有帮助的话,请点个赞👍、收藏⭐️下吧,非常感谢! 💕💕💕
🥷🏻博主简介:20年软件开发经验,经历嵌入式驱动开发、Android开发、Unity游戏开发。积累收藏了大量游戏开发资源和素材,如有需要请联系我。
互3互推也请联系我…