设计
Unity Editor窗口类
public class ExcelEditorWindow : EditorWindow
{
[MenuItem( "Frameworks/读表配置界面", false, 10 )]
private static void Open()
{
Rect wr = new Rect( 0, 0, 500, 500 );
ExcelEditorWindow window = ( ExcelEditorWindow ) EditorWindow.GetWindowWithRect( typeof( ExcelEditorWindow ), wr, true, "Export Excel Window" );
window.Show();
}
}
[MenuItem( “Frameworks/读表配置界面”, false, 10 )]
第一个参数是路径
第二个参数默认false
第三个参数是优先级 越低越靠上
自定义窗口绘制内容
private void OnGUI()
{
}
好 跟着我们的草图 我们一步步的完成就可以了
标题
选择导入的excel根目录
GUILayout.Label( "选择一个批量导出的Excel目录: " );
GUILayout.BeginHorizontal();
excelRootFolder = GUILayout.TextField( excelRootFolder, 128, GUILayout.MaxWidth( 400f ) );
if ( GUILayout.Button( "选择目录" ) )
{
string newFolder = EditorUtility.OpenFolderPanel( "选择Excel目录", excelRootFolder, string.Empty );
if ( !string.IsNullOrEmpty( newFolder ) && !string.IsNullOrWhiteSpace( newFolder ) )
{
excelRootFolder = newFolder;
if ( Directory.Exists( excelRootFolder ) )
{
data.excelRootFolder = excelRootFolder;
}
}
}
GUILayout.EndHorizontal();
然后我们下一个功能和这个功能之间 留一点空隙
GUILayout.Space( 4 );
然后就是 批量遍历按钮 和 导出按钮
GUILayout.BeginHorizontal();
if ( GUILayout.Button( "开始遍历" ) )
{
excels.Clear();
if ( Directory.Exists( excelRootFolder ) )
{
ConsoleUtils.Clear();
string[] xlsxs = Directory.GetFiles( excelRootFolder, "*.xlsx", SearchOption.TopDirectoryOnly );
excels.AddRange( xlsxs );
}
else
{
EditorUtility.DisplayDialog( "路径错误", $"不存在 {excelRootFolder}", "确认" );
Debug.LogError( $"Excel路径错误: {excelRootFolder}" );
}
}
else if ( GUILayout.Button( "批量导出" ) )
{
BatchExport( excelRootFolder );
}
else if ( GUILayout.Button( "打开目录 - Excel源" ) )
{
EditorUtility.RevealInFinder( excelRootFolder );
}
else if ( GUILayout.Button( "打开目录 - 数据源" ) )
{
var dataFolder = Path.Combine( Application.dataPath, "BundleRes/ExcelData/" );
EditorUtility.RevealInFinder( dataFolder );
}
else if ( GUILayout.Button( "打开目录 - 数据类" ) )
{
var csFolder = Path.Combine( Application.dataPath, "Scripts/ExcelCSharp/" );
EditorUtility.RevealInFinder( csFolder );
}
GUILayout.EndHorizontal();
最后就是我们筛选出的excel展示区域 需要一个滑动展示页面
滑动页面核心
excelScrollerPos = EditorGUILayout.BeginScrollView( excelScrollerPos );
//其它需要渲染的目标内容写着中间
EditorGUILayout.EndScrollView();
if ( excels.Count > 0 )
{
GUILayout.Space( 4 );
excelScrollerPos = EditorGUILayout.BeginScrollView( excelScrollerPos );
EditorGUILayout.BeginVertical();
for ( int i = 0; i < excels.Count; i++ )
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.TextArea( excels[ i ], GUILayout.MaxWidth( 400 ) );
if ( GUILayout.Button( "Export" ) )
{
Export( excels[ i ] );
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndScrollView();
}
最终效果
源码
//==========================
// - FileName: Assets/Frameworks/Editor/Excel/ExcelEditorWindow.cs
// - Created: ChenJC
// - CreateTime: 2023-06-19 10:03:20
// - UnityVersion: 2019.4.35f1
// - Version: 1.0
// - Description:
//==========================
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class ExcelEditorWindow : EditorWindow
{
[MenuItem( "Frameworks/读表配置界面", false, 10 )]
private static void Open()
{
Rect wr = new Rect( 0, 0, 500, 500 );
ExcelEditorWindow window = ( ExcelEditorWindow ) EditorWindow.GetWindowWithRect( typeof( ExcelEditorWindow ), wr, true, "Export Excel Window" );
window.Show();
}
class ExcelEditorConfig
{
public string excelRootFolder = "Excels";
public string lastUpdateTime = string.Empty;
}
ExcelEditorConfig data;
private string GetConfigFilePath()
{
return Path.Combine( Application.persistentDataPath, nameof( ExcelEditorConfig ) );
}
private void OnEnable()
{
if ( !File.Exists( GetConfigFilePath() ) )
{
data = new ExcelEditorConfig();
try
{
var clips = Application.dataPath.Split( '/' );
var projname = clips[ clips.Length - 2 ];
var plant = projname.Split( '-' )[ 0 ] + "-plan";
var plantPath = Application.dataPath.Replace( $"{projname}/Assets", $"{plant}/Excel/" );
data.excelRootFolder = plantPath;
}
catch ( Exception e ) { }
data.excelRootFolder = Path.Combine( Application.dataPath, data.excelRootFolder );
}
else
{
try
{
data = JsonConvert.DeserializeObject<ExcelEditorConfig>( File.ReadAllText( GetConfigFilePath() ) );
}
catch ( Exception e )
{
Debug.LogError( e );
data = new ExcelEditorConfig();
}
}
data.lastUpdateTime = DateTime.Now.ToString();
excelRootFolder = data.excelRootFolder;
}
private void OnDisable()
{
try
{
string jsonstr = JsonConvert.SerializeObject( data );
File.WriteAllText( GetConfigFilePath(), jsonstr );
}
catch ( Exception e )
{
Debug.LogError( e );
}
}
string excelRootFolder = string.Empty;
List<string> excels = new List<string>();
Vector2 excelScrollerPos = Vector2.zero;
//绘制窗口时调用
private void OnGUI()
{
GUILayout.Label( "选择一个批量导出的Excel目录: " );
GUILayout.BeginHorizontal();
excelRootFolder = GUILayout.TextField( excelRootFolder, 128, GUILayout.MaxWidth( 400f ) );
if ( GUILayout.Button( "选择目录" ) )
{
string newFolder = EditorUtility.OpenFolderPanel( "选择Excel目录", excelRootFolder, string.Empty );
if ( !string.IsNullOrEmpty( newFolder ) && !string.IsNullOrWhiteSpace( newFolder ) )
{
excelRootFolder = newFolder;
if ( Directory.Exists( excelRootFolder ) )
{
data.excelRootFolder = excelRootFolder;
}
}
}
GUILayout.EndHorizontal();
GUILayout.Space( 4 );
GUILayout.BeginHorizontal();
if ( GUILayout.Button( "开始遍历" ) )
{
excels.Clear();
if ( Directory.Exists( excelRootFolder ) )
{
ConsoleUtils.Clear();
string[] xlsxs = Directory.GetFiles( excelRootFolder, "*.xlsx", SearchOption.TopDirectoryOnly );
excels.AddRange( xlsxs );
}
else
{
EditorUtility.DisplayDialog( "路径错误", $"不存在 {excelRootFolder}", "确认" );
Debug.LogError( $"Excel路径错误: {excelRootFolder}" );
}
}
else if ( GUILayout.Button( "批量导出" ) )
{
BatchExport( excelRootFolder );
}
else if ( GUILayout.Button( "打开目录 - Excel源" ) )
{
EditorUtility.RevealInFinder( excelRootFolder );
}
else if ( GUILayout.Button( "打开目录 - 数据源" ) )
{
var dataFolder = Path.Combine( Application.dataPath, "BundleRes/ExcelData/" );
EditorUtility.RevealInFinder( dataFolder );
}
else if ( GUILayout.Button( "打开目录 - 数据类" ) )
{
var csFolder = Path.Combine( Application.dataPath, "Scripts/ExcelCSharp/" );
EditorUtility.RevealInFinder( csFolder );
}
GUILayout.EndHorizontal();
if ( excels.Count > 0 )
{
GUILayout.Space( 4 );
excelScrollerPos = EditorGUILayout.BeginScrollView( excelScrollerPos );
EditorGUILayout.BeginVertical();
for ( int i = 0; i < excels.Count; i++ )
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.TextArea( excels[ i ], GUILayout.MaxWidth( 400 ) );
if ( GUILayout.Button( "Export" ) )
{
Export( excels[ i ] );
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndScrollView();
}
}
private void Export( string xlsx )
{
ExcelExport.ConvertFromFile( xlsx );
}
private void BatchExport( string folder )
{
ExcelExport.ConvertFromFolder( folder );
}
private void OnInspectorUpdate()
{
this.Repaint();
}
}