unity3d

Unity 编辑器十二 EditorPrefs、Undo、GUI

2021-12-13  本文已影响0人  合肥黑

参考
【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。

示例:


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.一些常用的方法如下:
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
上一篇 下一篇

猜你喜欢

热点阅读