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");
}
}
}