Exemple #1
0
        /// <summary>
        /// 从单一xlsx文件中获取数据结构并生成Config代码
        /// </summary>
        /// <param name="filePath"></param>
        private static void GenSingleFile(string filePath)
        {
            var package = new ExcelPackage(new FileInfo(filePath));
            var ws      = package.Workbook.Worksheets["Data"];

            if (ws == null)
            {
                throw new InvalidOperationException("xls文件中没有 'Data' Sheet页");
            }
            var templatePath = Path.Combine(Application.dataPath, "Framework/Editor/Data/ConfigTemplate.txt");

            if (!File.Exists(templatePath))
            {
                throw new FileNotFoundException("模板文件不存在: " + templatePath);
            }
            var tableName    = Path.GetFileNameWithoutExtension(filePath);
            var fieldDeclare = new StringBuilder();
            var fieldInit    = new StringBuilder();
            var hash         = new List <string>();
            var columnIdx    = 0;

            while (true)
            {
                columnIdx += 1;
                var fieldName = ws.Cells[2, columnIdx].Value?.ToString();
                if (string.IsNullOrWhiteSpace(fieldName))
                {
                    break;
                }
                var fieldType = ws.Cells[3, columnIdx].Value?.ToString();
                var fieldDesc = ws.Cells[1, columnIdx].Value?.ToString();
                hash.Add(fieldName);
                hash.Add(fieldType);
                hash.Add(fieldDesc);
                BuildFieldCode(fieldDeclare, fieldInit, fieldName, fieldType, fieldDesc);
            }
            var dst        = Path.Combine(Application.dataPath, $"Scripts/Config/{tableName}Config.cs");
            var customCode = new StringBuilder();
            var oldHash    = string.Empty;

            if (File.Exists(dst))
            {
                var oldContent = File.ReadAllLines(dst);
                var reading    = false;
                foreach (var line in oldContent)
                {
                    if (line.StartsWith("    /// Hash:"))
                    {
                        oldHash = line.Replace("    /// Hash:", "").Trim();
                    }
                    if (line.Trim().StartsWith("#region ====="))
                    {
                        reading = true;
                        continue;
                    }
                    if (line.Trim().StartsWith("#endregion ====="))
                    {
                        reading = false;
                        continue;
                    }
                    if (!reading)
                    {
                        continue;
                    }
                    customCode.Append(line);
                    customCode.Append('\n');
                }
            }

            var hashString = hash.CalcHash();

            if (hashString == oldHash)
            {
                // 表结构没变化
                Logger.LogInfo(LogModule.Data, $"{tableName}没有变化已跳过");
                return;
            }
            // 删去最后一个换行符
            if (fieldDeclare.Length > 0)
            {
                fieldDeclare.Remove(fieldDeclare.Length - 1, 1);
            }
            if (fieldInit.Length > 0)
            {
                fieldInit.Remove(fieldInit.Length - 1, 1);
            }
            if (customCode.Length > 0)
            {
                customCode.Remove(customCode.Length - 1, 1);
            }

            var template = File.ReadAllText(templatePath);

            template = template.Replace("#Hash#", hashString);
            template = template.Replace("#GenTime#", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            template = template.Replace("#TableName#", tableName);
            template = template.Replace("#FieldDeclare#", fieldDeclare.ToString());
            template = template.Replace("#FieldInit#", fieldInit.ToString());
            template = template.Replace("#CustomCode#", customCode.ToString());
            File.WriteAllText(dst, template, Encoding.UTF8);
            ScriptHeaderGenerator.OnWillCreateAsset(dst + ".meta");
        }
Exemple #2
0
        internal static void ExportUi()
        {
            var exportPath = Application.dataPath + ProjectSettings.Instance.UiExportPath;

            if (exportPath == string.Empty)
            {
                EditorUtility.DisplayDialog("错误", "请先在Instech/Preferences中设置UI导出路径", "OK");
                return;
            }
            var prefab                  = Selection.activeGameObject;
            var viewName                = prefab.name.Substring(2);
            var viewContentBuilder      = new StringBuilder();
            var presenterContentBuilder = new StringBuilder();
            var components              = new Dictionary <string, Type>();

            // Primary Method
            GenerateContent(prefab, viewContentBuilder, presenterContentBuilder, components);

            var baseFilePath = $"{exportPath.TrimEnd('/', '\\')}/{viewName}";

            var viewFilePath = baseFilePath + "View.cs";

            File.WriteAllText(viewFilePath, viewContentBuilder.ToString(), new UTF8Encoding(false));
            ScriptHeaderGenerator.OnWillCreateAsset(viewFilePath);

            var presenterFilePath = baseFilePath + "Presenter.cs";

            if (!File.Exists(presenterFilePath))
            {
                // 不存在presenter的话才会生成代码
                File.WriteAllText(presenterFilePath, presenterContentBuilder.ToString(), new UTF8Encoding(false));
                ScriptHeaderGenerator.OnWillCreateAsset(presenterFilePath);
            }

            AssetDatabase.Refresh();

            // 给prefab挂载脚本
            var componentName = viewName + "View";
            var component     = prefab.GetComponent(componentName);
            var viewType      = GetTypeByName(componentName);

            if (component == null)
            {
                if (viewType == null)
                {
                    EditorUtility.DisplayDialog("快完成了", "第一次导出,需要等待编译后再次执行一次!", "OK");
                    Logger.LogInfo(LogModule.Editor, $"Generated code for view: {viewName}");
                    return;
                }
                component = prefab.AddComponent(viewType);
            }
            else
            {
                UnityEngine.Object.DestroyImmediate(component, true);
                component = prefab.AddComponent(viewType);
            }
            var needAgain = UpdateComponentReference(component, components);

            if (needAgain)
            {
                EditorUtility.DisplayDialog("快完成了", "有新的控件,需要等待编译后再次执行一次!", "OK");
            }
            else
            {
                EditorUtility.DisplayDialog("完成", $"导出完成,该View有{components.Count}个控件!", "OK");
            }
            Logger.LogInfo(LogModule.Editor, $"Generated code for view: {viewName}");
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }