一、继承MonoBehaviour
1、[Header(" ")] 标题
//Add a header above some fields in the Inspector.
[Header("Tittle")]
public string Str;
2、[Tooltip(" ")] 如果鼠标光标是在字段上,显示的说明文本
//Specify a tooltip for a field.
[Tooltip("This is a Tooltip")]
public string Str;
3、[Range(0, 5)] 将int、float变量限制在特定范围内,滑动条
//Attribute used to make a float or int variable in a script be restricted to a
//specific range.
[Range(0, 5)]
public int Int;
[Range(0, 5)]
public float Float;
4、[Multiline] 字符串多行文本显示
//Attribute used to make a string value be shown in a multiline textarea.
[Multiline]
public string Str;
5、[TextArea(2, 5)] 字符串多行文本显示,可以设置可现实的最大值和最小值的行数
//Attribute to make a string be edited with a height-flexible and scrollable
//text area.
[TextArea(2, 5)]
public string Str;
6、[ContextMenu(" ")] 右键脚本添加菜单事件
//Adds the function to the context menu of the component.
[ContextMenu("QwQ")]
void HelloWorld()
{
Debug.Log("我发出了QwQ");
}
7、[ContextMenuItem(" ", " ")] 右键变量添加菜单事件
//Use this attribute to add a context menu to a field that calls a named method.
[ContextMenuItem("ResetValue", "resetValue")]
public int A = 100;
void resetValue()
{
A = 50;
}
8、[HideInInspector] 在Inspector面板中隐藏
//Makes a variable not show up in the inspector but be serialized.
[HideInInspector]
public string Str;
9、[Serializable] 可序列化
[Serializable]
public class SerializableTest
{
public int A = 5;
public Color c = Color.white;
}
public SerializableTest Test;
10、[Space(50)] 两个变量之间添加间距
//Use this DecoratorDrawer to add some spacing in the Inspector.
public string Str0;
[Space(50)]
public string Str1;
11、Class序列化
[Serializable]
public class TestClass
{
public string Str = "这是一句话...";
public bool IsOn;
}
public class Test : MonoBehaviour
{
public TestClass testClass;
}
12、[MenuItem("A/B")] 在菜单栏添加事件
(1)在顶部菜单栏添加事件
public class Test : MonoBehaviour
{
[MenuItem("QAQ/QvQ")]
public static void QvQ()
{
print("O-O");
}
}
(2)在Project空白部分右键菜单栏添加事件
public class Test
{
const string menuA = "Assets/QAQ/QvQ";
const string menuB = "Assets/QAQ/Q_Q";
const string menuC = "Assets/QAQ/QuQ";
static bool isOn;
[MenuItem(menuA, priority = 0, validate = false)]
private static bool OvO()
{
bool value = !EditorPrefs.GetBool("HAHA", false);
EditorPrefs.SetBool("HAHA", value);
Menu.SetChecked(menuA, value);
isOn = value;
Debug.Log("OvO" + value);
return value;
}
[MenuItem(menuB, priority = 100)]
private static void O_O()
{
if (isOn)
Debug.Log("O_O");
}
[MenuItem(menuC)]
private static void OuO()
{
if (!isOn)