(client) feat:实现摄像机跟踪与移动,实现任意位置生成实体,实现更安全的资源加载方式(指定unity内部加载资源) #42

Merged
TheRedApricot merged 4 commits from zzdxxz/Gen_Hack-and-Slash-Roguelite-zzdxxz:temp725 into main 2025-08-07 16:44:46 +08:00
42 changed files with 4748 additions and 7296 deletions
Showing only changes of commit 3c9be3a30c - Show all commits

View File

@ -384,6 +384,142 @@ Transform:
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &465209085
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 465209086}
- component: {fileID: 465209088}
- component: {fileID: 465209087}
m_Layer: 5
m_Name: prompt
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &465209086
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 465209085}
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: 1672332560}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 110, y: -160}
m_SizeDelta: {x: 200, y: 300}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &465209087
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 465209085}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text: "\u8BF4\u660E\u6587\u672C"
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 317edb274e9c5144a9916937bdbf7716, type: 2}
m_sharedMaterial: {fileID: -1361428157011412921, guid: 317edb274e9c5144a9916937bdbf7716, type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 2533359615
m_fontColor: {r: 1, g: 1, b: 1, a: 0.5882353}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 12
m_fontSizeBase: 12
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 1
m_HorizontalAlignment: 1
m_VerticalAlignment: 256
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_TextWrappingMode: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 0
m_ActiveFontFeatures: 6e72656b
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_EmojiFallbackSupport: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!222 &465209088
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 465209085}
m_CullTransparentMesh: 1
--- !u!1 &603423466 --- !u!1 &603423466
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1205,6 +1341,142 @@ MonoBehaviour:
needPause: 1 needPause: 1
isInputOccupied: 0 isInputOccupied: 0
actionButton: 27 actionButton: 27
--- !u!1 &1620768398
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1620768399}
- component: {fileID: 1620768401}
- component: {fileID: 1620768400}
m_Layer: 5
m_Name: Text (TMP) (1)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1620768399
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1620768398}
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: 1672332560}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 1}
m_AnchorMax: {x: 0.5, y: 1}
m_AnchoredPosition: {x: 0, y: -15}
m_SizeDelta: {x: 200, y: 20}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1620768400
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1620768398}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text: "\u6309Esc\u8FD4\u56DE\uFF0C\u70B9\u51FB\u4EFB\u610F\u4F4D\u7F6E\u751F\u6210"
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 317edb274e9c5144a9916937bdbf7716, type: 2}
m_sharedMaterial: {fileID: -1361428157011412921, guid: 317edb274e9c5144a9916937bdbf7716, type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 2533359615
m_fontColor: {r: 1, g: 1, b: 1, a: 0.5882353}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 12
m_fontSizeBase: 12
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 1
m_HorizontalAlignment: 2
m_VerticalAlignment: 512
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_TextWrappingMode: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 0
m_ActiveFontFeatures: 6e72656b
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_EmojiFallbackSupport: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!222 &1620768401
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1620768398}
m_CullTransparentMesh: 1
--- !u!1 &1672332559 --- !u!1 &1672332559
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1235,6 +1507,8 @@ RectTransform:
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: m_Children:
- {fileID: 1988809572} - {fileID: 1988809572}
- {fileID: 1620768399}
- {fileID: 465209086}
m_Father: {fileID: 1236970686} m_Father: {fileID: 1236970686}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
@ -1258,6 +1532,7 @@ MonoBehaviour:
needPause: 1 needPause: 1
isInputOccupied: 1 isInputOccupied: 1
actionButton: 0 actionButton: 0
promptText: {fileID: 465209087}
--- !u!1 &1891846098 --- !u!1 &1891846098
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1652,6 +1927,10 @@ PrefabInstance:
propertyPath: actionButton propertyPath: actionButton
value: 282 value: 282
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 7904664609186573076, guid: 72cde32427f7d914692a7b0d22fb791d, type: 3}
propertyPath: entityPlacementUI
value:
objectReference: {fileID: 1672332561}
- target: {fileID: 8099462081536526843, guid: 72cde32427f7d914692a7b0d22fb791d, type: 3} - target: {fileID: 8099462081536526843, guid: 72cde32427f7d914692a7b0d22fb791d, type: 3}
propertyPath: m_AnchorMax.x propertyPath: m_AnchorMax.x
value: 1 value: 1

View File

@ -8,214 +8,170 @@ using Object = UnityEngine.Object;
namespace Base namespace Base
{ {
/// <summary>
/// UI窗口输入控制和管理类
/// 负责根据输入显示/隐藏UI并根据UI状态管理游戏暂停。
/// </summary>
public class UIInputControl : Utils.MonoSingleton<UIInputControl>, ITickUI public class UIInputControl : Utils.MonoSingleton<UIInputControl>, ITickUI
{ {
public Dictionary<KeyCode, UIBase> UIwindowKeys = new(); // 存储场景中所有UIBase的实例
private List<UIBase> noKeyWindows = new(); private List<UIBase> _allWindows = new List<UIBase>();
private List<UIBase> allWindows = new(); // 新增:所有窗口的集合 // 缓存当前可见的窗口
private readonly List<UIBase> _visibleWindows = new List<UIBase>();
private bool needUpdate = false;
/// <summary>
/// 查找并注册场景中所有的UI窗口包括非激活状态的
/// </summary>
private void RegisterAllWindows()
{
_allWindows.Clear();
// 获取当前活动场景中的所有 GameObject
var activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
if (!activeScene.isLoaded)
{
Debug.LogWarning("当前场景未加载,无法注册窗口!");
return;
}
// 遍历场景中的所有根对象,并查找其子对象中的 UIBase
foreach (var rootGameObject in activeScene.GetRootGameObjects())
{
var windows = rootGameObject.GetComponentsInChildren<UIBase>(true);
_allWindows.AddRange(windows);
}
// 去重(如果有重复的窗口)
_allWindows = _allWindows.Distinct().ToList();
// 初始化所有窗口为隐藏状态
foreach (var window in _allWindows)
{
if (window != null && window.gameObject != null)
{
window.Hide();
}
}
needUpdate = true;
Debug.Log($"窗口数量{_allWindows.Count}");
}
/// <summary>
/// UI逻辑更新循环需要被外部的某个管理器在Update中调用
/// </summary>
public void TickUI() public void TickUI()
{ {
foreach (var kvp in UIwindowKeys) //使用这个是为了让输入独占窗口关闭自己后不会立即激活其他窗口的按键,延迟一帧
if (needUpdate)
{ {
if (Input.GetKeyDown(kvp.Key)) // 更新可见窗口缓存和暂停状态
{ UpdateVisibleWindowsCache();
HandleWindowActivation(kvp.Value); UpdatePauseState();
break; needUpdate = false;
} return;
} }
} if(_visibleWindows.Any(window => window.isInputOccupied))
return;
private void Init() foreach (var window in _allWindows)
{
UIwindowKeys.Clear();
noKeyWindows.Clear();
allWindows.Clear(); // 清空所有窗口集合
var uiInstances = Resources.FindObjectsOfTypeAll<UIBase>();
foreach (var uiBase in uiInstances)
{ {
allWindows.Add(uiBase); // 添加到所有窗口集合 // 检查窗口是否设置了有效的激活按键,并且该按键在本帧被按下
if (window.actionButton == KeyCode.None || !Input.GetKeyDown(window.actionButton)) continue;
var key = uiBase.actionButton;
if (key == KeyCode.None)
{
noKeyWindows.Add(uiBase);
uiBase.Hide();
continue;
}
if (UIwindowKeys.ContainsKey(key))
{
Debug.LogWarning($"Key '{key}' is already assigned to another window. Skipping...");
continue;
}
UIwindowKeys[key] = uiBase;
uiBase.Hide();
}
}
private void HandleWindowActivation(UIBase targetWindow)
{
bool wasTargetVisible = targetWindow.IsVisible;
bool shouldCloseExclusive = false;
bool exclusiveWindowWasOpen = false;
// 第一次遍历:检查是否有需要关闭的独占窗口
foreach (var window in allWindows)
{
if (window == targetWindow) continue;
if (window.IsVisible && window.exclusive)
{
// 记录有独占窗口打开且需要关闭
shouldCloseExclusive = true;
exclusiveWindowWasOpen = true;
break;
}
}
// 第二次遍历:根据条件关闭窗口
foreach (var window in allWindows)
{
if (window == targetWindow) continue;
if (window.IsVisible) if (window.IsVisible)
{ {
// 关闭所有独占窗口(无论是新窗口打开还是关闭) // 如果窗口当前是可见的,且未占用输入,则通过按键隐藏它
// 或当目标窗口是独占窗口时关闭所有其他窗口 if (!window.isInputOccupied)
if (window.exclusive || targetWindow.exclusive || shouldCloseExclusive)
{ {
window.Hide(); Hide(window);
} }
} }
else
{
// 如果窗口当前是隐藏的,则显示它
Show(window);
}
} }
// 切换目标窗口状态
if (wasTargetVisible)
{
targetWindow.Hide();
}
else
{
targetWindow.Show();
}
// 更新暂停状态(优化版)
UpdatePauseState(exclusiveWindowWasOpen, targetWindow);
} }
private void UpdatePauseState(bool exclusiveWindowWasOpen, UIBase targetWindow) /// <summary>
/// 公开的显示窗口方法
/// </summary>
/// <param name="windowToShow">要显示的窗口</param>
public void Show(UIBase windowToShow)
{ {
bool needPause = false; if (!windowToShow || windowToShow.IsVisible) return;
foreach (var window in allWindows) // 如果窗口是独占的,隐藏所有其他窗口
if (windowToShow.exclusive)
{ {
if (window.IsVisible && window.needPause) List<UIBase> windowsToHide = new List<UIBase>(_visibleWindows);
foreach (var visibleWindow in windowsToHide)
{ {
needPause = true; Hide(visibleWindow);
break;
} }
} }
// 只在状态改变时更新 // 显示目标窗口并更新缓存与暂停状态
if (Base.Clock.Instance.Pause != needPause) windowToShow.Show();
{ needUpdate = true;
Base.Clock.Instance.Pause = needPause;
}
} }
/// <summary> /// <summary>
/// 模拟按键输入切换窗口 /// 公开的隐藏窗口方法
/// </summary> /// </summary>
/// <param name="keyCode">要模拟的按键</param> /// <param name="windowToHide">要隐藏的窗口</param>
public void SimulateKeyPress(KeyCode keyCode) public void Hide(UIBase windowToHide)
{ {
if (UIwindowKeys.TryGetValue(keyCode, out UIBase targetWindow)) if (!windowToHide || !windowToHide.IsVisible) return;
{
HandleWindowActivation(targetWindow); // 调用内部逻辑处理 // 隐藏目标窗口并更新缓存与暂停状态
} windowToHide.Hide();
else needUpdate = true;
{
Debug.LogWarning($"No window is assigned to the key '{keyCode}'.");
}
} }
/// <summary> /// <summary>
/// 打开指定的窗口(无论是否有激活键) /// 根据当前所有可见窗口的 needPause 属性来更新游戏时钟的暂停状态
/// </summary> /// </summary>
/// <param name="window">要打开的窗口</param> private void UpdatePauseState()
public void OpenWindow(UIBase window)
{ {
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window))) bool shouldPause = _visibleWindows.Any(w => w.needPause);
if (Base.Clock.Instance.Pause != shouldPause)
{ {
Debug.LogWarning("Cannot open the specified window as it is not registered."); Base.Clock.Instance.Pause = shouldPause;
return;
} }
HandleWindowActivation(window); // 调用内部逻辑处理,标记为函数调用
} }
/// <summary> /// <summary>
/// 关闭指定的窗口(无论是否有激活键) /// 更新当前可见窗口的缓存列表
/// </summary> /// </summary>
/// <param name="window">要关闭的窗口</param> private void UpdateVisibleWindowsCache()
public void CloseWindow(UIBase window)
{ {
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window))) _visibleWindows.Clear();
foreach (var window in _allWindows)
{ {
Debug.LogWarning("Cannot close the specified window as it is not registered."); if (window.IsVisible)
return; {
_visibleWindows.Add(window);
}
} }
HandleWindowActivation(window); // 调用内部逻辑处理,标记为函数调用
} }
/// <summary>
/// 切换指定窗口的显示状态(无论是否有激活键)
/// </summary>
/// <param name="window">要切换的窗口</param>
public void ToggleWindow(UIBase window)
{
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
{
Debug.LogWarning("Cannot toggle the specified window as it is not registered.");
return;
}
HandleWindowActivation(window); // 调用内部逻辑处理,标记为函数调用
}
/// <summary>
/// 在对象销毁时清理事件监听
/// </summary>
private void OnDestroy() private void OnDestroy()
{ {
SceneManager.sceneLoaded -= OnSceneLoaded; SceneManager.sceneLoaded -= OnSceneLoaded;
} }
/// <summary>
/// 在对象启动时初始化
/// </summary>
protected override void OnStart() protected override void OnStart()
{ {
// 注册场景加载事件
SceneManager.sceneLoaded += OnSceneLoaded; SceneManager.sceneLoaded += OnSceneLoaded;
// 初始化时调用一次 // RegisterAllWindows();
Init();
} }
/// <summary>
/// 场景加载完成后重新初始化
/// </summary>
private void OnSceneLoaded(Scene scene, LoadSceneMode mode) private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{ {
// 场景加载完成后调用 Init 方法 RegisterAllWindows();
Init();
} }
} }
} }

View File

@ -9,14 +9,4 @@ namespace Entity
} }
} }
public class MonsterAttributes
{
public int health = 10;
public int moveSpeed = 1;
public int attack = 1;
public int defense = 0;
public int attackSpeed = 2;
public int attackRange = 3;
public int attackTargetCount = 1;
}
} }

View File

@ -78,7 +78,7 @@ namespace Entity
{ {
var rightMenu = Prefab.RightMenuPrefab.Instance; var rightMenu = Prefab.RightMenuPrefab.Instance;
rightMenu.Init(GetMenu()); rightMenu.Init(GetMenu());
rightMenu.transform.position=Input.mousePosition; rightMenu.transform.position = Input.mousePosition;
rightMenu.Show(); rightMenu.Show();
} }
} }
@ -86,12 +86,12 @@ namespace Entity
private List<(string name, UnityAction callback)> GetMenu() private List<(string name, UnityAction callback)> GetMenu()
{ {
var result = new List<(string name, UnityAction callback)>(); var result = new List<(string name, UnityAction callback)>();
if(entity.PlayerControlled) if (entity.PlayerControlled)
result.Add(("结束操控",()=>entity.PlayerControlled=false)); result.Add(("结束操控", () => entity.PlayerControlled = false));
else else
result.Add(("手动操控",()=>entity.PlayerControlled=true)); result.Add(("手动操控", () => entity.PlayerControlled = true));
result.Add(("杀死",()=>entity.Kill())); result.Add(("杀死", () => entity.Kill()));
result.Add(("变成笨蛋",()=>BecomeDefault())); result.Add(("变成笨蛋", () => BecomeDefault()));
return result; return result;
} }
@ -99,5 +99,6 @@ namespace Entity
{ {
entity.Kill(); entity.Kill();
Managers.EntityManage.Instance.GenerateDefaultEntity(entity.Position); Managers.EntityManage.Instance.GenerateDefaultEntity(entity.Position);
}
} }
} }

View File

@ -10,6 +10,8 @@ namespace UI
{ {
public GameObject menuContent; public GameObject menuContent;
public EntityPlacementUI entityPlacementUI;
public Prefab.TextPrefab textTemplate; public Prefab.TextPrefab textTemplate;
public Prefab.ButtonPrefab buttonTemplate; public Prefab.ButtonPrefab buttonTemplate;
@ -35,7 +37,7 @@ namespace UI
// var button= InstantiatePrefab(buttonTemplate, menuContent.transform); // var button= InstantiatePrefab(buttonTemplate, menuContent.transform);
// button.text.text = i.ToString(); // button.text.text = i.ToString();
// } // }
} }
private void InitCharacter() private void InitCharacter()
@ -46,7 +48,7 @@ namespace UI
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.CharacterDef>(); var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.CharacterDef>();
foreach (var def in defList) foreach (var def in defList)
{ {
var button=InstantiatePrefab(buttonTemplate, menuContent.transform); var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
button.Label = def.label; button.Label = def.label;
var pawnDef = def; var pawnDef = def;
button.AddListener(() => GenerateEntityCallback(pawnDef)); button.AddListener(() => GenerateEntityCallback(pawnDef));
@ -57,8 +59,8 @@ namespace UI
{ {
var title = InstantiatePrefab(textTemplate, menuContent.transform); var title = InstantiatePrefab(textTemplate, menuContent.transform);
title.Label = "生成怪物"; title.Label = "生成怪物";
var defList=Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.MonsterDef>(); var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.MonsterDef>();
foreach (var def in defList) foreach (var def in defList)
{ {
var button = InstantiatePrefab(buttonTemplate, menuContent.transform); var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
@ -67,7 +69,7 @@ namespace UI
button.AddListener(() => GenerateEntityCallback(pawnDef)); button.AddListener(() => GenerateEntityCallback(pawnDef));
} }
} }
/// <summary> /// <summary>
/// 通用的实例化函数,返回实例化的预制件脚本组件。 /// 通用的实例化函数,返回实例化的预制件脚本组件。
/// </summary> /// </summary>
@ -84,10 +86,10 @@ namespace UI
} }
// 实例化预制件 // 实例化预制件
GameObject instance = Instantiate(prefab.gameObject, parent); var instance = Instantiate(prefab.gameObject, parent);
// 获取实例化对象的脚本组件 // 获取实例化对象的脚本组件
T instantiatedComponent = instance.GetComponent<T>(); var instantiatedComponent = instance.GetComponent<T>();
if (instantiatedComponent == null) if (instantiatedComponent == null)
{ {
@ -99,7 +101,16 @@ namespace UI
private void GenerateEntityCallback(PawnDef pawnDef) private void GenerateEntityCallback(PawnDef pawnDef)
{ {
Managers.EntityManage.Instance.GenerateEntity(pawnDef, new(0, 0)); entityPlacementUI.currentAction = () =>
{
// 将鼠标屏幕坐标转换为世界坐标,并确保 Z 值为 0
if (!Camera.main) return;
var worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
worldPosition.z = 0;
Managers.EntityManage.Instance.GenerateEntity(pawnDef, worldPosition);
};
entityPlacementUI.Prompt = $"当前生成器:\n名称{pawnDef.label}\n描述{pawnDef.description}";
Base.UIInputControl.Instance.Show(entityPlacementUI);
} }
} }

View File

@ -1,24 +1,34 @@
using Base; using Base;
using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
namespace UI namespace UI
{ {
public delegate void NonReturnCallback();
public class EntityPlacementUI:UIBase,ITickUI public class EntityPlacementUI:UIBase,ITickUI
{ {
public UnityAction currentAction = null; public TMP_Text promptText;
public NonReturnCallback currentAction;
public string Prompt
{
get => promptText.text;
set => promptText.text = value;
}
public void TickUI() public void TickUI()
{ {
if (!IsVisible||currentAction==null) if (!IsVisible)
return; return;
if (Input.GetMouseButton(0))
{
currentAction.Invoke();
}
if (Input.GetKeyDown(KeyCode.Escape)) if (Input.GetKeyDown(KeyCode.Escape))
{ {
Hide(); Base.UIInputControl.Instance.Hide(this);
}
if (currentAction!=null&&Input.GetMouseButtonDown(0))
{
currentAction();
} }
} }

View File

@ -6,7 +6,7 @@ namespace UI
{ {
public void ContinueButton() public void ContinueButton()
{ {
Base.UIInputControl.Instance.CloseWindow(this); Base.UIInputControl.Instance.Hide(this);
} }
public void ExitButton() public void ExitButton()

File diff suppressed because one or more lines are too long