(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);
|
||||
}
|
||||
}
|
||||
|
||||
|
36
Client/Assets/Scripts/UI/EntityPlacementUI.cs
Normal file
36
Client/Assets/Scripts/UI/EntityPlacementUI.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using Base;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
|
||||
public delegate void NonReturnCallback();
|
||||
public class EntityPlacementUI:UIBase,ITickUI
|
||||
{
|
||||
public TMP_Text promptText;
|
||||
|
||||
public NonReturnCallback currentAction;
|
||||
|
||||
public string Prompt
|
||||
{
|
||||
get => promptText.text;
|
||||
set => promptText.text = value;
|
||||
}
|
||||
public void TickUI()
|
||||
{
|
||||
if (!IsVisible)
|
||||
return;
|
||||
if (Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
Base.UIInputControl.Instance.Hide(this);
|
||||
}
|
||||
if (currentAction!=null&&Input.GetMouseButtonDown(0))
|
||||
{
|
||||
currentAction();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/UI/EntityPlacementUI.cs.meta
Normal file
3
Client/Assets/Scripts/UI/EntityPlacementUI.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 22c2554b0c0949aab37618f3a80ffe5a
|
||||
timeCreated: 1753505464
|
@ -1,10 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class EscUI:UIBase
|
||||
{
|
||||
public void ContinueButton()
|
||||
{
|
||||
Base.UIInputControl.Instance.CloseWindow(this);
|
||||
Base.UIInputControl.Instance.Hide(this);
|
||||
}
|
||||
|
||||
public void ExitButton()
|
||||
|
100
Client/Assets/Scripts/UI/LogUI.cs
Normal file
100
Client/Assets/Scripts/UI/LogUI.cs
Normal file
@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Prefab;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class LogUI : UIBase
|
||||
{
|
||||
public Transform contentPanel; // 日志内容容器
|
||||
public TextPrefab textPrefab; // 文本预制体引用
|
||||
|
||||
// 日志类型颜色映射
|
||||
private static readonly Dictionary<LogType, Color> logColors = new Dictionary<LogType, Color>
|
||||
{
|
||||
{ LogType.Log, Color.white },
|
||||
{ LogType.Warning, Color.yellow },
|
||||
{ LogType.Error, new Color(1f, 0.4f, 0.4f) },
|
||||
{ LogType.Exception, new Color(1f, 0.2f, 0.2f) },
|
||||
{ LogType.Assert, new Color(0.8f, 0.4f, 1f) }
|
||||
};
|
||||
|
||||
private List<Transform> _logItems = new List<Transform>(); // 已创建的日志条目
|
||||
private int _lastLogCount = 0; // 上次显示的日志数量
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Logging.LogCapturer.Clear();
|
||||
}
|
||||
|
||||
public override void Show()
|
||||
{
|
||||
base.Show();
|
||||
RefreshLogDisplay();
|
||||
}
|
||||
|
||||
private void RefreshLogDisplay()
|
||||
{
|
||||
var logs = Logging.LogCapturer.GetLogs();
|
||||
|
||||
// 如果日志数量减少,清理多余的条目
|
||||
if (logs.Count < _lastLogCount)
|
||||
{
|
||||
for (int i = logs.Count; i < _lastLogCount; i++)
|
||||
{
|
||||
Destroy(_logItems[i].gameObject);
|
||||
}
|
||||
|
||||
_logItems.RemoveRange(logs.Count, _logItems.Count - logs.Count);
|
||||
}
|
||||
|
||||
// 更新现有条目
|
||||
for (int i = 0; i < Math.Min(logs.Count, _logItems.Count); i++)
|
||||
{
|
||||
UpdateLogEntry(_logItems[i], logs[logs.Count - 1 - i]);
|
||||
}
|
||||
|
||||
// 添加新的条目
|
||||
if (logs.Count > _lastLogCount)
|
||||
{
|
||||
for (int i = _lastLogCount; i < logs.Count; i++)
|
||||
{
|
||||
CreateLogEntry(logs[logs.Count - 1 - i]);
|
||||
}
|
||||
}
|
||||
|
||||
_lastLogCount = logs.Count;
|
||||
}
|
||||
|
||||
private void CreateLogEntry(Logging.LogCapturer.LogEntry entry)
|
||||
{
|
||||
// 实例化文本预制体
|
||||
var logItem = Instantiate(textPrefab, contentPanel);
|
||||
_logItems.Add(logItem.transform);
|
||||
|
||||
UpdateLogEntry(logItem.transform, entry);
|
||||
}
|
||||
|
||||
private void UpdateLogEntry(Transform logItemTransform, Logging.LogCapturer.LogEntry entry)
|
||||
{
|
||||
var logItem = logItemTransform.GetComponent<TextPrefab>();
|
||||
|
||||
// 设置文本内容
|
||||
logItem.Label = entry.ToString();
|
||||
|
||||
// 设置文本颜色(根据日志类型)
|
||||
if (logColors.TryGetValue(entry.Type, out Color color))
|
||||
{
|
||||
logItem.text.color = color;
|
||||
}
|
||||
else
|
||||
{
|
||||
logItem.text.color = Color.white; // 默认颜色
|
||||
}
|
||||
|
||||
logItem.text.alignment = TextAlignmentOptions.TopLeft;
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/UI/LogUI.cs.meta
Normal file
3
Client/Assets/Scripts/UI/LogUI.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 84d93a37c40b425890e954f2ddb9344c
|
||||
timeCreated: 1753457075
|
@ -2,12 +2,24 @@ using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public abstract class UIBase:MonoBehaviour
|
||||
public abstract class UIBase : MonoBehaviour
|
||||
{
|
||||
public bool exclusive = true;
|
||||
public bool needPause = true;
|
||||
public bool isInputOccupied = false;
|
||||
|
||||
public KeyCode actionButton = KeyCode.None;
|
||||
|
||||
// 显示或隐藏窗口
|
||||
public virtual void Show() { gameObject.SetActive(true); }
|
||||
public virtual void Hide() { gameObject.SetActive(false); }
|
||||
public virtual void Show()
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public virtual void Hide()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
// 判断是否可见
|
||||
public bool IsVisible => gameObject.activeInHierarchy;
|
||||
|
Reference in New Issue
Block a user