创建一个窗口
创建一个窗口必须继承自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的一些成员
- focusedWindow:静态属性,获取鼠标聚焦的window
- mouseOverWindow:静态属性,获取当前鼠标下的window
- this.ShowNotification(new GUIContent(“显示一条消息!”)); 显示一条消息
- this.RemoveNotification(); 清除消息显示
- 创建一些组件:
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;
}
}