Unity 编辑器十二 EditorPrefs、Undo、GUI
参考
【Unity 编辑器】扩展总结八:EditorPrefs、ScriptableObject、Undo
【Unity 编辑器】扩展总结九:GUIStyle、GUISkin
【Unity编辑器】扩展总结十:AssetPostprocessor资源导入管线
一、EditorPrefs
Unity编辑器为开发者提供了类似PlayerPrefs的数据保存方式EditorPrefs。EditorPrefs是适用于编辑器模式,而PlayerPrefs适用于游戏运行时。
EditorPrefs提供了四种数据的保存:int,float,string,bool。通过Set方法保存数据,下次则通过Get方法来获取数据,HasKey方法可以判断是否存在该数据的保存,删除数据调用DeleteKey方法即可。
using UnityEngine;
using UnityEditor;
public class WindowExample2 : EditorWindow
{
private static WindowExample2 window;//窗体实例
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);
}
}
}
注意:需要谨慎调用EditorPrefs.DeleteAll()方法,因为该方法还可能会删除Unity编辑器自身存储的一些数据,给开发者带来不必要的麻烦。
二、Undo
Undo用于编辑器模式下的撤销操作,这里介绍几种常用的API。
- Undo.RegisterCreatedObjectUndo : 记录新建的对象状态,可以撤销新建的对象
- Undo.RecordObject:记录对象的状态,需要在修改之前调用
- Undo.AddComponent:可以撤销新挂载的组件
- Undo.DestroyObjectImmediate:可以撤销删除对象的操作
- Undo.SetTransformParent:可以撤销修改父对象的操作
示例:
image.png
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()
{
//获取选中的场景对象
Transform trans = Selection.activeGameObject.transform;
if (trans)
{
Undo.RecordObject(trans, "MoveObj");
trans.position += Vector3.up;
}
}
[MenuItem("Tools/AddComponent Obj")]
private static void AddComponentObj()
{
//获取选中的场景对象
GameObject selectedObj = Selection.activeGameObject;
if (selectedObj)
{
Undo.AddComponent(selectedObj,typeof(Rigidbody));
}
}
[MenuItem("Tools/Destroy Obj")]
private static void DestroyObj()
{
//获取选中的场景对象
GameObject selectedObj = Selection.activeGameObject;
if (selectedObj)
{
Undo.DestroyObjectImmediate(selectedObj);
}
}
[MenuItem("Tools/SetParent Obj")]
private static void SetParentObj()
{
//获取选中的场景对象
Transform trans = Selection.activeGameObject.transform;
Transform root = Camera.main.transform;
if (trans)
{
Undo.SetTransformParent(trans, root, trans.name);
}
}
}
三、GUIStyle
GUIStyle用于修改GUI的风格样式,除了适用于编辑器开发,也适用于Unity旧版的UI系统(IMGUI)。GUIStyleu拥有多种属性,可以方便开发者自定义编辑器UI样式。当我们未自定义GUIStyle时,使用的就是unity默认的GUIStyle样式。GUIStyle有点像网页前端开发的层叠样式表CSS,拥有很多状态属性可以调整。(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默认的Label样式
EditorGUILayout.LabelField("GUIStyle");
//使用自定义的Label样式
EditorGUILayout.LabelField("GUIStyle", _titleStyle);
}
}
四、GUISkin
GUISkin是基本所有样式的集合,可以作为一种配置资源。如果开发者需要自定义大量的GUIStyle,可以通过GUISkin配置资源来定义,并且开发者可以在Inspector面板中直接修改样式。
在Project面板,鼠标右键Create-GUISkin既可以创建。可以将新建的GUISkin资源放在Editor里的Resources文件内,方便动态加载。
五、AssetPostprocessor
1.一些常用的方法如下:
- OnPreprocessTexture:在导入纹理贴图之前调用
- OnPreprocessModel:在导入模型之前调用
- OnPreprocessAudio:在导入音频之前调用
- OnPostprocessTexture:在导入纹理贴图之后调用
- OnPostprocessModel:在导入模型之后调用
- OnPostprocessAudio:在导入音频之后调用
- OnPostprocessAllAssets:所有资源的导入,删除,移动操作都会调用该方法
2.示例,对导入的纹理贴图资源进行一定的自动设置
注意:对图片纹理的设置需要放在OnPreprocessTexture方法中执行
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);
}
}
效果如下:
添加脚本前导入图片:
image.png
添加脚本后导入图片:
image.png
显示的日志信息:
image.png