? ? ? ? 匯總一些Unity Editor開發(fā)的常用方法和實(shí)現(xiàn)方式,會持續(xù)更新。
添加自定義菜單欄方法
using UnityEngine;
using UnityEditor;
public class EditorTools : EditorWindow
{
[MenuItem("EditorTools/自定義的編輯器方法")]
public static void CustomEditroFunction()
{
Debug.Log("Here is your code...");
}
}
如圖一,添加自定義的菜單欄需要使用UnityEditor的命名空間,我們自定義的EditorTools類需要繼承EditorWindow類。然后就是定義菜單欄點(diǎn)擊后所執(zhí)行的具體函數(shù)CustomEditroFunction,這里需要注意的是函數(shù)需要是靜態(tài)函數(shù),需要增加static關(guān)鍵字。MenuItem方法則是定義菜單欄的路徑,菜單欄路徑可以采用中文。圖二則是自定義菜單欄的效果。
EditorWindow類文檔連接:https://docs.unity3d.com/cn/2022.2/ScriptReference/EditorWindow.html
添加Project自定義菜單欄方法
using UnityEngine;
using UnityEditor;
public class ProjectEditorTools : EditorWindow
{
[MenuItem("Assets/自定義的編輯器方法", priority = 0)]
public static void CustomEditroFunction()
{
Debug.Log("Here is your code...");
}
}
在Project窗口添加自定義菜單欄需要MenuItem方法以Assets為起始,自定義的菜單欄也會在頂部菜單欄Assets頁簽下顯示。
添加Hierarchy自定義菜單欄方法
using UnityEngine;
using UnityEditor;
public class HierarchyEditorTools : EditorWindow
{
[MenuItem("GameObject/自定義的編輯器方法", priority = 0)]
public static void CustomEditorFunction()
{
Debug.Log("Here is your code...");
}
}
?在Hierarchy窗口右鍵菜單添加自定義菜單欄需要MenuItem方法的路徑以GameObject起始,如上面示例代碼。添加方法后除了Hierarchy會顯示自定義的菜單欄,在頂部GameObject下也會顯示自定義的菜單欄。
?MenuItem文檔連接:https://docs.unity3d.com/cn/2022.2/ScriptReference/MenuItem.html
添加Inspector中腳本自定義方法
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
[CustomEditor(typeof(Text), true)]
public class TextEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
CustomTextFunction();
}
private void CustomTextFunction()
{
if (GUILayout.Button("清空內(nèi)容"))
{
Text text = (Text)target;
text.text = string.Empty;
}
}
}
這里我們?yōu)閁nity原有的Text腳本增加功能按鈕,添加自定義功能。
如圖一,首先還是需要使用UnityEditor命名空間,自定義類TextEditor繼承自Editor。通過覆寫OnInspectorGUI方法實(shí)現(xiàn)Inspector中腳本的繪制,DrawDefaultInspector為繪制默認(rèn)視圖。接下來CustomTextFunction則是我們自定義的部分,通過GUILayout.Button("清空內(nèi)容")方法繪制自定義按鈕,if語句中則是點(diǎn)擊按鈕后的自定義功能,這里示例的則是清除了Text內(nèi)容。實(shí)際效果如圖二。
在示例代碼中需要注意的是,[CustomEditor(typeof(Text), true)]指定了Inspector自定義腳本的類型,會為指定的自定義腳本添加功能?;怑ditor包含成員target,通過target可以獲取指定的腳本對象。
Editor類文檔連接:?https://docs.unity3d.com/cn/2022.2/ScriptReference/Editor.html
添加自定義窗口方法
using UnityEngine;
using UnityEditor;
public class CustomEditorWindow :EditorWindow
{
[MenuItem("EditorTools/CustomEditorWindow")]
public static void ShowCustomEditorWindow()
{
EditorWindow.GetWindowWithRect<CustomEditorWindow>(new Rect(0, 0, 300, 150), false, "CustomEditorWindow");
}
private void OnGUI()
{
}
}
如圖一,這里我們創(chuàng)建了一個最簡單的自定義窗口,自定義窗口類需要繼承EditorWindow類,通過 EditorWindow.GetWindowWithRect 或者 EditorWindow.GetWindow 方法創(chuàng)建窗口。
EditorWindow.GetWindowWithRect 為創(chuàng)建固定尺寸的窗口。
EditorWindow.GetWindow 為創(chuàng)建動態(tài)尺寸的窗口,可以通過拖動右下角拉伸窗口尺寸。
OnGUI 方法用于繪制窗口內(nèi)的各種組件,下一節(jié)將單獨(dú)講解。
EditorWindow類文檔連接:https://docs.unity3d.com/cn/2022.2/ScriptReference/EditorWindow.html
GUILayout的常用方法
using UnityEngine;
using UnityEditor;
public class CustomEditorWindow : EditorWindow
{
private string textField = "";
private string textArea = "";
[MenuItem("EditorTools/CustomEditorWindow")]
public static void ShowCustomEditorWindow()
{
EditorWindow.GetWindowWithRect<CustomEditorWindow>(new Rect(0, 0, 300, 150), false, "CustomEditorWindow");
}
private void OnGUI()
{
GUILayout.Label("Hello World");
textField = GUILayout.TextField(textField);
textArea = GUILayout.TextArea(textArea);
if (GUILayout.Button("點(diǎn)擊按鈕"))
{
Debug.LogError("按鈕點(diǎn)擊響應(yīng)成功");
}
}
}
?這里我們來介紹一下GUILayout常用的方法。
GUILayout.Label:用于創(chuàng)建文本內(nèi)容,支持多行顯示,使用\n或者@“”等方式可以多行顯示。
GUILayout.TextField:創(chuàng)建一個可編輯的單行文本框,到達(dá)最大長度后輸入內(nèi)容會超框。如圖一需要創(chuàng)建一個成員變量存儲輸入內(nèi)容。
GUILayout.TextArea:創(chuàng)建一個可編輯的多行文本框,到達(dá)最大長度后文本框會換行。同樣輸入內(nèi)容需要成員變量存儲。
GUILayout.Button:創(chuàng)建一個單擊按鈕,需要用if語句判斷點(diǎn)擊。按鈕支持圖片形式,可傳入一個Texture類對象創(chuàng)建圖片按鈕。
using UnityEngine;
using UnityEditor;
public class HorizontalGUI : EditorWindow
{
[MenuItem("EditorTools/HorizontalGUI")]
public static void HorizontalGUIEnum()
{
EditorWindow.GetWindowWithRect<HorizontalGUI>(new Rect(0, 0, 500, 50), false, "HorizontalGUI");
}
private void OnGUI()
{
GUILayout.BeginHorizontal();
if (GUILayout.Button("按鈕1"))
{
}
if (GUILayout.Button("按鈕2"))
{
}
GUILayout.Label("文本顯示:一二三四五");
GUILayout.EndHorizontal();
}
}
GUILayout.BeginHorizontal:開始一個水平控件組,可將多個控件放在同一行。
GUILayout.EndHorizontal:結(jié)束一個水平控件組,BeginHorizontal的配套結(jié)束方法。
未完待續(xù)。
關(guān)于GUILayout更多細(xì)節(jié)可以查閱官方文檔。
GUILayout類文檔連接:https://docs.unity3d.com/cn/2022.2/ScriptReference/GUILayout.html
EditorUtility的常用方法
EditorUtility.OpenFilePanel:顯示“打開文件”對話框并返回所選的路徑名稱。
EditorUtility.OpenFolderPanel:顯示“打開文件夾”對話框并返回所選的路徑名稱。
EditorUtility.RevealInFinder:打開指定目錄文件夾文章來源:http://www.zghlxwxcb.cn/news/detail-450715.html
EditorUtility類文檔連接:https://docs.unity3d.com/cn/2022.2/ScriptReference/EditorUtility.html文章來源地址http://www.zghlxwxcb.cn/news/detail-450715.html
到了這里,關(guān)于【Unity小知識】Editor編寫常用方法匯總的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!