kingBook

导航

unity Attributes特性

AddComponentMenu

//使用 AddComponentMenu 属性可在“Component”菜单中的任意位置放置脚本,而不仅是“Component > Scripts”菜单。
//使用此属性可以更好地组织 Component 菜单,从而改进添加脚本时的工作流程。 重要提示:您需要重新启动。

using UnityEngine;
[AddComponentMenu("Transform/Follow Transform")]
public class FollowTransform : MonoBehaviour{ }
//注意:MenuItem是编辑器类,所以只能导入 using UnityEditor; 命名空间,且一般我们的类也不是集成自MonoBehaviour的,而是集成ScriptableObject的

[MenuItem("Tools/Run")]
private static void Run(){
    Debug.Log("Run");
}

//MenuItem菜单的先后排序:
[MenuItem("Tools/Run2", false, 2)]
private static void Run2(){
    Debug.Log("Run2");
}
[MenuItem("Tools/Run1", false, 1)]
private static void Run1(){
    Debug.Log("Run1");
}

//MenuItem与快捷键进行关联:
//规则是: 
//% = ctrl 
//# = Shift 
//& = Alt 
//LEFT/RIGHT/UP/DOWN = 上下左右 
//F1…F2 = F... 
//HOME, END, PGUP, PGDN = 键盘上的特殊功能键 
//注意,如果是键盘上的普通按键,比如a~z,则要写成_a ~ _z这种带_前缀的。
[MenuItem("Tools/Run3 _%#&_1")]
private static void Run3(){
    Debug.Log("Run3");
}

ContextMenu

//向Inspector面板中脚本Script的上下文菜单(快捷,右键),添加一条指令,当选择该命令时,函数会执行。
//如:预定义的方法Reset(可以进行重载)。
//注意:只能用于非静态函数

using UnityEngine;
public class ContextTesting : MonoBehaviour{
    /// Add a context menu named "Do Something" in the inspector
    /// of the attached script.
    [ContextMenu("Do Something")]
    void DoSomething(){
        Debug.Log("Perform operation");
    }
}

ContextMenuItem

//在Inspector面板中,为字段field添加一个快捷的菜单

[Multiline][ContextMenuItem("Reset", "ResetString")]
public string abc;
public void ResetString(){
	abc = "";
}

CreateAssetMenu

//快速的创建ScriptableObject派生类的实例,并存储成以“.asset"结尾的文件,ScriptableObject的派生类可以存储为外部的文件,图形化编辑对象数据,一些静态的数据,动态的加载,ScriptableObject是一种解决方案,具体见另一篇文章的说明:
//https://www.jianshu.com/p/da578e55ca47

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "xxxx",menuName = "xxx/xxx")]
public class testEdit : ScriptableObject {
    public int a = 10;
    public int b = 11;
    public int c = 12;
    [Multiline][ContextMenuItem("Reset", "ResetString")]
    public string abc;

}

PreferBinarySerialization

//只能用在ScriptableObject的派生类上(使用在其它类型上面会被忽略),修改序列化的模式为二进制,而不是YAML, 当你的资源比较大的时候,保存成二进制,生成的数据会更加的紧凑,从而提高程序的读写性能。
[CreateAssetMenu]
[PreferBinarySerialization]
public class testEdit : ScriptableObject {
    
    public Color a;
    public int b = 11;
    public int c = 12;  
    [Multiline][ContextMenuItem("Reset", "ResetString")]
    public string abc;

}
//用记事本打开生成后的asset,会发现都是二进制的数据
//在 Inspector 中的某些字段上方添加标题。

[Header("Health Settings")]
public int health = 0;
public int maxHealth = 100;

[Header("Shield Settings")]
public int shield = 0;
public int maxShield = 0;

HelpURL

//为类提供自定义文档 URL。
using UnityEngine;
using UnityEditor;

[HelpURL("http://example.com/docs/MyComponent.html")]
public class MyComponent{
}

HideInInspector

//使变量不显示在 Inspector 中,但进行序列化。
using UnityEngine;
public class Example : MonoBehaviour{
    // Make the variable p not show up in the inspector
    // but be serialized.
    [HideInInspector]
    int p = 5;
}

Space

//设置间隔
public int a = 10;
[Space(50)]
public int b = 11;

TextArea

//文本区域
//参数:
//minLines:文本区域最小行数
//maxLines:文本区域最大行数,超过最大行数,会出现滚动条。

[TextArea(1,5)]
public string abc;

Multiline

//在一个支持多行的文本区域内编辑string字符串,他和 TextArea 不同,Multiline 的 TextArea 没有滚动条。
[Multiline(1,5)]
public string abc;

ColorUsage

[ColorUsage(false)]
public Color m_color;

CanEditMultipleObjects

[CanEditMultipleObjects]//可多对象编辑
public class Collider2DEditor:Editor {}

SerializeField

[SerializeField]//序列化私有属性
private Emitter _emitter;

System.NonSerialized

[System.NonSerialized]//非序列化属性
public string message;

ExecuteInEditMode

[ExecuteInEditMode]//使脚本的所有实例都在编辑模式下执行。
public class Test:MonoBehaviour{}

更多说明:
https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html
https://www.jianshu.com/p/70f6e0d8bbf8

posted on 2019-08-06 11:58  kingBook  阅读(156)  评论(0编辑  收藏  举报