(client) feat:实现指定位置生成实体

This commit is contained in:
m0_75251201
2025-08-04 13:26:04 +08:00
parent e1ff66ff28
commit 3c9be3a30c
8 changed files with 2404 additions and 195 deletions

View File

@ -10,6 +10,8 @@ namespace UI
{
public GameObject menuContent;
public EntityPlacementUI entityPlacementUI;
public Prefab.TextPrefab textTemplate;
public Prefab.ButtonPrefab buttonTemplate;
@ -35,7 +37,7 @@ namespace UI
// var button= InstantiatePrefab(buttonTemplate, menuContent.transform);
// button.text.text = i.ToString();
// }
}
private void InitCharacter()
@ -46,7 +48,7 @@ namespace UI
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.CharacterDef>();
foreach (var def in defList)
{
var button=InstantiatePrefab(buttonTemplate, menuContent.transform);
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
button.Label = def.label;
var pawnDef = def;
button.AddListener(() => GenerateEntityCallback(pawnDef));
@ -57,8 +59,8 @@ namespace UI
{
var title = InstantiatePrefab(textTemplate, menuContent.transform);
title.Label = "生成怪物";
var defList=Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.MonsterDef>();
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.MonsterDef>();
foreach (var def in defList)
{
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
@ -67,7 +69,7 @@ namespace UI
button.AddListener(() => GenerateEntityCallback(pawnDef));
}
}
/// <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)
{
@ -99,7 +101,16 @@ namespace UI
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 TMPro;
using UnityEngine;
using UnityEngine.Events;
namespace UI
{
public delegate void NonReturnCallback();
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()
{
if (!IsVisible||currentAction==null)
if (!IsVisible)
return;
if (Input.GetMouseButton(0))
{
currentAction.Invoke();
}
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()
{
Base.UIInputControl.Instance.CloseWindow(this);
Base.UIInputControl.Instance.Hide(this);
}
public void ExitButton()