public override void OnInspectorGUI() { base.OnInspectorGUI(); ExcelMachine machine = target as ExcelMachine; GUILayout.Label("Excel Spreadsheet Settings:", headerStyle); GUILayout.BeginHorizontal(); GUILayout.Label("File:", GUILayout.Width(50)); string path = string.Empty; if (string.IsNullOrEmpty(machine.excelFilePath)) { path = Application.dataPath; } else { path = machine.excelFilePath; } machine.excelFilePath = GUILayout.TextField(path, GUILayout.Width(250)); if (GUILayout.Button("...", GUILayout.Width(20))) { string folder = Path.GetDirectoryName(path); #if UNITY_EDITOR_WIN path = EditorUtility.OpenFilePanel("Open Excel file", folder, "excel files;*.xls;*.xlsx"); #else // for UNITY_EDITOR_OSX path = EditorUtility.OpenFilePanel("Open Excel file", folder, "xls"); #endif if (path.Length != 0) { machine.SpreadSheetName = Path.GetFileName(path); // the path should be relative not absolute one to make it work on any platform. int index = path.IndexOf("Assets"); if (index >= 0) { // set relative path machine.excelFilePath = path.Substring(index); // pass absolute path machine.SheetNames = new ExcelQuery(path).GetSheetNames(); } else { EditorUtility.DisplayDialog("Error", @"Wrong folder is selected. Set a folder under the 'Assets' folder! \n The excel file should be anywhere under the 'Assets' folder", "OK"); return; } } } GUILayout.EndHorizontal(); // Failed to get sheet name so we just return not to make editor on going. if (machine.SheetNames.Length == 0) { EditorGUILayout.Separator(); EditorGUILayout.LabelField("Error: Failed to retrieve the specified excel file."); EditorGUILayout.LabelField("If the excel file is opened, close it then reopen it again."); return; } // spreadsheet name should be read-only EditorGUILayout.TextField("Spreadsheet File: ", machine.SpreadSheetName); EditorGUILayout.Space(); using (new GUILayout.HorizontalScope()) { EditorGUILayout.LabelField("Worksheet: ", GUILayout.Width(100)); machine.CurrentSheetIndex = EditorGUILayout.Popup(machine.CurrentSheetIndex, machine.SheetNames); if (machine.SheetNames != null) { machine.WorkSheetName = machine.SheetNames[machine.CurrentSheetIndex]; } if (GUILayout.Button("Refresh", GUILayout.Width(60))) { // reopen the excel file e.g) new worksheet is added so need to reopen. machine.SheetNames = new ExcelQuery(machine.excelFilePath).GetSheetNames(); // one of worksheet was removed, so reset the selected worksheet index // to prevent the index out of range error. if (machine.SheetNames.Length <= machine.CurrentSheetIndex) { machine.CurrentSheetIndex = 0; string message = "Worksheet was changed. Check the 'Worksheet' and 'Update' it again if it is necessary."; EditorUtility.DisplayDialog("Info", message, "OK"); } } } EditorGUILayout.Separator(); GUILayout.BeginHorizontal(); if (machine.HasColumnHeader()) { if (GUILayout.Button("Update")) { Import(); } if (GUILayout.Button("Reimport")) { Import(true); } } else { if (GUILayout.Button("Import")) { Import(); } } GUILayout.EndHorizontal(); EditorGUILayout.Separator(); DrawHeaderSetting(machine); EditorGUILayout.Separator(); GUILayout.Label("Path Settings:", headerStyle); machine.TemplatePath = EditorGUILayout.TextField("Template: ", machine.TemplatePath); machine.RuntimeClassPath = EditorGUILayout.TextField("Runtime: ", machine.RuntimeClassPath); machine.EditorClassPath = EditorGUILayout.TextField("Editor:", machine.EditorClassPath); //machine.DataFilePath = EditorGUILayout.TextField("Data:", machine.DataFilePath); machine.onlyCreateDataClass = EditorGUILayout.Toggle("Only DataClass", machine.onlyCreateDataClass); EditorGUILayout.Separator(); if (GUILayout.Button("Generate")) { if (string.IsNullOrEmpty(machine.SpreadSheetName) || string.IsNullOrEmpty(machine.WorkSheetName)) { Debug.LogWarning("No spreadsheet or worksheet is specified."); return; } Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + machine.RuntimeClassPath); Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + machine.EditorClassPath); ScriptPrescription sp = Generate(machine); if (sp != null) { Debug.Log("Successfully generated!"); } else { Debug.LogError("Failed to create a script from excel."); } } if (GUI.changed) { EditorUtility.SetDirty(machine); } }
protected virtual void CreateAssetCreationScript(BaseMachine m, ScriptPrescription sp) { Debug.LogWarning("!!! It should be implemented in the derived class !!!"); }
public NewScriptGenerator(ScriptPrescription scriptPrescription) { m_ScriptPrescription = scriptPrescription; }
public override void OnInspectorGUI() { base.OnInspectorGUI(); ExcelMachine machine = target as ExcelMachine; GUILayout.Label("Excel Spreadsheet Settings:", headerStyle); GUILayout.BeginHorizontal(); GUILayout.Label("Template Excel:", GUILayout.Width(90)); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); string path = string.Empty; if (string.IsNullOrEmpty(machine.excelFilePath)) { path = Application.dataPath; } else { path = machine.excelFilePath; } machine.excelFilePath = GUILayout.TextField(path, GUILayout.Width(250)); if (GUILayout.Button("...", GUILayout.Width(20))) { path = OpenExcelFilePanel(path); if (path.Length != 0) { // the path should be relative not absolute one to make it work on any platform. string trimPath = TrimToRelativePath(path); if (!string.IsNullOrEmpty(trimPath)) { // set relative path machine.excelFilePath = trimPath; // pass absolute path machine.SheetNames = new ExcelQuery(path).GetSheetNames(); } else { EditorUtility.DisplayDialog("Error", @"Wrong folder is selected. Set a folder under the 'Assets' folder! \n The excel file should be anywhere under the 'Assets' folder", "OK"); return; } } } GUILayout.EndHorizontal(); // Failed to get sheet name so we just return not to make editor on going. if (machine.SheetNames.Length == 0) { EditorGUILayout.Separator(); EditorGUILayout.LabelField("Error: Failed to retrieve the specified excel file."); EditorGUILayout.LabelField("If the excel file is opened, close it then reopen it again."); return; } // begin: add excel export path GUILayout.BeginHorizontal(); GUILayout.Label("Export Folder:", GUILayout.Width(100)); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); string exportPath = string.Empty; if (string.IsNullOrEmpty(machine.exportFilePath)) { exportPath = Application.dataPath; } else { exportPath = machine.exportFilePath; } machine.exportFilePath = GUILayout.TextField(exportPath, GUILayout.Width(250)); if (GUILayout.Button("...", GUILayout.Width(20))) { string folder = Path.GetDirectoryName(exportPath); #if UNITY_EDITOR_WIN exportPath = EditorUtility.OpenFilePanel("Open Excel file", folder, "excel files;*.xls;*.xlsx"); #else // for UNITY_EDITOR_OSX exportPath = EditorUtility.OpenFolderPanel("Open Excel file", folder, "xls"); #endif if (exportPath.Length != 0) { // the path should be relative not absolute one to make it work on any platform. int index = exportPath.IndexOf("Assets"); if (index >= 0) { // set relative path machine.exportFilePath = exportPath.Substring(index); } else { EditorUtility.DisplayDialog("Error", @"Wrong folder is selected. Set a folder under the 'Assets' folder! \n The excel file should be anywhere under the 'Assets' folder", "OK"); return; } } } GUILayout.EndHorizontal(); EditorGUILayout.Separator(); // end: add excel export path EditorGUILayout.Space(); using (new GUILayout.HorizontalScope()) { EditorGUILayout.LabelField("Worksheet: ", GUILayout.Width(100)); machine.CurrentSheetIndex = EditorGUILayout.Popup(machine.CurrentSheetIndex, machine.SheetNames); if (machine.SheetNames != null) { machine.WorkSheetName = machine.SheetNames[machine.CurrentSheetIndex]; } if (GUILayout.Button("Refresh", GUILayout.Width(60))) { // reopen the excel file e.g) new worksheet is added so need to reopen. machine.SheetNames = new ExcelQuery(machine.excelFilePath).GetSheetNames(); // one of worksheet was removed, so reset the selected worksheet index // to prevent the index out of range error. if (machine.SheetNames.Length <= machine.CurrentSheetIndex) { machine.CurrentSheetIndex = 0; string message = "Worksheet was changed. Check the 'Worksheet' and 'Update' it again if it is necessary."; EditorUtility.DisplayDialog("Info", message, "OK"); } } } EditorGUILayout.Separator(); GUILayout.BeginHorizontal(); if (machine.HasColumnHeader(machine.WorkSheetName)) { if (GUILayout.Button("Update")) { Import(); } if (GUILayout.Button("Reimport")) { Import(true); } } else { if (GUILayout.Button("Import")) { Import(); } } GUILayout.EndHorizontal(); EditorGUILayout.Separator(); ColumnHeaderList curSheetConfig = machine.GetCurrentWorkSheetConfig(); curSheetConfig.IsReuseOtherSheet = EditorGUILayout.Toggle("Is Reuse Other Sheet", curSheetConfig.IsReuseOtherSheet); if (curSheetConfig.IsReuseOtherSheet) { curSheetConfig.ReuseSheetName = EditorGUILayout.TextField("Reuse Sheet Name: ", curSheetConfig.ReuseSheetName); } EditorGUILayout.Separator(); DrawHeaderSetting(machine); EditorGUILayout.Separator(); curSheetConfig.onlyCreatePostProcessClass = EditorGUILayout.Toggle("Only Gen PostProcessor", curSheetConfig.onlyCreatePostProcessClass); // //Generate button // if (GUILayout.Button("Generate Scripts")) { ScriptPrescription sp = Generate(machine); if (sp != null) { Debug.Log("Successfully generated!"); } else { Debug.LogError("Failed to create a script from excel."); } } EditorGUILayout.Separator(); GUILayout.BeginHorizontal(); // //Import Excels // GUILayout.Label("Import Excels:", headerStyle); GUILayout.EndHorizontal(); for (int i = 0; i < machine.importFileList.Count; i++) { GUILayout.BeginHorizontal(); string importFile = machine.importFileList[i]; importFile = GUILayout.TextField(importFile, GUILayout.Width(200)); if (GUILayout.Button("...", GUILayout.Width(20))) { if (string.IsNullOrEmpty(importFile)) { importFile = Application.dataPath; } path = OpenExcelFilePanel(importFile); if (path.Length != 0) { // the path should be relative not absolute one to make it work on any platform. string trimPath = TrimToRelativePath(path); if (!string.IsNullOrEmpty(trimPath)) { // set relative path machine.importFileList[i] = trimPath; } else { EditorUtility.DisplayDialog("Error", @"Wrong folder is selected. Set a folder under the 'Assets' folder! \n The excel file should be anywhere under the 'Assets' folder", "OK"); return; } } } if (GUILayout.Button("-", GUILayout.Width(20))) { machine.importFileList.RemoveAt(i); } if (GUILayout.Button("+", GUILayout.Width(20))) { machine.importFileList.Insert(i + 1, ""); } if (GUILayout.Button("Export", GUILayout.Width(48))) { ImportExcelFile(machine.importFileList[i]); } GUILayout.EndHorizontal(); } if (GUILayout.Button("Export All", GUILayout.Width(80))) { foreach (string filePath in machine.importFileList) { ImportExcelFile(filePath); } } EditorGUILayout.Separator(); GUILayout.Label("Path Settings:", headerStyle); machine.TemplatePath = EditorGUILayout.TextField("Template: ", machine.TemplatePath); machine.RuntimeClassPath = EditorGUILayout.TextField("Runtime: ", machine.RuntimeClassPath); machine.EditorClassPath = EditorGUILayout.TextField("Editor:", machine.EditorClassPath); //machine.DataFilePath = EditorGUILayout.TextField("Data:", machine.DataFilePath); if (GUI.changed) { EditorUtility.SetDirty(machine); //AssetDatabase.SaveAssets(); //AssetDatabase.Refresh(); } }