Unity Editor工具开发小抄
工具开发的需求不是天天有,这些冷门的api总是在用到的时候要翻之前的代码才回忆得起来,为了节约时间做个汇总,方便cv,持续更新。
Editor Windows模板
public class MeshVertexTool : EditorWindow
{
[MenuItem("Tools/OpenMeshVertex %q")]
public static void OpenMeshVertexWindows()
{
MeshVertexTool windows = (MeshVertexTool)EditorWindow.GetWindow(typeof(MeshVertexTool));
windows.titleContent = new GUIContent("模型顶点冗余数据优化工具");
windows.minSize = new Vector2(500f, 300f);
}
public void OnGUI()
{
//UI方法
}
}
Inspector扩展
[CustomEditor(typeof(UIRebuildChecker))]
public class UIRebuilderCheckerEditor : Editor
{
UIRebuildChecker checker;
private void OnEnable()
{
checker = (UIRebuildChecker)target;
}
public override void OnInspectorGUI()
{
}
}
Editor下加载资源
AssetDatabase.LoadAssetAtPath的路劲必须以Asset开头,有时可能需要加一步绝对路径到相对路径的转换
path = path.Replace(Application.dataPath, "Assets");
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
进度条
EditorUtility.DisplayProgressBar("查找中", string.Format("{0}/{1}", i, paths.Length), 1.0f * i / paths.Length);
EditorUtility.ClearProgressBar();
//可中途取消的进度条
bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", file, (float)i / (float)files.Length);
if (isCancel)
{
break;
}
ScrollView
scrollVec = EditorGUILayout.BeginScrollView(scrollVec);
EditorGUILayout.EndScrollView();
查找指定后缀的文件
string[] paths = Directory.GetFiles(searchPath, "*.fbx", SearchOption.AllDirectories);
保存修改后的资源
EditorUtility.SetDirty
AssetDatabase.SaveAssets
AssetDatabase.Refresh
路径选择
string selectPath = EditorUtility.OpenFolderPanel("请选择补丁生成的路径", Application.dataPath, "");
if (string.IsNullOrEmpty(selectPath))
{
return;
}
PlayerPrefs.SetString(IFixPatchConfig.PLAYERPREFS_PATH, selectPath );
下拉列表
private string[] options = new string[] { "选项1", "选项2", "选项3" };
private int index;
index = EditorGUILayout.Popup("下拉列表: ", index, options);
GUILayout.FlexibleSpace()
创建一个自适应的空白区域,也即是填满本次布局中的这部分空间
弹出消息提示
ShowNotification(new GUIContent("弹出消息提示"));
二次确认框
string content = string.Format("确认要删除区域配置{0}吗?", cur.Name);
if (EditorUtility.DisplayDialog("二次确认", content, "确定", "取消"))
{
}
EditorUtility.DisplayDialog("错误", "必须选择路径", "确定");
打开文件目录
EditorUtility.RevealInFinder(GetLuaPath());