修复BUG

This commit is contained in:
MuHua-123
2025-09-13 15:48:27 +08:00
parent 91a40e097b
commit 63b30ebd03
5 changed files with 60 additions and 141 deletions
@@ -19,11 +19,16 @@ public class InputMenu : InputControl {
/// <summary> 鼠标左键 </summary>
public void OnMouseLeft(InputValue inputValue) {
if (inputValue.isPressed) return;
ManagerMenu.I.Close();
UIShortcutMenu.I.Close();
}
/// <summary> 鼠标右键 </summary>
public void OnMouseRight(InputValue inputValue) {
ManagerMenu.I.Open();
UIShortcutMenu.I.Add("测试1/测试11", () => { Debug.Log("测试1/测试11"); });
UIShortcutMenu.I.Add("测试1/测试12", () => { Debug.Log("测试1/测试12"); });
UIShortcutMenu.I.Add("测试2", () => { Debug.Log("测试2"); });
UIShortcutMenu.I.Open();
}
#endregion
}
@@ -1,81 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MuHua;
/// <summary>
/// 菜单管理器
/// </summary>
public class ManagerMenu : ModuleSingle<ManagerMenu> {
private List<DataMenuItem> datas = new List<DataMenuItem>();
protected override void Awake() => NoReplace(false);
private void Start() {
// 初始化菜单项数据
Add("建筑/伐木场", () => Debug.Log("伐木场"));
Add("建筑/农场", () => Debug.Log("农场"));
Add("建筑/矿场", () => Debug.Log("矿场"));
Add("建筑/房屋/木屋", () => Debug.Log("木屋"));
Add("建筑/房屋/豪宅", () => Debug.Log("豪宅"));
Add("单位/步兵", () => Debug.Log("步兵"));
Add("单位/骑兵", () => Debug.Log("骑兵"));
Add("拆除", () => Debug.Log("拆除"));
}
public void Open() => UIShortcutMenu.I.Open(datas);
public void Close() => UIShortcutMenu.I.Close();
/// <summary> 添加菜单项(方法) </summary>
public void Add(string name, Action callback) {
string[] names = name.Split('/');
List<DataMenuItem> datas = this.datas;
for (int i = 0; i < names.Length; i++) {
string menu = names[i];
DataMenuItem item = datas.Find(obj => obj.name == menu);
if (item == null) {
item = new DataMenuItem { name = menu };
if (i == names.Length - 1) { item.callback = callback; }
datas.Add(item);
}
datas = item.items;
}
}
/// <summary> 移除菜单项 </summary>
public void Remove(string name) {
string[] names = name.Split('/');
List<DataMenuItem> datas = this.datas;
DataMenuItem parent = null;
DataMenuItem target = null;
for (int i = 0; i < names.Length; i++) {
string menu = names[i];
target = datas.Find(obj => obj.name == menu);
if (target == null) return; // 未找到,直接返回
if (i == names.Length - 1) {
// 找到要移除的项
datas.Remove(target);
return;
}
parent = target;
datas = target.items;
}
}
}
/// <summary>
/// 菜单项目
/// </summary>
public class DataMenuItem {
/// <summary> 名称 </summary>
public string name;
/// <summary> 回调 </summary>
public Action callback;
/// <summary> 子菜单项 </summary>
public List<DataMenuItem> items = new List<DataMenuItem>();
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 4fbad1b7bdd1ba34da55091f94b08438
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -13,6 +13,9 @@ public class UIShortcutMenu : ModuleUISingle<UIShortcutMenu> {
public VisualTreeAsset menuTreeAsset;
/// <summary> 项目模板 </summary>
public VisualTreeAsset itemTreeAsset;
/// <summary> 数据列表 </summary>
public List<DataMenuItem> datas = new List<DataMenuItem>();
/// <summary> 控件列表 </summary>
public static List<UIControl> controls = new List<UIControl>();
@@ -25,7 +28,7 @@ public class UIShortcutMenu : ModuleUISingle<UIShortcutMenu> {
private void OnDestroy() => controls.ForEach(control => control.Dispose());
/// <summary> 打开菜单 </summary>
public void Open(List<DataMenuItem> datas) {
public void Open() {
Close();
Vector3 position = UITool.GetMousePosition(Element);
UIMenuPanel menuPanel = Create();
@@ -48,6 +51,43 @@ public class UIShortcutMenu : ModuleUISingle<UIShortcutMenu> {
return menuPanel;
}
/// <summary> 添加菜单项(方法) </summary>
public void Add(string name, Action callback) {
string[] names = name.Split('/');
List<DataMenuItem> datas = this.datas;
for (int i = 0; i < names.Length; i++) {
string menu = names[i];
DataMenuItem item = datas.Find(obj => obj.name == menu);
if (item == null) {
item = new DataMenuItem { name = menu };
if (i == names.Length - 1) { item.callback = callback; }
datas.Add(item);
}
datas = item.items;
}
}
/// <summary> 移除菜单项 </summary>
public void Remove(string name) {
string[] names = name.Split('/');
List<DataMenuItem> datas = this.datas;
DataMenuItem parent = null;
DataMenuItem target = null;
for (int i = 0; i < names.Length; i++) {
string menu = names[i];
target = datas.Find(obj => obj.name == menu);
if (target == null) return; // 未找到,直接返回
if (i == names.Length - 1) {
// 找到要移除的项
datas.Remove(target);
return;
}
parent = target;
datas = target.items;
}
}
/// <summary> 添加控件 </summary>
public static void AddControl(UIControl control) => controls.Add(control);
/// <summary> 移除控件 </summary>
@@ -121,3 +161,14 @@ public class UIMenuPanel : ModuleUIPanel, UIControl {
}
}
}
/// <summary>
/// 菜单项目
/// </summary>
public class DataMenuItem {
/// <summary> 名称 </summary>
public string name;
/// <summary> 回调 </summary>
public Action callback;
/// <summary> 子菜单项 </summary>
public List<DataMenuItem> items = new List<DataMenuItem>();
}
@@ -534,8 +534,7 @@ Transform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1646925906}
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1439912284
@@ -689,50 +688,6 @@ MonoBehaviour:
m_EditorClassIdentifier:
document: {fileID: 670296963}
SlideButtonTemplate: {fileID: 9197481963319205126, guid: 9452bd34d4b5bff4084d975753638b86, type: 3}
--- !u!1 &1646925905
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1646925906}
- component: {fileID: 1646925907}
m_Layer: 0
m_Name: ManagerMenu
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1646925906
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1646925905}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1439912283}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1646925907
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1646925905}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4fbad1b7bdd1ba34da55091f94b08438, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1922845062
GameObject:
m_ObjectHideFlags: 0