Browse Source

关卡地图大小和出怪表相对应

wgl 6 tháng trước cách đây
mục cha
commit
d427fd7ba6

+ 10 - 2
ActionTowerDefense/Assets/GameLevelEditor/GameMap/CoreScripts/Editor/GameMapEditor.cs

@@ -134,6 +134,8 @@ public class GameMapEditor : EditorWindow
 	private static readonly GUIContent GC_EditAuto = new GUIContent("Setup Auto-tile");
     private static readonly GUIContent GC_Viz = new GUIContent("*", "Toggle layer visblity in editor");
 
+	private static string enemyExcelPath = "Luban/Config/Datas/出怪表.xlsx";
+
     private GenericMenu addTileMenu = null;
 
 	private static readonly Dictionary<int, int> map64 = new Dictionary<int, int>()
@@ -620,11 +622,16 @@ public class GameMapEditor : EditorWindow
 			r.x = r.xMax - 200f; r.width = 100f; r.height = 15f;
             if (GUI.Button(r, GC_ExcelReload))
             {
-				List<string> excelWorksheets = ExcelEditor.ReadExcelSheetsInfo("Luban/Config/Datas/出怪表.xlsx");
+				List<string> excelWorksheets = ExcelEditor.ReadExcelSheetsInfo(enemyExcelPath);
 				for (int i = 0; i < excelWorksheets.Count; i++)
 				{
 					asset.maps[i].ident = excelWorksheets[i];
+					int.TryParse(ExcelEditor.GetCellData(enemyExcelPath, excelWorksheets[i], "C6"), out int width);
+					int.TryParse(ExcelEditor.GetCellData(enemyExcelPath, excelWorksheets[i], "D6"), out int height);
+					asset.maps[i].Resize(width, height);
 				}
+				mapSize_w = asset.maps[mapIdx].width;
+				mapSize_h = asset.maps[mapIdx].height;
 			}
 			r.x = r.xMax; r.width = 100f; r.height = 15f;
 			if (GUI.Button(r, GC_MapSelect))
@@ -662,10 +669,11 @@ public class GameMapEditor : EditorWindow
 						mapSize_h = EditorGUILayout.IntField(mapSize_h, GUILayout.Width(35));
 						if (GUILayout.Button(GC_apply, EditorStyles.miniButtonRight))
 						{
-							Undo.RecordObject(asset, "Resize map");
 							asset.maps[mapIdx].Resize(mapSize_w, mapSize_h);
 							mapSize_w = asset.maps[mapIdx].width;
 							mapSize_h = asset.maps[mapIdx].height;
+							ExcelEditor.ModifyExcel(enemyExcelPath, asset.maps[mapIdx].ident, "C6", mapSize_w);
+							ExcelEditor.ModifyExcel(enemyExcelPath, asset.maps[mapIdx].ident, "D6", mapSize_h);
 						}
 						GUILayout.FlexibleSpace();
 					}

+ 29 - 9
ActionTowerDefense/Assets/Scripts/ExcelEditor.cs

@@ -32,15 +32,35 @@ public class ExcelEditor
         using (ExcelPackage package = new ExcelPackage(fileInfo))
         {
             ExcelWorksheet worksheet = package.Workbook.Worksheets[oldSheetName];
-            if (worksheet != null)
-            {
-                worksheet.Name = newSheetName;
-                package.Save();
-            }
-            else
-            {
-                Debug.LogError($"Sheet '{oldSheetName}' not found in the Excel file.");
-            }
+            worksheet.Name = newSheetName;
+            package.Save();
+        }
+    }
+
+    //获取excel数据
+    public static string GetCellData(string filePath, string sheetName, string cellId)
+    {
+        FileInfo fileInfo = new FileInfo(filePath);
+
+        using (ExcelPackage package = new ExcelPackage(fileInfo))
+        {
+            ExcelWorksheet worksheet = package.Workbook.Worksheets[sheetName];
+            var value = worksheet.Cells[cellId].Value;
+            return value?.ToString() ?? string.Empty;
+        }
+    }
+
+    //修改excel文件
+    public static void ModifyExcel(string filePath, string sheetMame, string cellId, int newData)
+    {
+        FileInfo fileInfo = new FileInfo(filePath);
+
+
+        using (ExcelPackage package = new ExcelPackage(fileInfo))
+        {
+            ExcelWorksheet worksheet = package.Workbook.Worksheets[sheetMame];
+            worksheet.Cells[cellId].Value = newData; // 通过单元格地址
+            package.Save();
         }
     }
 }

BIN
ActionTowerDefense/Luban/Config/Datas/出怪表.xlsx