參考
【Unity 編輯器】擴(kuò)展總結(jié)八:EditorPrefs仔粥、ScriptableObject、Undo
【Unity 編輯器】擴(kuò)展總結(jié)九:GUIStyle蟹但、GUISkin
【Unity編輯器】擴(kuò)展總結(jié)十:AssetPostprocessor資源導(dǎo)入管線
一躯泰、EditorPrefs
Unity編輯器為開發(fā)者提供了類似PlayerPrefs的數(shù)據(jù)保存方式EditorPrefs。EditorPrefs是適用于編輯器模式华糖,而PlayerPrefs適用于游戲運(yùn)行時(shí)麦向。
EditorPrefs提供了四種數(shù)據(jù)的保存:int,float,string,bool。通過Set方法保存數(shù)據(jù)客叉,下次則通過Get方法來獲取數(shù)據(jù)诵竭,HasKey方法可以判斷是否存在該數(shù)據(jù)的保存,刪除數(shù)據(jù)調(diào)用DeleteKey方法即可兼搏。
using UnityEngine;
using UnityEditor;
public class WindowExample2 : EditorWindow
{
private static WindowExample2 window;//窗體實(shí)例
private string tempMsg;
//顯示窗體
[MenuItem("MyWindow/Second Window")]
private static void ShowWindow()
{
window = EditorWindow.GetWindow<WindowExample2>("Window Example");
window.Show();
}
private void OnEnable()
{
if (EditorPrefs.HasKey("TempMsg"))
{
tempMsg = EditorPrefs.GetString("TempMsg");
}
}
private void OnGUI()
{
tempMsg = EditorGUILayout.TextField("Temp Msg", tempMsg);
if (GUILayout.Button("Save"))
{
EditorPrefs.SetString("TempMsg", tempMsg);
}
}
}
注意:需要謹(jǐn)慎調(diào)用EditorPrefs.DeleteAll()方法卵慰,因?yàn)樵摲椒ㄟ€可能會(huì)刪除Unity編輯器自身存儲(chǔ)的一些數(shù)據(jù),給開發(fā)者帶來不必要的麻煩佛呻。
二裳朋、Undo
Undo用于編輯器模式下的撤銷操作,這里介紹幾種常用的API吓著。
- Undo.RegisterCreatedObjectUndo : 記錄新建的對(duì)象狀態(tài)鲤嫡,可以撤銷新建的對(duì)象
- Undo.RecordObject:記錄對(duì)象的狀態(tài),需要在修改之前調(diào)用
- Undo.AddComponent:可以撤銷新掛載的組件
- Undo.DestroyObjectImmediate:可以撤銷刪除對(duì)象的操作
- Undo.SetTransformParent:可以撤銷修改父對(duì)象的操作
示例:
using UnityEditor;
using UnityEngine;
public class UndoTest
{
[MenuItem("Tools/Create Obj")]
private static void CreateObj()
{
GameObject newObj = new GameObject("Undo");
Undo.RegisterCreatedObjectUndo(newObj, "CreateObj");
}
[MenuItem("Tools/Move Obj")]
private static void MoveObj()
{
//獲取選中的場景對(duì)象
Transform trans = Selection.activeGameObject.transform;
if (trans)
{
Undo.RecordObject(trans, "MoveObj");
trans.position += Vector3.up;
}
}
[MenuItem("Tools/AddComponent Obj")]
private static void AddComponentObj()
{
//獲取選中的場景對(duì)象
GameObject selectedObj = Selection.activeGameObject;
if (selectedObj)
{
Undo.AddComponent(selectedObj,typeof(Rigidbody));
}
}
[MenuItem("Tools/Destroy Obj")]
private static void DestroyObj()
{
//獲取選中的場景對(duì)象
GameObject selectedObj = Selection.activeGameObject;
if (selectedObj)
{
Undo.DestroyObjectImmediate(selectedObj);
}
}
[MenuItem("Tools/SetParent Obj")]
private static void SetParentObj()
{
//獲取選中的場景對(duì)象
Transform trans = Selection.activeGameObject.transform;
Transform root = Camera.main.transform;
if (trans)
{
Undo.SetTransformParent(trans, root, trans.name);
}
}
}
三绑莺、GUIStyle
GUIStyle用于修改GUI的風(fēng)格樣式泛范,除了適用于編輯器開發(fā),也適用于Unity舊版的UI系統(tǒng)(IMGUI)紊撕。GUIStyleu擁有多種屬性罢荡,可以方便開發(fā)者自定義編輯器UI樣式。當(dāng)我們未自定義GUIStyle時(shí)对扶,使用的就是unity默認(rèn)的GUIStyle樣式区赵。GUIStyle有點(diǎn)像網(wǎng)頁前端開發(fā)的層疊樣式表CSS,擁有很多狀態(tài)屬性可以調(diào)整浪南。(unity2019之后新出的UIElement模塊中的USS就更像是CSS了)
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
private GUIStyle _titleStyle;
private void OnEnable()
{
_titleStyle = new GUIStyle();
_titleStyle.fontStyle = FontStyle.Bold;
_titleStyle.fontSize = 20;
}
public override void OnInspectorGUI()
{
//使用Unity默認(rèn)的Label樣式
EditorGUILayout.LabelField("GUIStyle");
//使用自定義的Label樣式
EditorGUILayout.LabelField("GUIStyle", _titleStyle);
}
}
四笼才、GUISkin
GUISkin是基本所有樣式的集合,可以作為一種配置資源络凿。如果開發(fā)者需要自定義大量的GUIStyle骡送,可以通過GUISkin配置資源來定義昂羡,并且開發(fā)者可以在Inspector面板中直接修改樣式。
在Project面板摔踱,鼠標(biāo)右鍵Create-GUISkin既可以創(chuàng)建虐先。可以將新建的GUISkin資源放在Editor里的Resources文件內(nèi)派敷,方便動(dòng)態(tài)加載蛹批。
五、AssetPostprocessor
1.一些常用的方法如下:
- OnPreprocessTexture:在導(dǎo)入紋理貼圖之前調(diào)用
- OnPreprocessModel:在導(dǎo)入模型之前調(diào)用
- OnPreprocessAudio:在導(dǎo)入音頻之前調(diào)用
- OnPostprocessTexture:在導(dǎo)入紋理貼圖之后調(diào)用
- OnPostprocessModel:在導(dǎo)入模型之后調(diào)用
- OnPostprocessAudio:在導(dǎo)入音頻之后調(diào)用
- OnPostprocessAllAssets:所有資源的導(dǎo)入篮愉,刪除腐芍,移動(dòng)操作都會(huì)調(diào)用該方法
2.示例,對(duì)導(dǎo)入的紋理貼圖資源進(jìn)行一定的自動(dòng)設(shè)置
注意:對(duì)圖片紋理的設(shè)置需要放在OnPreprocessTexture方法中執(zhí)行
using UnityEngine;
using UnityEditor;
public class AssetsImport : AssetPostprocessor
{
private void OnPreprocessTexture()
{
Debug.Log("OnPreprocessTexture:" + this.assetPath);
TextureImporter importer = this.assetImporter as TextureImporter;
importer.textureType = TextureImporterType.Sprite;
importer.maxTextureSize = 512;
importer.mipmapEnabled = false;
}
public void OnPostprocessTexture(Texture2D tex)
{
Debug.Log("OnPostprocessTexture:" + this.assetPath);
}
}
效果如下:
添加腳本前導(dǎo)入圖片:
添加腳本后導(dǎo)入圖片:
顯示的日志信息: