1、常用的属性特性
- [Range(0,100)] //限制数值范围
- [Multiline(3)] //字符串多行显示
- [TextArea(2,4)] //文本输入框
- [SerializeField] //序列化字段,主要用于序列化私有字段
- [NonSerialized] //反序列化一个变量,并且在Inspector上隐藏
- [HideInInspector] //public变量在Inspector面板隐藏
- [FormerlySerializedAs(“Value1”)] //当变量名发生改变时,可以保存原来Value1的值
- [ContextMenu(“TestBtn”)] //组件右键菜单按钮
- [ContextMenuItem(“Reset Value”,“Reset”)] //定义属性的右键菜单
- [Header(“Header Name”)] //加粗效果的标题
- [Space(10)] //表示间隔空间,数字越大,间隔越大
- [Tooltip(“Tips”)] //显示字段的提示信息
- [ColorUsage(true)] //显示颜色面板
1.1 Range(min, max) 限制数值范围
代码示例
[Range(0, 10)]
public float floatValue;
效果展示
1.2 Multiline(行数)字符串多行显示
代码示例
[Multiline(3)]
public string str;
效果展示
1.3TextArea(min, max)文本输入框
代码示例
[TextArea(2, 5)]
public string textArea;
效果展示
1.4[SerializeField] //序列化字段,主要用于序列化私有字段
代码示例
[SerializeField]
private string serStr;
效果展示
未加
加上SerializeField
1.5[NonSerialized] //反序列化一个变量,并且在Inspector上隐藏
代码示例
[NonSerialized]
public string str2;
效果展示
未加
加上
1.6[HideInInspector] //public变量在Inspector面板隐藏
代码示例
[HideInInspector]
public string str3;
效果展示
未加
加上
1.7[Header(“Header Name”)] //加粗效果的标题
代码示例
[Header("Test Header")]
public string str4;
效果展示
1.8[Space(10)] //表示间隔空间,数字越大,间隔越大
代码示例
[Space(100)]
public string str5;
效果展示
1.9[Tooltip(“Tips”)] //显示字段的提示信息
代码示例
[Tooltip("Str6 6666666")]
public string str6;
效果展示
2、常用的方法特性
- [DrawGizmo] //用于Gizmos渲染,将逻辑与调试代码分离
- [MenuItem] //添加菜单项
2.1 [DrawGizmo] //用于Gizmos渲染,将逻辑与调试代码分离
代码示例
[DrawGizmo(GizmoType.Active | GizmoType.Selected)]
public static void DrawGizmos(Transform target, GizmoType gizmoType)
{
Color color = Gizmos.color;
Gizmos.color = Color.blue;
Gizmos.DrawCube(target.transform.position, Vector3.one * 1.05f);
Gizmos.color = color;
}
效果展示
- 2.2 [MenuItem] //添加菜单项
其他文章可见
代码示例
效果展示
3、常用的类的特性
- [Serializable] //序列化一个类,作为一个子属性显示在监视面板
- [RequireComponent(typeof(Animator))] //挂载该类的对象,必须要有Animator组件
- [DisallowMultipleComponent] //不允许挂载多个该类或其子类
- [ExecuteInEditMode] //允许脚本在编辑器未运行的情况下运行
- [CanEditMultipleObjects] //允许当选择多个挂有该脚本的对象时,统一修改值
- [AddComponentMenu] //可以在菜单栏Component内添加组件按钮
- [CustomEditor] //要自定义编辑器就要加这个特性
- [CustomPropertyDrawer] //用于绘制自定义PropertyDrawer的特性
- [SelectionBase] //选择在场景视图中使用此属性的组件对象,即不会误选中子物体
3.1 [Serializable] //序列化一个类,作为一个子属性显示在监视面板
代码示例
public Person person;
[Serializable]
public class Person
{
public string name;
public int age;
public string str;
}
效果展示
3.2 [RequireComponent(typeof(Animator))] //挂载该类的对象,必须要有Animator组件
代码示例
[RequireComponent(typeof(Rigidbody))]
public class Test02_EditorAttibute : MonoBehaviour
效果展示
3.3[DisallowMultipleComponent] //不允许挂载多个该类或其子类
代码示例
[DisallowMultipleComponent]
public class Test02_EditorAttibute : MonoBehaviour
效果展示
3.4 [ExecuteInEditMode] //允许脚本在编辑器未运行的情况下运行
代码示例
[ExecuteInEditMode]
public class Test02_EditorAttibute : MonoBehaviour
效果展示
3.5 [CanEditMultipleObjects] //允许当选择多个挂有该脚本的对象时,统一修改值
代码示例
[CanEditMultipleObjects]
public class Test02_EditorAttibute : MonoBehaviour
效果展示
3.6 [AddComponentMenu] //可以在菜单栏Component内添加组件按钮
代码示例
[AddComponentMenu("Test02_EditorAttibute")]
public class Test02_EditorAttibute : MonoBehaviour
效果展示
- 自定义属性特性
需要用到两个类PropertyAttribute和PropertyDrawer
还有一个类特性[CustomPropertyDrawer(typeof(ShowTimeAttribute))]
代码示例
定义特性
using UnityEngine;
//定义特性
public class ShowTimeAttribute : PropertyAttribute
{
public readonly bool ShowHour;
//定义构造函数
public ShowTimeAttribute(bool isShowHour = false)
{
ShowHour = isShowHour;
}
}
用于绘制特性
using UnityEngine;
using UnityEditor;
//用于绘制特性,该类需要放到Editor中
[CustomPropertyDrawer(typeof(ShowTimeAttribute))]
public class TimeDrawer : PropertyDrawer
{
//设置绘制的区域高度
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property) * 2;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.Integer)
{
property.intValue = EditorGUI.IntField(new Rect(position.x, position.y, position.width, position.height / 2), label, Mathf.Max(0, property.intValue));
EditorGUI.LabelField(new Rect(position.x, position.y + position.height / 2, position.width, position.height / 2), "", TimeConvert(property.intValue));
}
else
{
EditorGUI.HelpBox(position, "To use the Time Atribute," + label.ToString() + "must be int", MessageType.Error);
}
}
private string TimeConvert(int value)
{
ShowTimeAttribute time = attribute as ShowTimeAttribute;
if (time != null)
{
if (time.ShowHour)
{
int hours = value / (60 * 60);
int minutes = (value % (60 * 60)) / 60;
int seconds = value % 60;
return string.Format("{0}:{1}:{2}(H:M:S)", hours, minutes.ToString().PadLeft(2, '0'), seconds.ToString().PadLeft(2, '0'));
}
}
else
{
int minutes = (value % (60 * 60)) / 60;
int seconds = value % 60;
return string.Format("{0}:{1}(M:S)", minutes.ToString().PadLeft(2, '0'), seconds.ToString().PadLeft(2, '0'));
}
return string.Empty;
}
}
使用
//测试
public class Test : MonoBehaviour
{
[ShowTime(true)]
public int time = 3605;
}
效果展示