Пример #1
0
        public void LoadExcelFromFolder(string path, string declareDir, string dataDir)
        {
            if (path == null)
            {
                return;
            }
            UnityEngine.Debug.Log("开始加载配置表");
            //遍历文件
            path = path.Replace('\\', '/');
            DirectoryInfo theFolder = new DirectoryInfo(path);

            FileInfo[] fileInfo = theFolder.GetFiles("*.xls*", SearchOption.AllDirectories);
            int        i        = 0;
            int        sum      = fileInfo.Length;

            List <FileInfo> needWriteExcels = new List <FileInfo>();
            List <FileInfo> serverExcels    = new List <FileInfo>();

            needWriteExcels.Clear();

            foreach (FileInfo f in fileInfo)
            {
                i++;
                UnityEditor.EditorUtility.DisplayProgressBar("配置信息", "读取配置表:" + f.Name, (float)i / (float)sum);
                FileAttributes fa = f.Attributes & FileAttributes.Hidden;
                if (fa != FileAttributes.Hidden && f.Name.IndexOf('~') == -1 && !f.Name.Contains("server_"))
                {
                    needWriteExcels.Add(f);
                }

                //server表单独存放
                if (fa != FileAttributes.Hidden && f.Name.IndexOf('~') == -1 && f.Name.Contains("server_"))
                {
                    serverExcels.Add(f);
                }
            }

            try
            {
                _records.Clear();
                CSharpWriter.ClearData();
                int len = needWriteExcels.Count;

                //根据文件大小进行排序,这样能让每个线程的负载比较均衡
                needWriteExcels.Sort((x, y) => { return((int)(y.Length - x.Length)); });

                Thread[] thread = new Thread[CN_MAX_THREAD_NUM];
                Dictionary <int, List <FileInfo> > xlsxFileDict = new Dictionary <int, List <FileInfo> >();

                for (int dataIndex = 0; dataIndex < len; dataIndex++)
                {
                    AddToDict(xlsxFileDict, dataIndex % CN_MAX_THREAD_NUM, needWriteExcels[dataIndex]);
                }
                _threadCount = xlsxFileDict.Count;

                foreach (var index in xlsxFileDict.Keys)
                {
                    thread[index]      = new Thread(new ThreadStart(() => LoadExcelWriteCs(xlsxFileDict[index], declareDir, dataDir)));
                    thread[index].Name = index.ToString();
                    thread[index].Start();
                }

                // 让主线程等待导表执行完成然后生成工程
                while (_records.Count != len)
                {
                    UnityEditor.EditorUtility.DisplayProgressBar("配置信息", string.Format("请稍等,正在努力把表格导出为代码文件... ...{0}/{1}", _records.Count, len), (float)_records.Count / (float)len);
                    Thread.Sleep(1000);
                }

                int readServerCount = 0;
                UnityEditor.EditorUtility.DisplayProgressBar("配置信息", string.Format("请稍等,提取Server表的中文{0}/{1}", readServerCount, serverExcels.Count), 0);
                foreach (var info in serverExcels)
                {
                    PeekStringToLanguageConverter(info);
                    readServerCount++;
                    UnityEditor.EditorUtility.DisplayProgressBar("配置信息", string.Format("请稍等,提取{2}表的中文{0}/{1}", readServerCount, serverExcels.Count, info.Name), readServerCount / (float)serverExcels.Count);
                }

                /*
                 * //打印每个表导出的时间消耗
                 * while (_records.Count > 0)
                 * {
                 *  var kv = _records.Dequeue();
                 *  UnityEngine.Debug.LogError("file:" + kv.Key + ";;;tick:" + kv.Value);
                 * }
                 */
                CSharpWriter.WriteCSharpData(declareDir, dataDir);
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.LogException(ex);
            }
            UnityEditor.EditorUtility.ClearProgressBar();
        }