Unity Editor扩展 实现一个Excel读表窗口

这篇具有很好参考价值的文章主要介绍了Unity Editor扩展 实现一个Excel读表窗口。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

设计

Unity Editor扩展 实现一个Excel读表窗口,unity,excel,lucene

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 )]

第一个参数是路径
Unity Editor扩展 实现一个Excel读表窗口,unity,excel,lucene
第二个参数默认false

第三个参数是优先级 越低越靠上

自定义窗口绘制内容

private void OnGUI()
{




}

好 跟着我们的草图 我们一步步的完成就可以了

标题

Unity Editor扩展 实现一个Excel读表窗口,unity,excel,lucene

选择导入的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();
        }

最终效果

Unity Editor扩展 实现一个Excel读表窗口,unity,excel,lucene文章来源地址https://www.toymoban.com/news/detail-589505.html

源码

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

到了这里,关于Unity Editor扩展 实现一个Excel读表窗口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity编辑器扩展-第六集-创建窗口/批量填图

    第五集链接:Unity编辑器扩展-第五集-撤回操作/禁止操作/加快捷键_菌菌巧乐兹的博客-CSDN博客 一、本节目标+效果展示 1.创建窗口 2.图片批量赋值到物体上 二、创建窗口  这个功能其实也很好理解,我们之前学了点击选择,但 我们难免会遇见需要选不同位置,不同种类的很多

    2024年02月10日
    浏览(33)
  • 【Unity编辑器扩展】语言国际化工具,生成多语言Excel自动翻译并导出多语言表

     多语言是个非常简单且常用的功能。但是重复工作量大,程序手动把多语言Key配置到多语言表经常会出现错漏,或者几经改版,有些Key已经不用却没有剔除,久而久之造成冗余。这中简单且重复的工作必须让工具来完成。 多语言通过Key,Value的形式保存,通过多语言API GF.

    2024年02月11日
    浏览(46)
  • 安装Unity Hub和Unity Editor

    1、首先下载UnityHub的安装包: https://unity.com/cn/download 开始您的创意项目并下载 Unity Hub | Unity 2、运行安装包 3、运行Unity Hub 第一次运行Unity Hub会默认弹出登录Unity提示,正常登录就可以了,登录后会在浏览器提示打开 UnityHub,点击打开即可。 4、偏好设置 可以通过偏好设置,

    2024年02月03日
    浏览(29)
  • unity多种方式实现文件选择和保存窗口

    编辑器的模式,可以利用EditorUtility类里面的方式实现,需要引用UnityEditor命名空间。 此种方式只支持在编辑器模式下运行,因为带有UnityEditor命名空间,故发布成exe之后不能使用。 首先需要引用 System.Windows.Forms 命名空间。 引用System.Windows.Forms报错 报错的是原因是缺少System

    2024年02月10日
    浏览(29)
  • Unity3d C#利用Editor编辑器拓展实现配置UI背景样式一键设置UI背景样式功能(含源码)

    在开发UI滚动列表的时候,经常会有每项的背景图不统一的情况,会间隔重复的情况居多。这种情况下,手动去设置间隔一行的背景图或者颜色是比较麻烦的。在此背景下,笔者尝试写个小工具,在搭建UI时配置一下循环背景的样式,可以通过一键点击后设置UI背景的样式,省

    2024年02月03日
    浏览(38)
  • Unity实现读取Excel文件

    我们都知道Unity有自带的类textAsset可以简单地读取Text文本的内容。但在实际的开发过程中,我们不可避免地会与excel 或者 json这些文件类型打交道,今天也是花了点时间,整理出来了如何简单地实现读取excel文档的功能。 github地址:github项目地址 本人个人博客:wyryyds.github.

    2024年02月11日
    浏览(76)
  • Unity Editor 编辑器介绍

    Project 窗口 :列出所有的资源 Console 窗口 :日志输出 Hierarchy 窗口 :在Project 窗口选择一个 Scene 场景后,会在这个窗口显示这个构成这个场景的所有游戏对象 Inspector 窗口: 在 Hierarchy窗口 或 Project 窗口中选择任何一个资源,Inspector窗口 显示与这个资源相关的所有属性。 S

    2024年02月16日
    浏览(31)
  • Unity Editor Inspector界面编辑

    一、继承MonoBehaviour 1、[Header(\\\" \\\")] 标题 2、[Tooltip(\\\" \\\")] 如果鼠标光标是在字段上,显示的说明文本 3、[Range(0, 5)] 将int、float变量限制在特定范围内,滑动条 4、[Multiline] 字符串多行文本显示 5、[TextArea(2, 5)] 字符串多行文本显示,可以设置可现实的最大值和最小值的行数

    2024年02月08日
    浏览(31)
  • 【Unity实战】实现强大通用易扩展的对话系统(附项目源码)

    之前的对话系统因为存在一些错误和原作者不允许我分享,所以被我下架了,而且之前对话系统确实少了一些功能,比如最基本的逐字打印功能,原本来是打算后面补充的。 对话系统在游戏中实现太常见了,所以我又重新去找了一些对话系统的课程进行学习,把实现过程和笔

    2024年02月05日
    浏览(32)
  • 记录使用注入的方式为Unity编辑器实现扩展能力

    使用场景 当前项目编辑器中不方便存放或者提交扩展代码 相同的扩展功能需要在多个项目(编辑器)中使用 项目开发中,偶尔临时需要使用一个功能,想随时使用随时卸载 设计思路 使用进程注入,将一个 c/c++ dll 注入到当前运行的unity编辑器中 使用 c/c++ dll 调用 mono 的函数接

    2024年02月15日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包