菜单项
???????大前提:如果引用了unityEditor的命名空间或者继承自Editor类的话,需要把该脚本放在Editor目录下,该目录可以在Asset根目录下,也可以在其子目录下。还有几个重载就不再验证了。
[MenuItem]
???????必须为静态方法,一般用于插件开发,平时无卵用。
using UnityEditor;
using UnityEngine;
public class Menu : MonoBehaviour
{
[MenuItem("Menu/Menu1")]
public static void MenuList()
{
Debug.Log("Menu");
}
}
 ???????还可以添加快捷键 ???????[MenuItem("Menu/Menu1 _W")] 直接按W触发,无大小写限制  ???????[MenuItem("Menu/Menu1 #w")] #表示shift+快捷键触发 ???????[MenuItem("Menu/Menu1 %w")] %表示ctrl+快捷键触发 ???????[MenuItem("Menu/Menu1 &w")] &表示alt+快捷键触发
[AddComponentMenu]
???????向Inspector组件菜单中添加目录索引,如果目录已经存在了则不会再次创建。
using UnityEngine;
[AddComponentMenu("Audio/ComponentTest")]
public class ComponentTest : MonoBehaviour
{
}
 ???????就算改为已有脚本名字[AddComponentMenu("Audio/UIViewer")] ,也是添加的当前脚本,并不冲突。 
[ContextMenu]
???????还是上面的脚本,新添加一个方法,访问权限与返回值任意,必须为实例方法。
using UnityEngine;
[AddComponentMenu("Audio/ComponentTest")]
public class ComponentTest : MonoBehaviour
{
[ContextMenu("DoSomething")]
private void DoSomething()
{
}
}
???????右键刚才添加的脚本就显示了。 
[RequireComponent]
using UnityEngine;
[RequireComponent(typeof(Audio.AudioManager))]
[AddComponentMenu("Audio/UIViewer")]
public class ComponentTest : MonoBehaviour
{
[ContextMenu("DoSomething")]
private void DoSomething()
{
}
}
 ???????此特性即添加此组件时,同时添加其依附组件,在未移除此组件时不可以移除依附组件,不光插件开发可以用到,平时也可以用。
Inspector界面
???????声明public字段时,可以在Inspector面板显示,其他访问权限的字段则不显示,这几个特性即与显示和数值有关。序列化我也是似懂非懂,可以参照下这篇文章——序列化理解起来很简单。
[HideInInspector]和[NonSerialized]
???????HideInInspector 单纯的在面板中隐藏,还是会序列化。 ???????NonSerialized 不仅在面板中隐藏,而且会阻止该字段序列化。 ???????先声明三个普通的公有字段,不加特性,在Inspector面板把三个值都改为2。然后添加特性后在代码处修改并打印。
using System;
using UnityEngine;
public class InspectorTest : MonoBehaviour
{
public int a = 3;
[HideInInspector] public int b = 3;
[NonSerialized] public int c = 3;
private void Awake()
{
Debug.Log("a" + a + " " + "b" + b + " " + "c" + c);
}
}
  ???????面板里也只出现了A的值,BC都被隐藏。B的值仍然是Inspector面板里赋予的初值,而C的值却可以被改变。
[Serializable]
???????可以把公有引用字段显示在面板上。
using System;
using UnityEngine;
public class InspectorTest : MonoBehaviour
{
public AnimatorTest animatorTest;
}
[Serializable]
public class AnimatorTest
{
public Animator animator;
public AnimationClip animationClip;
public Color color;
}

[SerializeField]
???????加在非公有字段前面,可以把非公有的字段显示在面板上,同时对其序列化。 ???????同样声明赋予初值没用,会取用面板赋予的值。
自定义操作界面
???????
[CustomEditor]
???????自定义Inspector面板里数据的显示、排序等,这里只写简单使用。同样也是数据类,四个字段。
using System;
using UnityEngine;
public class InspectorTest : MonoBehaviour
{
public int a;
public float b;
public string c;
public DataBase d;
}
[Serializable]
public class DataBase
{
public int a;
public float b;
}
using UnityEditor;
[CustomEditor(typeof(InspectorTest))]
public class InspectorEditor : Editor
{
public SerializedObject inspectorTest;
public SerializedProperty aInteger;
public SerializedProperty bFloat;
public SerializedProperty cString;
public SerializedProperty dDataBase;
private void OnEnable()
{
inspectorTest = new SerializedObject(target);
aInteger = inspectorTest.FindProperty("a");
bFloat = inspectorTest.FindProperty("b");
cString = inspectorTest.FindProperty("c");
dDataBase = inspectorTest.FindProperty("d");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.PropertyField(aInteger);
EditorGUILayout.PropertyField(cString);
EditorGUILayout.PropertyField(bFloat);
EditorGUILayout.PropertyField(dDataBase);
inspectorTest.Update();
}
}

|