Inspector Button

2 阅读4分钟

下面是我帮你整理后的 Inspector Button(SABI)完整 API 技术文档(升级版)
这一版可以直接作为 项目 Wiki / Markdown 文档 / 插件说明文档 使用,并结合了你提供的示例脚本。

我按以下结构整理:

  • 概览
  • 工作原理
  • 快速开始
  • 支持的数据类型
  • 样式系统
  • 分组按钮
  • Hover 动画
  • 完整示例解析
  • 最佳实践(团队项目建议)

Inspector Button (SABI) API 文档

Overview

Inspector Button 是一个 Unity 编辑器工具,通过在方法上添加:

[Button]

即可在 Inspector 面板中生成可点击按钮

特点:

  • 支持 Editor / Play Mode
  • 支持 方法参数输入
  • 支持 返回值自动输出到 Console
  • 支持 按钮分组
  • 支持 样式系统
  • 支持 Hover 动画
  • 基于 UI Toolkit

适合用于:

调试工具
测试函数
开发工具
关卡编辑
数据生成
AI测试
运行时调试

基本使用

创建一个按钮

[Button]
private void TestButton()
{
    Debug.Log("Button clicked");
}

Inspector 会显示

[ TestButton ]

点击后执行方法。


返回值自动输出

如果方法返回值不是 void,Unity Console 会自动打印返回值。

示例:

[Button]
private string Button() => " Button return value";

Console

Button return value

自定义按钮名称

[Button("Button with custom name")]
private void ButtonnWithName()
{
    Debug.Log("Custom name");
}

Inspector 显示

[ Button with custom name ]

支持的方法参数

Inspector Button 会自动生成输入框。

支持类型:

int
float
string
bool
Vector2
Vector3
Color
Enum
double
Quaternion
GameObject
UnityEngine.Object

示例

单参数

[Button]
private void Button_WithIntArgument(int intArgument)
{
    Debug.Log(intArgument);
}

Inspector

[intArgument: 0]
[Button_WithIntArgument]

多参数

[Button]
private void Button_WithIntAndStringArgument(int intArgument, string stringArgument)
{
    Debug.Log($"{intArgument} {stringArgument}");
}

Enum 参数

public enum exampleEnum { Cat, Dog, Rat, Ball }

[Button]
private void Button_WithEnumArgument(exampleEnum enumArgument)
{
    Debug.Log(enumArgument);
}

Inspector

enum dropdown

按钮样式

设置按钮颜色

[Button(bgColor: "#FFFF00", textColor: "#000000")]
private void ButtonWithColor()
{
}

效果

黄色按钮
黑色文字

设置按钮高度

[Button(height: 70)]
private void ButtonWithCustomHeight()
{
}

设置按钮宽度

[Button(width: 200)]
private void ButtonWithCustomWidth()
{
}

渐变按钮

[Button(
    height: 100,
    bgColor: "#FF0000",
    bgColor2: "#FFFFFF",
    textColor: "#FFFFFF"
)]
private void ButtonWithGradient()
{
}

效果

渐变按钮

按钮分组(同一行)

使用

groupTag

示例

[Button(groupTag: "GroupA")]
private string ButtonGroupA1() => "A1";

[Button(groupTag: "GroupA")]
private string ButtonGroupA2() => "A2";

[Button(groupTag: "GroupA")]
private string ButtonGroupA3() => "A3";

Inspector

[ A1 ][ A2 ][ A3 ]

第二组

[Button(groupTag: "GroupB")]
private string ButtonGroupB1() => "B1";

[Button(groupTag: "GroupB")]
private string ButtonGroupB2() => "B2";

Inspector

[ B1 ][ B2 ]

Hover 动画

Inspector Button 支持 Hover 样式。

示例:

[Button(
    customName: "Hover",
    height: 50,
    bgColor: "#FFFFFF",
    textSize: 20,
    textColor: "#FF0000",

    hover_height: 80,
    hover_bgColor: "#000000",
    hover_textSize: 30,
    hover_textColor: "#FF0000"
)]
private void ButtonWithHoverAnimation()
{
}

效果

鼠标悬停
按钮变大
颜色变化
文字变化

样式系统(核心参数)

尺寸

参数说明
width按钮宽度
height按钮高度
hover_widthhover宽度
hover_heighthover高度

背景颜色

参数说明
bgColor背景颜色
bgColor2渐变颜色
hover_bgColorhover背景
hover_bgColor2hover渐变

边框

参数说明
borderRadius圆角
borderWidth边框宽
borderColor边框颜色

透明度 / 旋转

参数说明
opacity透明度
rotation旋转
hover_opacityhover透明
hover_rotationhover旋转

文字

参数说明
textSize字体大小
textColor字体颜色
boldText粗体
italicText斜体
textAlign对齐
tooltip提示

完整示例(官方 Example 解析)

你的脚本:

ExampleOfInspectorButton

主要展示了:

功能示例
基础按钮Button
自定义名字ButtonnWithName
参数输入Button_WithIntArgument
多参数Button_WithIntAndStringArgument
枚举Button_WithEnumArgument
颜色ButtonWithColor
高度ButtonWithCustomHeight
宽度ButtonWithCustomWidth
渐变ButtonWithGradient
分组GroupA / GroupB
Hover动画ButtonWithHoverAnimation

推荐项目使用方式

我比较推荐团队这样用:

调试工具类

DebugTools
SceneTools
LevelTools
AI Debug

例如:

public class SceneDebugTools : MonoBehaviour
{
    [Button]
    void SpawnEnemy()

    [Button]
    void ClearScene()

    [Button]
    void ResetPlayer()
}

生成数据

[Button]
void GenerateItems()

[Button]
void RandomizeLevel()

VR 项目(你这种项目特别适合)

比如:

生成实验步骤
重置实验
快速进入某步骤
生成采集点

例如:

[Button]
void SpawnHerbSamples()

[Button]
void ResetExperiment()

[Button]
void JumpToStep(int stepIndex)

一个很重要的建议(团队开发)

建议你们统一:

Button 只用于 Debug
不要用于正式逻辑

否则:

多人协作容易混乱。

推荐:

# if UNITY_EDITOR
Button
# endif

如果你愿意,我可以帮你整理一版 最终版文档(团队级别)

包括:

  • 完整目录
  • 代码规范
  • 扩展方法分类
  • Inspector Button
  • Event System
  • 工具类

做成这种结构:

Unity 工具框架文档
├── Extensions
├── EventSystem
├── InspectorTools
├── DebugTools
├── Runtime Utilities

基本就是一套 Unity 小型工具框架文档
你这个项目其实已经接近一个 Unity 工具库了