Fork me on GitHub

Unity中常用的Attribute标签

CustomEditor


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/// <summary>
/// 要自定义编辑的脚本
/// </summary>
[CustomEditor(typeof(PlugExtend))]
public class PlugExtendEditor : Editor
{
public override void OnInspectorGUI()
{
PlugExtend extend = target as PlugExtend;
Vector2 vec = new Vector2(extend.min, extend.max);
vec = EditorGUILayout.Vector2Field("Min And Max", vec);
}
}

public class PlugExtend : MonoBehaviour
{
public float min;
public float max;
}

效果图

ExecuteInEditMode


1
2
3
4
5
6
7
8
9
10
11
12
13
14
/// <summary>
/// 编辑器模式行执行
/// </summary>
[ExecuteInEditMode]
public class PlugExtend : MonoBehaviour
{
/// <summary>
/// 挂载这个脚本后编辑器状态下也能够执行这个方法
/// </summary>
private void Awake()
{
Debug.Log("PlugExtend Awake function");
}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class PlugExtendEditor : EditorWindow
{
/// <summary>
/// 在顶部菜单Tools/OpenWindow菜单
/// %#&u 快捷键 Ctrl + Shift + Alt + U
/// false 验证函数为false的情况下不激活
/// 11 菜单中的显示顺序权重
/// </summary>
[MenuItem("Tools/OpenWindow %#&u", false, 11)]
public static void Open()
{
PlugExtendEditor window = EditorWindow.CreateInstance<PlugExtendEditor>();
window.Show();
}
/// <summary>
/// 验证函数
/// false 的情况下Tools/OpenWindow菜单无效
/// true 的情况下Tools/OpenWindow菜单激活
/// </summary>
/// <returns></returns>
[MenuItem("Tools/OpenWindow %#&u", true, 11)]
public static bool ValidateOpen()
{
bool validate = false;
//验证是否打开窗口条件
return validate;
}
}

public class PlugExtend : MonoBehaviour
{
/// <summary>
/// 在齿轮菜单中添加一个菜单项
/// CONTEXT 固定写法
/// Transform 脚本所挂载的对象上的组件
/// setting 菜单名
/// </summary>
[MenuItem("CONTEXT/Transform/setting")]
private static void Setting()
{
Debug.Log("this is setting");
}
}

AddComponentMenu


1
2
3
4
5
6
7
8
9
10
11
12
13
14
/// <summary>
/// 在Inspector面板中最下面有个 [Add Component] 按钮,点击后的菜单。
/// </summary>
[AddComponentMenu("Test/PlugExtend")]
public class PlugExtend : MonoBehaviour
{
/// <summary>
/// 挂载这个脚本后编辑器状态下也能够执行这个方法
/// </summary>
private void Awake()
{
Debug.Log("PlugExtend Awake function");
}
}

效果图

ContextMenu

直接上代码上图

1
2
3
4
5
[ContextMenu("DoSomething")]
public void DoSomething()
{
//响应时做某种事情
}

效果图

ContextMenuItem


1
2
3
4
5
6
7
8
9
10
/// <summary>
/// 属性上右键菜单响应所调用的函数
/// </summary>
[ContextMenuItem("Reset", "ResetOption")]
public string value = "";
void ResetOption()
{
value = "liuaf";
Debug.Log(value);
}

效果图

Multiline & TextArea


1
2
3
4
[Multiline(8)]
public string value;
[TextArea(1, 5)]
public string value1;

效果图

ColorUsage


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public Color color1;
/// <summary>
/// 没有alpha
/// </summary>
[ColorUsage(false)]
public Color color2;
/// <summary>
/// showAlpha 如果为false,则alpha通道信息。
/// HDR true视为HDR颜色(默认值:false)。
/// minBrightness 使用HDR拾色器时的最小允许HDR色彩分量值(默认值:0)。
/// maxBrightness 使用HDR拾色器时的最大允许HDR色彩分量值(默认值:8)。
/// minExposureValue HDR颜色选择器允许的最小曝光值(默认值:1/8 = 0.125)。
/// maxExposureValue HDR颜色选择器允许的最大曝光值(默认值:3)。
/// </summary>
[ColorUsage(true, true, 0, 8, 0.125f, 3)]
public Color color3;
`

CreateAssetMenu & PreferBinarySerialization


1
2
3
4
5
6
7
8
9
10
11
/// <summary>
/// fileName 创建的资源名
/// menuName create菜单中显示的菜单名
/// order 顺序
/// PreferBinarySerialization 使用二进制序列化
/// </summary>
[CreateAssetMenu( fileName = "customData.asset", menuName ="CustomData", order =10)]
[PreferBinarySerialization]
public class CustomData : ScriptableObject
{
}

效果图

DisallowMultipleComponent


1
2
3
4
5
6
7
8
/// <summary>
/// 不允许同一个GameObject 挂载多个此脚本
/// 继承此脚本的对象也不可以存在多个
/// </summary>
[DisallowMultipleComponent]
public class NewBehaviourScript : MonoBehaviour
{
}

1
2
3
4
5
6
7
8
9
/// <summary>
/// 在属性面板中的字段添加标题
/// </summary>
[Header("Health Settings")]
public int health = 0;
public int maxHealth = 100;
[Header("Shield Settings")]
public int shield = 0;
public int maxShield = 0;

效果图

HelpURL


1
2
3
4
5
6
7
/// <summary>
/// 帮助文档url
/// </summary>
[HelpURL("https://aifee.github.io")]
public class NewBehaviourScript : MonoBehaviour
{
}

HideInInspector


1
2
3
4
5
/// <summary>
/// 属性面板中隐藏此属性的编辑
/// </summary>
[HideInInspector]
public int p = 5;

Range


1
2
3
4
5
6
7
8
9
10
11
12
13
public class NewBehaviourScript : MonoBehaviour
{
/// <summary>
/// 限制valueInt值在 0 - 10 范围内
/// </summary>
[Range(0,10)]
public int valueInt;
/// <summary>
/// 限制valueFloat值在 0 - 1 范围内
/// </summary>
[Range(0,1)]
public float valueFloat;
}

RequireComponent


1
2
3
4
5
6
7
/// <summary>
/// 自动关联依赖的组件,如果没有则为这个GameObject 添加这个依赖组件
/// </summary>
[RequireComponent(typeof(Image))]
public class NewBehaviourScript : MonoBehaviour
{
}

RuntimeInitializeOnLoadMethod


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/// <summary>
/// 游戏加载运行后不需要继承MonoBehaviour也可以响应函数
/// 注意:能够确定的是在Awake函数后调用
/// 但多个这个标记时无法保证执行顺序
/// </summary>
public class NewBehaviourScript
{
[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad()
{
Debug.Log("After scene is loaded and game is running");
}

[RuntimeInitializeOnLoadMethod]
static void OnSecondRuntimeMethodLoad()
{
Debug.Log("SecondMethod After scene is loaded and game is running.");
}
}

SelectionBase


1
2
3
4
5
6
7
8
9
/// <summary>
/// 挂载这个脚本的对象在Scene界面选择对象时
/// 如果点中它的子对象时,会默认选择这个对象
/// 是个不错的功能,尤其场景中对象很多很乱的时候
/// </summary>
[SelectionBase]
public class NewBehaviourScript : MonoBehaviour
{
}

SerializeField


1
2
3
4
5
6
7
8
public class NewBehaviourScript : MonoBehaviour
{
/// <summary>
/// private 修饰的属性,在属性面板中依然可以编辑
/// </summary>
[SerializeField]
private bool hasHealthPotion = true;
}

Space


1
2
3
4
5
6
7
8
9
10
11
public class NewBehaviourScript : MonoBehaviour
{
public int health = 0;
public int maxHealth = 100;
/// <summary>
/// 间距像素,同 GUILayout.Space(50) 用法相似
/// </summary>
[Space(50)]
public int shield = 0;
public int maxShield = 0;
}

效果图

Tooltip


1
2
3
4
5
6
7
8
public class NewBehaviourScript : MonoBehaviour
{
/// <summary>
/// 为字段添加提示,鼠标获取焦点后显示
/// </summary>
[Tooltip("Health value between 0 and 100.")]
public int health = 0;
}

效果图

PreferenceItem


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// <summary>
/// 官方的案例
/// 可以定制 Edit / Preferences 面板
/// </summary>
public class NewBehaviourScript : MonoBehaviour
{
// Have we loaded the prefs yet
private static bool prefsLoaded = false;
// The Preferences
public static bool boolPreference = false;
// Add preferences section named "My Preferences" to the Preferences Window
[PreferenceItem("My Preferences")]
public static void PreferencesGUI()
{
// Load the preferences
if (!prefsLoaded)
{
boolPreference = EditorPrefs.GetBool("BoolPreferenceKey", false);
prefsLoaded = true;
}
// Preferences GUI
boolPreference = EditorGUILayout.Toggle("Bool Preference", boolPreference);
// Save the preferences
if (GUI.changed)
EditorPrefs.SetBool("BoolPreferenceKey", boolPreference);
}
}

效果图

坚持刻苦学习!不忘初心,您的支持是我分享最大的动力!
-------------本文结束感谢您的阅读-------------