1. 程式人生 > >unity 檔案更改自動生成.bytes檔案 與 點選play自動讀取指定路徑excel檔案並生成.bytes檔案

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

}