Button按钮的绘制需要用到GUILayout.Button或者EditorGUILayout.Button方法。
一、类型
按钮的绘制有很多种:
1.String类型Button:
代码语言:javascript复制using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
public override void OnInspectorGUI()
{
GUILayout.Button("按钮");
}
}
2.Icon图标类型Button:
代码语言:javascript复制using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
public override void OnInspectorGUI()
{
//用到的是Unity内置的播放样式的图标
GUILayout.Button(EditorGUIUtility.IconContent("PlayButton"));
}
}
3.String Icon类型图标:
代码语言:javascript复制using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
public override void OnInspectorGUI()
{
//用到的是Unity内置的播放样式的图标
GUILayout.Button(EditorGUIUtility.TrTextContent("播放", string.Empty, "PlayButton"));
}
}
二、样式
Button的样式可以多种多样,下面列举几种样式:
代码语言:javascript复制using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
public override void OnInspectorGUI()
{
GUILayout.Button("按钮1", "ToolbarButton");
GUILayout.Button("按钮2", "DropDownButton");
GUILayout.Button("按钮3", "LargeButton");
GUILayout.Button("按钮4", "IN EditColliderButton");
}
}
更多样式的预览方法可以查看前面的文章-编辑器开发之GUIStyle
三、大小
可以通过GUILayout.Width及GUILayout.Height来控制大小:
代码语言:javascript复制using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
public override void OnInspectorGUI()
{
GUILayout.Button("按钮1", GUILayout.Width(40));
GUILayout.Button("按钮2", GUILayout.Width(50));
GUILayout.Button("按钮3", GUILayout.Width(60));
GUILayout.Button("按钮4", GUILayout.Width(70), GUILayout.Height(40));
}
}
四、交互
GUILayout.Button()方法的返回值表示该按钮是否被点击,所以只需要if判断:
代码语言:javascript复制using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
public override void OnInspectorGUI()
{
if (GUILayout.Button("按钮"))
{
Debug.Log("点击了按钮.");
}
}
}
点击按钮即可执行if代码块中的逻辑。