- 在变量上使用[SerializeField]属性,可以强制对该变量进行序列化,即可以在Editor模式对变量进行赋值(即使是private类型也可以)。
public class serializeField : MonoBehaviour {
[SerializeField]
private string id;
}
2. 在class上使用[RequireComponent]属性,则在class所挂载的GameObject上会自动追加所需要的Component
[RequireComponent(typeof(Rigidbody))]
public class requireComponent : MonoBehaviour {}
3. [ContextMenu]属性,可以在挂载了该类的Inspector面板中执行相应的方法。
public class contextMenu : MonoBehaviour {
[ContextMenu("Do Something")]
void DoSomething()
{
Debug.Log("Perform operation");
}
}
4. [ContextMenuItem]属性,可以在挂载了该类的Inspector面板对某变量添加一个右键菜单并指定执行相应的方法。
public class contextMenuItem : MonoBehaviour {
[ContextMenuItem("Reset", "ResetName")]
public new string name = "Name";
public string password = "Password";
void ResetName() {
name = "Name";
}
}
5. 在静态方法上添加[MenuItem]属性。可以在Editor中创建一个菜单项,点击后执行该方法。多级菜单用“/”分隔。
public class menuItem : MonoBehaviour {
[MenuItem("MenuItem/Create GameObject")]
public static void CreateGameObject() {
new GameObject("new A GameObject");
}
}
6. [AddComponentMenu]属性,可以在UnityEditor的Component的Menu中添加自定义的功能。菜单是多级时使用“/”分隔。
[AddComponentMenu("AddMenu/AddConponent")]
public class addComponentMenu : MonoBehaviour {}
7. 在class上使用[DisallowMultipleComponent]属性,在同一个GameObject对象上只能添加该类的一个实例。
[DisallowMultipleComponent]
public class disallowMultipleConponent : MonoBehaviour {}
8. [Header]属性,在Inspector面板显示数据
public class header : MonoBehaviour {
[Header("生命值")]
public int CurrentHP = 0;
public int MaxHP = 100;
[Header("魔法值")]
public int CurrentMP = 0;
public int MaxMP = 100;
}
9. 默认状态下class的Start、Update、OnGUI等方法需要在Play状态下才会被执行,但在class中添加[ExecuteInEditMode]属性后,这些方法在Editor模式也能执行。Update方法只有在Scene面板中物体变化时才会被调用;OnGUI方法只有在GameView接收到事件时才会被调用。
[ExecuteInEditMode]
public class executeInEditMode : MonoBehaviour {
void Update () {
Debug.Log(this.transform.position);
}
}
10. 在public类型的变量上添加[HideInInspector]属性,则在Inspector面板上不会显示该变量。
public class hideInInspector : MonoBehaviour {
[HideInInspector]
public new string name;
public string password;
}
11. 在OnRenderImage上使用[ImageEffectOpaque]属性可以让渲染顺序在非透明之后、透明物体之前。
public class imageEffectOpaque : MonoBehaviour {
[ImageEffectOpaque]
void OnRenderImage(RenderTexture source, RenderTexture destination) {}
}
- 在string类型上使用[MultillineAttribute]属性,可以在Editor上输入多行文字。
public class multilineAttribute : MonoBehaviour {
[MultilineAttribute]
public string mText;
}
13. 在int或float上使用[Range]属性可以限制输入值的范围
public class range : MonoBehaviour {
[Range(0, 100)]
public float HP;
}
14. 在静态方法上使用[RuntimeInitializeOnLoadMethod]属性,在运行时会自动调用添加该属性的方法。
public class runtimeInitializeOnLoadMethod : MonoBehaviour {
[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad() {
Debug.Log("Game load and is running");
}
}
15. 使用[Space]属性可以在Inspector面板添加一些空位。
public class space : MonoBehaviour {
public int nospace1 = 0;
public int nospace2 = 0;
[Space(10)]
public int space1 = 0;
public int nospace3 = 0;
}
16. 在string上使用[TextArea]属性可以将变量在Inspector面板上的编辑区变成TextArea。
public class textArea : MonoBehaviour {
[TextArea]
public string mText;
}
17. 在变量上使用[Tooltip]属性,当鼠标移动到该变量上时提示信息。
public class tooltip : MonoBehaviour {
[Tooltip("This year is 2017!")]
public int yaer = 0;
}
18. 在变量上添加[FormerlySerializedAs]属性,可以令变量以另外的名称进行序列化,并且在变量自身修改名称时不会丢失之前的序列化的值。
public class formerlySerializedAs : MonoBehaviour {
[FormerlySerializedAs("myValue")]
private string m_MyValue;
public string myValue {
get {
return m_MyValue;
}
set {
m_MyValue = value;
}
}
}
- 在class上添加[CustomPreview]属性,将该class标记为指定类型的自定义预览。
[CustomPreview(typeof(GameObject))]
public class customPreview : ObjectPreview {
public override bool HasPreviewGUI()
{
return true;
}
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
GUI.Label(r, target.name + "is being previewed");
}
}
- 在class上添加[InitializeOnLoad]属性,在Play模式下自动执行静态构造函数,无需实例。
[InitializeOnLoad]
public class initializeOnLoad : MonoBehaviour {
static initializeOnLoad() {
EditorApplication.update += Update;
Debug.Log("Up and running");
}
static void Update() {
Debug.Log("Updating");
}
}
- 使用[PreferenceItem]属性自定义Preference界面。
public class preferenceItem {
private static bool prefsLoaded = false;
public static bool boolPreference = false;
//添加一个名为"My Preference"到Preferences Window
[PreferenceItem("My Preference")]
public static void PreferencesGUI() {
//Load the preference
if (!prefsLoaded) {
boolPreference = EditorPrefs.GetBool("BoolPreferenceKey", false);
prefsLoaded = true;
}
//Preferences GUI
boolPreference = EditorGUILayout.Toggle("Bool Preference", boolPreference);
//Save the preference
if (GUI.changed)
EditorPrefs.SetBool("BoolPreferenceKey", boolPreference);
}
}
22. 在打开一个Asset后被调用
public class assetHandler {
[OnOpenAsset(1)]
public static bool step1(int instanceID, int line) {
string name = EditorUtility.InstanceIDToObject(instanceID).name;
Debug.Log("Open Asset step: 1 (" + name + ")");
return false;
}
[OnOpenAsset(2)]
public static bool step2(int instanceID, int line) {
Debug.Log("Open Asset step: 2 (" + instanceID + ")");
return false;
}
}
- 在静态方法中添加[PostProcessBuild]属性,该方法会在Build后被调用。在静态方法中添加[PostProcessScene]属性,该方法会在Build前被调用。
public class postProcess : MonoBehaviour {
[PostProcessBuild(1)]
public static void OnPostProcessBuild(BuildTarget target, string pathToBuildProject) {
Debug.Log("PostProcessBuild");
Debug.Log(target);
Debug.Log(pathToBuildProject);
}
[PostProcessScene(1)]
public static void OnPostProcessScene()
{
Debug.Log("PostProcessScene");
}
}