轉(zhuǎn)載:https://yq.aliyun.com/articles/69190
這篇文章主要分享unity中與editor插件等相關(guān)的使用,比較基礎(chǔ)贯涎,不過如果都掌握了就可以擴展寫一些unity插件了佩憾,平時開發(fā)中也會提升工作效率挚币。
editor相關(guān)腳本一定要放在Editor文件夾下艾凯,繼承monobehaviour的文件不要放到Editor文件夾下摘投。
monobehaviour相關(guān)的編輯器功能
首先常用的在繼承monobehaviour類中寫public變量可以在inspector中序列化可編輯一般人都知道了煮寡,下面是一些可以更有效率更酷的方法。
增強序列化屬性
public bool isGood = false;
[Tooltip("hp")]//鼠標hover的時候顯示一個tooltip
public int life = 0;
[Range(0f, 1f)]//float slider
public float CloudRange = 0.5f;
[Range(0, 15)]//int slider
public int CloudRangeInt = 1;
[Header("OtherAttr")]//可以將屬性隔離開犀呼,形成分組的感覺
public float CloudHeader = 1f;
[Space(30)]//可以與上面形成一個空隙
public float CloudSpace = 1f;
[HideInInspector]//使屬性在inspector中隱藏幸撕,但是還是可序列化,想賦值可以通過寫程序賦值序列化
public float CloudHideInInspector = 1f;
[NonSerialized]//使public屬性不能序列化
public float CloudNonSerialized = 1f;
[SerializeField]//使private屬性可以被序列化外臂,在面板上顯示并且可以讀取保存
private bool CloudSerializeField = true;
效果如下圖坐儿,對于一些有范圍的數(shù)值可以用range做個slider讓策劃來調(diào)節(jié),可以用header和space來組織面板的外觀宋光,也可以針對不同的屬性進行是否序列化的選擇貌矿。
序列化類
也可以序列化一個類
[Serializable]//一個可序列化的類
public class SerializableClass {
public int x = 0;
public Vector2 pos;
public Color color;
public Sprite sprite;
}
public SerializableClass serializedObject;//一個可序列化的類的實例
組件面板的上下文菜單
有時在monobehaviour中寫一些方法可以初始化一些值或者隨機產(chǎn)生某個值這種需求,都可以在菜單中觸發(fā)罪佳,只要簡單的加一行即可逛漫。
[ContextMenu("Init")]//可以在組件的右鍵菜單及設(shè)置(那個小齒輪按鈕)菜單看到,
void Init()
{
isGood = false;
life = 0;
}
[ContextMenu("Random value")]
void RandomValue()
{
Debug.Log("TestContextMenu " + gameObject.name);
isGood = true;
life = UnityEngine.Random.Range(1, 100);
}
效果如下圖赘艳,點擊init就會賦一個初始的值酌毡,點擊randomvalue可以隨機產(chǎn)生一個life的值,這就是最簡單的editor工具了
inspector相關(guān)的編輯器功能
如果要在inspector中加上一些更高級的功能就需要使用editor相關(guān)的方法了
這是要使用的TestInspector類代碼
[CustomEditor(typeof(TestInspector))]
public class CloudTools : Editor {
#region inspector
TestInspector script;//所對應(yīng)的腳本對象
GameObject rootObject;//腳本的GameObject
SerializedObject seriObject;//所對應(yīng)的序列化對象
SerializedProperty headColor;//一個[SerializeField][HideInInspector]且private的序列化的屬性
private static bool toggle = true;//toggle按鈕的狀態(tài)
//初始化
public void OnEnable()
{
seriObject = base.serializedObject;
headColor = seriObject.FindProperty("headColor");
var tscript = (TestInspector)(base.serializedObject.targetObject);
if (tscript != null)
{
script = tscript;
rootObject = script.gameObject;
}else
{
Console.Error.WriteLine("tscript is null");
}
}
//清理
public void OnDisable()
{
var tscript = (TestInspector)(base.serializedObject.targetObject);
if (tscript == null)
{
// 這種情況是腳本對象被移除了;
Debug.Log("tscript == null");
}
else
{
// 這種情況是編譯腳本導致的重刷;
Debug.Log("tscript != null");
}
seriObject = null;
script = null;
rootObject = null;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
seriObject.Update();
//將target轉(zhuǎn)化為腳本對象
script = target as TestInspector;
//random按鈕
if (GUILayout.Button("RandomNum"))
{
//注冊undo蕾管,可以在edit菜單里看到undo枷踏,也可以通過ctrl+z來回退
Undo.RecordObject(script, "revert random num");
script.RandomNum(script.num);
}
//save scene和toggle這組按鈕
GUILayout.BeginHorizontal();
{
if (GUILayout.Button("SaveScene"))
{
EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
}
if(GUILayout.Button(toggle ? "untoggle" : "toggle"))
{
toggle = !toggle;
}
}
GUILayout.EndHorizontal();
script.isAlive = EditorGUILayout.BeginToggleGroup("isAlive", script.isAlive);
if (script.isAlive)//如果isAlive不勾選則不顯示life
{
script.life = EditorGUILayout.Slider("life", script.life, 0, 100f);
}
EditorGUILayout.EndToggleGroup();
//可以顯示TestInspector中序列化但是不在inspector中顯示的屬性
EditorGUILayout.PropertyField(headColor);
seriObject.ApplyModifiedProperties();
//展示普通信息
EditorGUILayout.LabelField("life " + script.life, GUILayout.Width(200));
Repaint();
}
#endregion
}
其中需要用OnEnable和OnDisable來做初始化和清理工作,OnInspectorGUI方法可以類比monobehaviour中的OnGUI掰曾,做ui渲染和ui事件處理旭蠕。
里面還注冊了UnDo,好處是可以通過ctrl+z來進行撤銷操作旷坦,這樣才更完美更像一個完善的unity插件掏熬。
代碼也沒什么難度,我也做了下簡單的注釋塞蹭,執(zhí)行一下看看效果大部分人就都理解了孽江。效果如下圖
各種上下文菜單
組件菜單
之前可以在monobehaviour中加入[ContextMenu("Random value")]
來生成對應(yīng)腳本組件面板的上下文菜單,那么如果要生成一個在transform組件上的菜單怎么辦
[MenuItem("CONTEXT/Transform/RandomPosition")]
static void ContextMenu_TransformRandomPosition()//隨機改變transform組件的position
{
Debug.Log("ContextMenu_Transform");
Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
foreach (Transform transform in transforms)
{
transform.localPosition = new Vector3(UnityEngine.Random.Range(-10, 10),
UnityEngine.Random.Range(-10, 10),
UnityEngine.Random.Range(-10, 10));
Debug.Log(transform.localPosition);
}
}
效果如下圖番电,如果策劃或者美術(shù)需要對transform的position干這種隨機的事岗屏,是不是就可以這么搞了辆琅?或者對collider、rigibody之類的組件加上一些屬性模板的設(shè)置这刷,會很方便吧
帶勾選的菜單
const string Menu_Checked = "Cloud/MenuChecked";//checked menu的名字
const string Key_MenuChecked = "MenuChecked";//checked menu狀態(tài)存儲的key
[MenuItem(Menu_Checked)]
static void MenuChecked()
{
bool flag = Menu.GetChecked(Menu_Checked);
if (flag)
{
Debug.Log("Key_MenuChecked to 0");
PlayerPrefs.SetInt(Key_MenuChecked, 0);//通過存儲0和1來判斷是否check menu
}
else
{
Debug.Log("Key_MenuChecked to 1");
PlayerPrefs.SetInt(Key_MenuChecked, 1);
}
Menu.SetChecked(Menu_Checked, !flag);
}
[MenuItem(Menu_Checked, true)]//判斷menu是否check的函數(shù)
public static bool IsMenuChecked()
{
Menu.SetChecked(Menu_Checked, PlayerPrefs.GetInt(Key_MenuChecked, 0) == 1);
return true;
}
效果如下圖婉烟,其中需要一個菜單的valid函數(shù)來判斷菜單是否在勾選狀態(tài),這里用了playprefs暇屋,在windows上就寫到注冊表里了
project面板中的菜單
這個菜單是加到了Assets下面似袁,那么在project面板中右鍵也可以看到,這種菜單可以干什么呢咐刨,我也沒想好昙衅,不過干些修改assetsimport屬性或者修改一些資源等等還是挺好用的吧
[MenuItem("Assets/TestAssets")]
static void MenuAssets()
{
if(Selection.activeObject == null)
{
Debug.Log("TestAssets choose null");
}
else
{
Debug.Log("TestAssets name = " + Selection.activeObject.name);
}
}
一般這種菜單都可以通過Selection.activeObject/activeGameObject
等等來獲取選中對象,當然也可以獲取多選的多個對象定鸟,這個看下api就知道了
hierarchy面板菜單
這個菜單還是比較實用的而涉,相對來說也不太一樣
[MenuItem("GameObject/Create Other/TestGameObject")]//將菜單放到GameObject菜單中,可以在hierarchy中看到
static void MenuGameObject()
{
Debug.Log("TestGameObject");
}
將菜單加到GameObject下面联予,就可以在hierarchy里右鍵看到了
那么基于這個菜單我們可以做個比較實用的功能啼县,例如右鍵hierarchy中場景的一個GameObject并且對它進行SetActive為true or false的操作,代碼如下:
//快捷鍵可以為%=ctrl/cmd #=shift &=alt LEFT/RIGHT/UP/DOWN F1-F12,HOME END PGUP PGDN _a~_z
[MenuItem("GameObject/SetActive _a", false, 11)] //11及以后可以在Camera之后顯示
static void MenuGameObjectSetActive()//通過按a鍵來設(shè)置所選擇GameObject的active狀態(tài)
{
Debug.Log("MenuGameObjectSetActive");
if(Selection.activeGameObject != null)
{
Undo.RecordObject(Selection.activeGameObject, "SetActive" + Selection.activeGameObject.activeSelf + " " + Selection.activeGameObject.name);
Selection.activeGameObject.SetActive(!Selection.activeGameObject.activeSelf);//就算鎖定了inpector,也是處理當前選中的
}
Debug.Log(Selection.activeObject.name);
}
[MenuItem("GameObject/SetActive", true, 11)]
static bool CheckIsGameObject()//判斷是否顯示該菜單的校驗方法沸久,如果沒選擇GameObject為灰
{
UnityEngine.Object selectedObject = Selection.activeObject;
if(selectedObject != null && selectedObject.GetType() == typeof(GameObject))
{
Debug.Log(selectedObject.name);
return true;
}
return false;
}
其中做校驗的方法是為了在不選中GameObject的時候能夠?qū)⒉藛位业艏揪臁A硗膺@種菜單可以綁定一個快捷鍵,這個例子是綁定了a鍵卷胯,菜單中也可以看出來子刮。
最終效果就成了:我選中一個GameObject,只要按下a鍵就可以SetActive(false)诵竭,再按下變成true话告,還是比較實用的吧,基于此可以做很多實用的東西卵慰。效果如下圖
對話框
對話框比較簡單,就是一些內(nèi)置的api佛呻,具體可以查看api裳朋,支持了例如簡單和復雜對話框、打開保存文件對話框吓著、進度條等等功能
EditorUtility.DisplayCancelableProgressBar("ok", "done", 0.7f)
EditorUtility.ClearProgressBar();
EditorUtility.OpenFilePanel("open", "d:/", ".txt");
新窗口
如果要做的事情可能不是與某個GameObject相關(guān)鲤嫡,inspector不能滿足要求,那么可以創(chuàng)建一個新的窗口绑莺,創(chuàng)建新的editor窗口需要繼承EditorWindow暖眼,代碼如下
[CustomEditor(typeof(CloudWindow))]
public class CloudWindow : EditorWindow {
#region 對話框
//通過MenuItem按鈕來創(chuàng)建這樣的一個對話框
[MenuItem("Cloud/ShowEditorTestPanel")]
public static void ConfigDialog()
{
EditorWindow.GetWindow(typeof(CloudWindow));
}
public UnityEngine.Object go = null;
string goName= "default";
float life = 100f;
bool isAlive = true;
bool toggleEnabled;
void OnGUI()
{
//Label
GUILayout.Label("Label Test", EditorStyles.boldLabel);
//通過EditorGUILayout.ObjectField可以接受Object類型的參數(shù)進行相關(guān)操作
go = EditorGUILayout.ObjectField(go, typeof(UnityEngine.Object), true);
//Button
if (GUILayout.Button("Button Test"))
{
if (go == null)
{
Debug.Log("go == null");
}
else
{
Debug.Log(go.name);
}
}
goName = EditorGUILayout.TextField("textfield", goName);
toggleEnabled = EditorGUILayout.BeginToggleGroup("optional settings", toggleEnabled);
if (toggleEnabled)
{
isAlive = EditorGUILayout.Toggle("isalive", isAlive);
life = EditorGUILayout.Slider("life", life, 0, 100);
}
EditorGUILayout.EndToggleGroup();
}
#endregion
}
好像代碼也不復雜,也沒什么難度就是常見的ui繪制纺裁,效果如下:
后續(xù)
如果有更高的需求可能需要更深入的研究一下unity中editor的相關(guān)api和文檔
unity還提供了可以在scene窗口中做一些操作诫肠,例如畫一些輔助線司澎、顯示label、操作handler等栋豫,具體可以參考http://blog.csdn.net/kun1234567/article/details/19421471
結(jié)語
如果把這些代碼執(zhí)行一遍挤安,改改調(diào)試一下,理解基本流程丧鸯,那么已經(jīng)可以寫一些提高工作效率的unity插件了