unity 文件更改自动生成.bytes文件 与 点击play自动读取指定路径excel文件并生成.bytes文件

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Policy;
using Excel;
using Google.Protobuf;
using NUnit.Framework.Constraints;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

//文件更改自动生成.bytes文件

//AssetPostprocessor  在文件改变时自动返回改变文件路径

public class ConvertExcelToBinary : AssetPostprocessor
{

    static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        if (importedAssets == null || importedAssets.Length == 0) return;

        List<string> allExcels = new List<string>();
        foreach (string str in importedAssets)
        {
            string extension = Path.GetExtension(str);
            string dir = Path.GetFileName(Path.GetDirectoryName(str));
            string fileName = Path.GetFileName(str);
            if (!fileName.StartsWith("~") && extension == ".xlsx" && (dir == "Client" || dir == "Common"))
            {
                allExcels.Add(str);
            }
        }
        foreach (var excel in allExcels)
        {
            Debug.Log("你导入了:" + excel);
        }

        foreach (var excel in allExcels)
        {
            GenerateBinary(excel);
            string fileName = Path.GetFileNameWithoutExtension(excel);
            Debug.Log(fileName + ".bytes 生成至DataDesign");
        }


    }

    

    public static void GenerateBinary(string file)
    {
        string excelAssestPath = "Assets/Resources/DataDesign/";
        String targetPath = Path.Combine(excelAssestPath, Path.GetFileNameWithoutExtension(file) + ".bytes");

        if (File.Exists(targetPath))
            File.Delete(targetPath);
        try
        {
            FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

           //ExcelDataSet 、ExcelDataTable 、ExcelDataRow 均为proto中定义的消息

//message ExcelDataSet
//{
//repeated ExcelDataTable tables =1;
//}

//message ExcelDataTable
//{

//repeated ExcelDataRow rows =1;

//}

//message ExcelDataRow
//{

//repeated string cells =1;
//}

            ExcelDataSet set = new ExcelDataSet();
            do
            {
                ExcelDataTable table = new ExcelDataTable();
                // sheet name   
                while (excelReader.Read())
                {
                    ExcelDataRow row = new ExcelDataRow();
                    for (int i = 0; i < excelReader.FieldCount; i++)
                    {
                        string value = excelReader.IsDBNull(i) ? "" : excelReader.GetString(i);
                        row.Cells.Add(value);
                    }
                    table.Rows.Add(row);
                }
                if (table.Rows.Count > 0)
                    set.Tables.Add(table);

            } while (excelReader.NextResult());
            var bytes = set.ToByteArray();  //Proto中的生成bytes方法
            File.WriteAllBytes(targetPath, bytes);
        }
        catch (IOException ex)
        {

        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    }

}

//点击play自动读取指定路径excel文件并生成.bytes文件

[InitializeOnLoad]
public static class ReimportAllExecle
{
    private const string CLIENT_EXCLES_PATH = "Assets/Excels/Client";
    private const string COMMON_EXCLES_PATH = "Assets/Excels/Common";
    static ReimportAllExecle()
    {
       // return;
        List<string> CurrentExcels = new List<string>();

        if (Directory.Exists(CLIENT_EXCLES_PATH))
        {
            DirectoryInfo direction = new DirectoryInfo(CLIENT_EXCLES_PATH);
            FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

            Debug.Log(files.Length);

            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name.EndsWith(".meta"))
                {
                    continue;
                }
                if(files[i].Name.EndsWith(".csv"))
                {
                    continue;
                }
              // Debug.LogError("Name:" + files[i].Name); //打印出来这个文件架下的所有文件
               CurrentExcels.Add(files[i].DirectoryName + "/" + files[i].Name);
            }
        }
        if (Directory.Exists(COMMON_EXCLES_PATH))
        {
            DirectoryInfo direction = new DirectoryInfo(COMMON_EXCLES_PATH);
            FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

            Debug.Log(files.Length);

            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name.EndsWith(".meta"))
                {
                    continue;
                }
                if (files[i].Name.EndsWith(".csv"))
                {
                    continue;
                }
                // Debug.LogError("Name:" + files[i].Name); //打印出来这个文件架下的所有文件
                CurrentExcels.Add(files[i].DirectoryName + "/" + files[i].Name);
            }
        }
        foreach (var excel in CurrentExcels)
        {
            ConvertExcelToBinary.GenerateBinary(excel);
            string fileName = Path.GetFileNameWithoutExtension(excel);
            Debug.Log(fileName + ".bytes 生成至DataDesign");
        }
    }

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值