UnityEditor研究学习之创建一个窗口

创建一个窗口

创建一个窗口必须继承自EditorWindow类,然后一行代码搞定:
ReplaceSharderEditor window = (ReplaceSharderEditor)EditorWindow.GetWindow(typeof(ReplaceSharderEditor));

using UnityEditor;

public class ReplaceSharderEditor : EditorWindow
{
    [MenuItem("辅助/批量替换shader")]
    public static void OpenWindow()
    {
        ReplaceSharderEditor window = (ReplaceSharderEditor)EditorWindow.GetWindow(typeof(ReplaceSharderEditor));
    }

}


EditorWindow的一些成员

  1. focusedWindow:静态属性,获取鼠标聚焦的window
  2. mouseOverWindow:静态属性,获取当前鼠标下的window
  3. this.ShowNotification(new GUIContent(“显示一条消息!”)); 显示一条消息
  4. this.RemoveNotification(); 清除消息显示
  5. 创建一些组件:
    groupEnabled = EditorGUILayout.BeginToggleGroup(“Option Setting”, groupEnabled);
    toggleEnabled = EditorGUILayout.Toggle(“Toggle”, toggleEnabled);
    sliderValue = EditorGUILayout.Slider(“Slider”, sliderValue, 0, 3);
    myStr = GUILayout.TextField(myStr);

一些特定的回调函数:
在这里插入图片描述

using UnityEngine;
using UnityEditor;

public class EditorWindowTest : EditorWindow
{
    [MenuItem("My Window/cchop")]
    public static void OpenWindow()
    {
        EditorWindowTest window = EditorWindow.GetWindow<EditorWindowTest>();
        EditorWindow.GetWindow<EditorWindowTest>(false, "Window的标题");
        window.Show();
    }

    string myStr = "Text Field";
    bool groupEnabled;
    bool toggleEnabled;
    float sliderValue;
    //显示
    void OnGUI()
    {
        GUILayout.Label(EditorWindow.focusedWindow.ToString());
        GUILayout.Label(EditorWindow.mouseOverWindow.ToString());
        GUILayout.Label("这是一个Label(带有EditorStyles.boldLabel)", EditorStyles.boldLabel);

        groupEnabled = EditorGUILayout.BeginToggleGroup("Option Setting", groupEnabled);   //只有群组启用,子组件才会被启用
        toggleEnabled = EditorGUILayout.Toggle("Toggle", toggleEnabled);
        sliderValue = EditorGUILayout.Slider("Slider", sliderValue, 0, 3);
        myStr = GUILayout.TextField(myStr);

        EditorGUILayout.EndToggleGroup();

        if (GUILayout.Button("显示Notification"))
        {
            this.ShowNotification(new GUIContent("显示一条消息!"));
        }

        if (GUILayout.Button("删除Notification"))
        {
            this.RemoveNotification();
        }
    }
}

小demo:批量替换材质球的shader

在这里插入图片描述
1.显示进度条:EditorUtility.DisplayProgressBar(title, desc, value);
2.清除进度条:EditorUtility.ClearProgressBar();
3.从指定路径下加载资源:AssetDatabase.LoadAssetAtPath< T>(path);
4.显示提示信息:EditorGUILayout.HelpBox(tipMsg, tipMsgType);
5.更改鼠标外观:DragAndDrop.visualMode = DragAndDropVisualMode.Generic;

using UnityEditor;
using UnityEngine;
using System.IO;

public class ReplaceSharderEditor : EditorWindow
{
    string filePath;
    Rect fileRect;
    Shader shader;

    string tipMsg = null;
    MessageType tipMsgType = MessageType.Info;

    [MenuItem("辅助/批量替换shader")]
    public static void OpenWindow()
    {
        ReplaceSharderEditor window = (ReplaceSharderEditor)EditorWindow.GetWindow(typeof(ReplaceSharderEditor), false, "批量替换shader");
    }

    void OnGUI()
    {
        EditorGUILayout.LabelField("文件夹路径:");

        //获得一个框
        fileRect = EditorGUILayout.GetControlRect(GUILayout.Width(position.width - 25));
        //将上面的框作为文本输入框 
        filePath = EditorGUI.TextField(fileRect, filePath);

        //拖拽文件中
        if (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform)
        {
            string[] paths = DragAndDrop.paths;

            if (fileRect.Contains(Event.current.mousePosition) && paths != null && paths.Length > 0)
            {
                if (Directory.Exists(paths[0]))
                {
                    //更改鼠标外观
                    DragAndDrop.visualMode = DragAndDropVisualMode.Generic;

                    //拖拽丢弃时才进行赋值操作
                    if (Event.current.type == EventType.DragPerform)
                        filePath = paths[0];
                }
            }
        }

        EditorGUILayout.LabelField("shader :");
        shader = (Shader)EditorGUILayout.ObjectField(shader, typeof(Shader), true);

        if (GUILayout.Button("批量替换", GUILayout.Height(30)))
        {
            Replace();
        }

        if (GUILayout.Button("重置", GUILayout.Height(30)))
        {
            Reset();
        }

        //提示信息
        if (!string.IsNullOrEmpty(tipMsg))
        {
            EditorGUILayout.HelpBox(tipMsg, tipMsgType);
        }
    }

    void Replace()
    {
        if (!Directory.Exists(filePath))
        {
            tipMsg = "错误路径!";
            tipMsgType = MessageType.Error;
            return;
        }
        if (shader == null)
        {
            tipMsg = "shader为空!";
            tipMsgType = MessageType.Error;
            return;
        }

        var dir = new DirectoryInfo(filePath);
        var files = dir.GetFiles("*.mat", SearchOption.AllDirectories);
        int count = 0;
        int length = files.Length;
        for (int i = 0; i < length; i++)
        {
            UpdateProgress(i + 1, length, "替换中...");
            var path = files[i].FullName;
            path = path.Replace('\\', '/');
            var mat = AssetDatabase.LoadAssetAtPath<Material>(path.Substring(path.IndexOf("Assets")));
            if (mat != null && mat.shader != shader)
            {
                mat.shader = shader;
                count++;
            }
        }
        AssetDatabase.Refresh();
        AssetDatabase.SaveAssets();
        tipMsg = "修改成功!修改了" + count + "个Material";
        tipMsgType = MessageType.Info;

        EditorUtility.ClearProgressBar();
    }

    void UpdateProgress(int progress, int progressMax, string desc)
    {
        string title = "Processing...[" + progress + " - " + progressMax + "]";
        float value = (float)progress / (float)progressMax;
        EditorUtility.DisplayProgressBar(title, desc, value);
    }

    void Reset()
    {
        tipMsg = null;
        filePath = null;
        shader = null;
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cchoop

有用的话请杯肥宅水

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值