diff --git a/DungeonShooting_Godot/prefab/ui/RoomUI.tscn b/DungeonShooting_Godot/prefab/ui/RoomUI.tscn index 91c4332..22dc29c 100644 --- a/DungeonShooting_Godot/prefab/ui/RoomUI.tscn +++ b/DungeonShooting_Godot/prefab/ui/RoomUI.tscn @@ -115,12 +115,14 @@ expand_mode = 1 [node name="MapBar" type="TextureRect" parent="Control"] -layout_mode = 0 +layout_mode = 1 +anchors_preset = 1 anchor_left = 1.0 anchor_right = 1.0 offset_left = -176.0 offset_right = -132.0 offset_bottom = 44.0 +grow_horizontal = 0 scale = Vector2(4, 4) texture = ExtResource("2") diff --git a/DungeonShooting_Godot/src/framework/generator/UiManagerMethodsGenerator.cs b/DungeonShooting_Godot/src/framework/generator/UiManagerMethodsGenerator.cs index 643c668..7634384 100644 --- a/DungeonShooting_Godot/src/framework/generator/UiManagerMethodsGenerator.cs +++ b/DungeonShooting_Godot/src/framework/generator/UiManagerMethodsGenerator.cs @@ -11,11 +11,10 @@ public static class UiManagerMethodsGenerator { private static string savePath = "src/game/manager/UiManager_Methods.cs"; - + /// /// 执行生成操作, 并返回执行结果 /// - /// public static bool Generate() { //扫描所有ui @@ -36,31 +35,40 @@ $"{{\n" + $"\n"; + var uiNameClass = $" public static class UiName\n" + + $" {{\n"; + var methodClass = ""; + foreach (var fileInfo in fileInfos) { if (fileInfo.Extension == ".tscn") { - var name = fileInfo.Name.Substring(0, fileInfo.Name.Length - 5); - var field = fileInfo.FullName.Substring(System.Environment.CurrentDirectory.Length + 1); - field = field.Replace("\\", "/"); - field = field.Replace(".", "_"); - field = field.Replace("/", "_"); - field = Regex.Replace(field, "[^\\w]", ""); - code += $" public static UI.{name}.{name}Panel Open_{name}()\n" + - $" {{\n" + - $" return OpenUi(ResourcePath.{field});\n" + - $" }}\n" + - $"\n" + - $" public static UiBase[] Get_{name}_Instance()\n" + - $" {{\n" + - $" return GetUiInstance(nameof(UI.{name}.{name}));\n" + - $" }}\n" + - $"\n"; + var uiName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 5); + uiNameClass += $" public const string {uiName} = \"{uiName}\";\n"; + methodClass += $" /// \n" + + $" /// 打开 {uiName}, 并返回UI实例\n" + + $" /// \n" + + $" public static UI.{uiName}.{uiName}Panel Open_{uiName}()\n" + + $" {{\n" + + $" return OpenUi(UiName.{uiName});\n" + + $" }}\n" + + $"\n" + + $" /// \n" + + $" /// 获取所有 {uiName} 的实例, 如果没有实例, 则返回一个空数组\n" + + $" /// \n" + + $" public static UI.{uiName}.{uiName}Panel[] Get_{uiName}_Instance()\n" + + $" {{\n" + + $" return GetUiInstance(nameof(UI.{uiName}.{uiName}));\n" + + $" }}\n" + + $"\n"; } } + uiNameClass += $" }}\n\n"; + code += uiNameClass; + code += methodClass; code += $"}}\n"; - + File.WriteAllText(savePath, code); } catch (Exception e) diff --git a/DungeonShooting_Godot/src/framework/ui/IUiNode.cs b/DungeonShooting_Godot/src/framework/ui/IUiNode.cs index 31c5e70..2d7ce1c 100644 --- a/DungeonShooting_Godot/src/framework/ui/IUiNode.cs +++ b/DungeonShooting_Godot/src/framework/ui/IUiNode.cs @@ -23,18 +23,25 @@ Instance = node; } - public UiBase OpenNestedUi(string resourcePath, params object[] args) + /// + /// 嵌套打开子ui + /// + public UiBase OpenNestedUi(string uiName, params object[] args) { - var packedScene = ResourceManager.Load(resourcePath); + var packedScene = ResourceManager.Load("res://" + GameConfig.UiPrefabDir + uiName + ".tscn"); var uiBase = packedScene.Instantiate(); Instance.AddChild(uiBase); + uiBase.OnCreateUi(); uiBase.ShowUi(args); return uiBase; } - public T OpenNestedUi(string resourcePath) where T : UiBase + /// + /// 嵌套打开子ui + /// + public T OpenNestedUi(string uiName) where T : UiBase { - return (T)OpenNestedUi(resourcePath); + return (T)OpenNestedUi(uiName); } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/ui/UiManager.cs b/DungeonShooting_Godot/src/framework/ui/UiManager.cs index 354220b..c3b86f1 100644 --- a/DungeonShooting_Godot/src/framework/ui/UiManager.cs +++ b/DungeonShooting_Godot/src/framework/ui/UiManager.cs @@ -111,9 +111,9 @@ /// /// 根据Ui资源路径打开Ui, 并返回Ui实例, 该Ui资源的场景根节点必须继承 /// - public static UiBase OpenUi(string resourcePath, params object[] args) + public static UiBase OpenUi(string uiName, params object[] args) { - var packedScene = ResourceManager.Load(resourcePath); + var packedScene = ResourceManager.Load("res://" + GameConfig.UiPrefabDir + uiName + ".tscn"); var uiBase = packedScene.Instantiate(); var canvasLayer = GetUiLayer(uiBase.Layer); canvasLayer.AddChild(uiBase); @@ -125,9 +125,9 @@ /// /// 根据Ui资源路径打开Ui, 并返回Ui实例, 该Ui资源的场景根节点必须继承 /// - public static T OpenUi(string resourcePath, params object[] args) where T : UiBase + public static T OpenUi(string uiName, params object[] args) where T : UiBase { - return (T)OpenUi(resourcePath, args); + return (T)OpenUi(uiName, args); } /// @@ -141,13 +141,18 @@ /// /// 获取Ui实例 /// - public static UiBase[] GetUiInstance(string uiName) + public static T[] GetUiInstance(string uiName) where T : UiBase { if (_recordUiMap.TryGetValue(uiName, out var list)) { - return list.ToArray(); + var result = new T[list.Count]; + for (var i = 0; i < list.Count; i++) + { + result[i] = (T)list[i]; + } + return result; } - return new UiBase[0]; + return new T[0]; } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/GameApplication.cs b/DungeonShooting_Godot/src/game/GameApplication.cs index 34e1d0e..39f0116 100644 --- a/DungeonShooting_Godot/src/game/GameApplication.cs +++ b/DungeonShooting_Godot/src/game/GameApplication.cs @@ -81,7 +81,7 @@ //打开ui UiManager.Open_RoomUI(); - + RoomManager = ResourceManager.Load(ResourcePath.scene_Room_tscn).Instantiate(); SceneRoot.AddChild(RoomManager); } diff --git a/DungeonShooting_Godot/src/game/manager/UiManager_Methods.cs b/DungeonShooting_Godot/src/game/manager/UiManager_Methods.cs index 38b8f47..91aaa92 100644 --- a/DungeonShooting_Godot/src/game/manager/UiManager_Methods.cs +++ b/DungeonShooting_Godot/src/game/manager/UiManager_Methods.cs @@ -4,24 +4,42 @@ public static partial class UiManager { + public static class UiName + { + public const string EditorTools = "EditorTools"; + public const string RoomUI = "RoomUI"; + } + + /// + /// 打开 EditorTools, 并返回UI实例 + /// public static UI.EditorTools.EditorToolsPanel Open_EditorTools() { - return OpenUi(ResourcePath.prefab_ui_EditorTools_tscn); + return OpenUi(UiName.EditorTools); } - public static UiBase[] Get_EditorTools_Instance() + /// + /// 获取所有 EditorTools 的实例, 如果没有实例, 则返回一个空数组 + /// + public static UI.EditorTools.EditorToolsPanel[] Get_EditorTools_Instance() { - return GetUiInstance(nameof(UI.EditorTools.EditorTools)); + return GetUiInstance(nameof(UI.EditorTools.EditorTools)); } + /// + /// 打开 RoomUI, 并返回UI实例 + /// public static UI.RoomUI.RoomUIPanel Open_RoomUI() { - return OpenUi(ResourcePath.prefab_ui_RoomUI_tscn); + return OpenUi(UiName.RoomUI); } - public static UiBase[] Get_RoomUI_Instance() + /// + /// 获取所有 RoomUI 的实例, 如果没有实例, 则返回一个空数组 + /// + public static UI.RoomUI.RoomUIPanel[] Get_RoomUI_Instance() { - return GetUiInstance(nameof(UI.RoomUI.RoomUI)); + return GetUiInstance(nameof(UI.RoomUI.RoomUI)); } }