(client) feat:实现摄像机跟踪与移动,实现任意位置生成实体,实现更安全的资源加载方式(指定unity内部加载资源) (#42)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
@ -10,23 +10,25 @@ namespace UI
|
||||
{
|
||||
public GameObject menuContent;
|
||||
|
||||
public EntityPlacementUI entityPlacementUI;
|
||||
|
||||
public Prefab.TextPrefab textTemplate;
|
||||
public Prefab.ButtonPrefab buttonTemplate;
|
||||
|
||||
void Start()
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
void Init()
|
||||
private void Init()
|
||||
{
|
||||
InitEvent();
|
||||
InitCharacter();
|
||||
InitMonster();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void InitEvent()
|
||||
|
||||
|
||||
private void InitEvent()
|
||||
{
|
||||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "事件菜单";
|
||||
@ -35,10 +37,10 @@ namespace UI
|
||||
// var button= InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
// button.text.text = i.ToString();
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
void InitCharacter()
|
||||
private void InitCharacter()
|
||||
{
|
||||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "生成人物";
|
||||
@ -46,12 +48,28 @@ 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));
|
||||
}
|
||||
}
|
||||
|
||||
private void InitMonster()
|
||||
{
|
||||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "生成怪物";
|
||||
|
||||
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.MonsterDef>();
|
||||
foreach (var def in defList)
|
||||
{
|
||||
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
button.Label = def.label;
|
||||
var pawnDef = def;
|
||||
button.AddListener(() => GenerateEntityCallback(pawnDef));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通用的实例化函数,返回实例化的预制件脚本组件。
|
||||
/// </summary>
|
||||
@ -59,7 +77,7 @@ namespace UI
|
||||
/// <param name="prefab">要实例化的预制件</param>
|
||||
/// <param name="parent">实例化对象的父对象</param>
|
||||
/// <returns>实例化的预制件脚本组件</returns>
|
||||
T InstantiatePrefab<T>(T prefab, Transform parent) where T : Component
|
||||
private T InstantiatePrefab<T>(T prefab, Transform parent) where T : Component
|
||||
{
|
||||
if (prefab == null || parent == null)
|
||||
{
|
||||
@ -68,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)
|
||||
{
|
||||
@ -81,9 +99,18 @@ namespace UI
|
||||
return instantiatedComponent;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user