Unity Editor extension implements an Excel table reading window

design

Insert image description here

Unity Editor window class

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/meter reading configuration interface”, false, 10)]

The first parameter is the path and
Insert image description here
the second parameter defaults to false.

The third parameter is the lower the priority, the higher it is.

Customize window drawing content

private void OnGUI()
{
    
    




}

So just follow our sketch and we can complete it step by step.

title

Insert image description here

Select the imported excel root directory

		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();

Then leave a little gap between our next function and this function

GUILayout.Space( 4 );

Then there are the batch traversal button and the export button

 		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();

Finally, the excel display area we selected needs a sliding display page.

滑动页面核心

excelScrollerPos = EditorGUILayout.BeginScrollView( excelScrollerPos );
//Other target content that needs to be rendered is written in the middle
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();
        }

final effect

Insert image description here

Source code

//==========================
// - 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();
    }
}

Guess you like

Origin blog.csdn.net/qq_39162566/article/details/131422650