(client) feat:添加消息定义,添加故事定义及其运算,武器动画可用,装备UI可用以及切换武器 fix:修复快速攻击导致协程释放出错卡死,重构为计时器,修复类型转换错误导致报错
This commit is contained in:
@ -5,6 +5,7 @@ using Data;
|
||||
using EventWorkClass;
|
||||
using Utils;
|
||||
using UnityEngine;
|
||||
using Base;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
@ -12,7 +13,7 @@ namespace Managers
|
||||
/// 事件管理器,负责事件的加载、注册和执行。
|
||||
/// 遵循单例模式,并在启动流程中扮演一个管理器角色。
|
||||
/// </summary>
|
||||
class EventManager : Singleton<EventManager>, ILaunchManager
|
||||
class EventManager : Singleton<EventManager>, ILaunchManager, ITick // 实现 ITick 接口
|
||||
{
|
||||
/// <summary>
|
||||
/// 存储所有已加载的事件定义,键为事件名称,值为对应的事件工作类实例。
|
||||
@ -20,26 +21,38 @@ namespace Managers
|
||||
public Dictionary<string, EventWorkClassBase> EventDefs { get; private set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前加载步骤的描述,用于启动流程的进度显示。
|
||||
/// 存储所有已加载的故事定义,键为故事名称,值为对应的故事定义实例。
|
||||
/// </summary>
|
||||
public string StepDescription => "正在载入事件";
|
||||
public Dictionary<string, StoryDef> StoryDefs { get; private set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化事件管理器,从定义管理器中加载所有事件定义并实例化其工作类。
|
||||
/// 存储当前正在播放的所有故事实例。
|
||||
/// </summary>
|
||||
private readonly List<StoryPlayer> _activeStoryPlayers = new List<StoryPlayer>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前加载步骤的描述,用于启动流程的进度显示。
|
||||
/// </summary>
|
||||
public string StepDescription => "正在载入事件和故事";
|
||||
|
||||
/// <summary>
|
||||
/// 初始化事件管理器,从定义管理器中加载所有事件定义和故事定义。
|
||||
/// </summary>
|
||||
public void Init()
|
||||
{
|
||||
// 如果事件定义已经加载,则直接返回,避免重复初始化。
|
||||
if (EventDefs != null)
|
||||
// 如果事件和故事定义已经加载,则直接返回,避免重复初始化。
|
||||
if (EventDefs != null && StoryDefs != null)
|
||||
return;
|
||||
|
||||
var defs = DefineManager.Instance.QueryDefinesByType<EventDef>();
|
||||
// 从定义管理器查询并加载所有事件定义。
|
||||
var eventDefs = DefineManager.Instance.QueryDefinesByType<EventDef>();
|
||||
EventDefs = new Dictionary<string, EventWorkClassBase>();
|
||||
foreach (var def in defs)
|
||||
var loadedEventCount = 0;
|
||||
foreach (var def in eventDefs)
|
||||
{
|
||||
if (EventDefs.ContainsKey(def.defName))
|
||||
{
|
||||
Debug.LogWarning($"警告:事件名称重复,已跳过加载名称为 {def.defName} 的事件定义。");
|
||||
Debug.LogWarning($"警告:事件名称重复,已跳过加载名称为 '{def.defName}' 的事件定义。");
|
||||
continue;
|
||||
}
|
||||
var eventWorker = GetAndInstantiateEventWorker(def.workClass);
|
||||
@ -48,19 +61,45 @@ namespace Managers
|
||||
Debug.LogWarning($"警告:未能找到或实例化名称为 '{def.workClass}' 的事件工作类,已跳过加载名称为 '{def.defName}' 的事件定义。");
|
||||
continue;
|
||||
}
|
||||
eventWorker.Init(def.value);
|
||||
eventWorker.Init(def.parameter);
|
||||
EventDefs.Add(def.defName, eventWorker);
|
||||
loadedEventCount++;
|
||||
}
|
||||
Debug.Log($"事件管理器初始化完成,共载入 {EventDefs.Count} 个事件。");
|
||||
Debug.Log($"事件管理器初始化完成,共载入 {loadedEventCount} 个事件。");
|
||||
|
||||
// 从定义管理器查询并加载所有故事定义。
|
||||
var storyDefs = DefineManager.Instance.QueryDefinesByType<StoryDef>();
|
||||
StoryDefs = new Dictionary<string, StoryDef>();
|
||||
var loadedStoryCount = 0;
|
||||
foreach (var storyDef in storyDefs)
|
||||
{
|
||||
if (StoryDefs.ContainsKey(storyDef.defName))
|
||||
{
|
||||
Debug.LogWarning($"警告:故事名称重复,已跳过加载名称为 '{storyDef.defName}' 的故事定义。");
|
||||
continue;
|
||||
}
|
||||
StoryDefs.Add(storyDef.defName, storyDef);
|
||||
loadedStoryCount++;
|
||||
}
|
||||
Debug.Log($"事件管理器初始化完成,共载入 {loadedStoryCount} 个故事。");
|
||||
|
||||
// 将自身注册到时钟系统,以便每帧更新故事播放。
|
||||
Clock.AddTick(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理事件管理器,释放所有已加载的事件定义。
|
||||
/// 清理事件管理器,释放所有已加载的事件和故事定义。
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
// 从时钟系统移除自身,停止接收Tick更新。
|
||||
Clock.RemoveTick(this);
|
||||
// 清理所有正在播放的故事实例。
|
||||
_activeStoryPlayers.Clear();
|
||||
// 释放事件定义字典。
|
||||
EventDefs = null;
|
||||
Debug.Log("事件管理器已清理。");
|
||||
// 释放故事定义字典。
|
||||
StoryDefs = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -76,7 +115,7 @@ namespace Managers
|
||||
return;
|
||||
}
|
||||
|
||||
if (!EventDefs.ContainsKey(eventName))
|
||||
if (!EventDefs.TryGetValue(eventName, out var eventWorker))
|
||||
{
|
||||
Debug.LogWarning($"警告:未能找到名称为 '{eventName}' 的事件定义,已跳过执行该事件。");
|
||||
return;
|
||||
@ -84,9 +123,89 @@ namespace Managers
|
||||
// 假设 Program.Instance 和 FocusedDimensionId 存在且可访问。
|
||||
// 如果 dimensionID 为 null,则使用当前焦点维度ID。
|
||||
dimensionID ??= Program.Instance.FocusedDimensionId;
|
||||
EventDefs[eventName].Run(dimensionID);
|
||||
eventWorker.Run(dimensionID);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 播放指定名称的故事。
|
||||
/// </summary>
|
||||
/// <param name="storyName">要播放的故事的名称。</param>
|
||||
/// <param name="dimensionID">故事执行的维度ID,如果为null,将使用当前焦点的维度ID。</param>
|
||||
public void PlayStory(string storyName, string dimensionID = null)
|
||||
{
|
||||
if (StoryDefs == null || StoryDefs.Count == 0)
|
||||
{
|
||||
Debug.LogError($"错误:故事定义尚未加载或已被清理。无法播放故事 '{storyName}'。");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!StoryDefs.TryGetValue(storyName, out var storyDef))
|
||||
{
|
||||
Debug.LogWarning($"警告:未能找到名称为 '{storyName}' 的故事定义,已跳过播放该故事。");
|
||||
return;
|
||||
}
|
||||
|
||||
if (storyDef.storyStage == null || storyDef.storyStage.Length == 0)
|
||||
{
|
||||
Debug.LogWarning($"警告:故事 '{storyDef.defName}' 没有定义任何阶段,已跳过播放。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 确保 dimensionID 有效,如果为 null 则使用当前焦点维度ID。
|
||||
dimensionID ??= Program.Instance.FocusedDimensionId;
|
||||
|
||||
// 创建一个新的 StoryPlayer 实例并添加到活跃列表中。
|
||||
var player = new StoryPlayer(storyDef, dimensionID, this);
|
||||
_activeStoryPlayers.Add(player);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 每帧更新,用于驱动所有活跃故事的播放逻辑。
|
||||
/// 作为 <see cref="ITick"/> 接口的实现,由时钟系统调用。
|
||||
/// </summary>
|
||||
public void Tick()
|
||||
{
|
||||
if (_activeStoryPlayers.Count == 0) return;
|
||||
|
||||
// 获取自上一帧以来的时间增量。
|
||||
var deltaTime = Time.deltaTime;
|
||||
|
||||
// 遍历所有正在播放的故事实例,并更新它们的状态。
|
||||
// 为了避免在迭代过程中修改列表,先收集要移除的,再统一移除。
|
||||
var playersToRemove = new List<StoryPlayer>();
|
||||
foreach (var player in _activeStoryPlayers)
|
||||
{
|
||||
player.Update(deltaTime);
|
||||
if (player.IsFinished)
|
||||
{
|
||||
playersToRemove.Add(player);
|
||||
}
|
||||
}
|
||||
|
||||
// 移除所有已完成的故事实例。
|
||||
foreach (var player in playersToRemove)
|
||||
{
|
||||
_activeStoryPlayers.Remove(player);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串颜色值解析为 <see cref="Color"/> 对象。
|
||||
/// 支持十六进制颜色(如 "#RRGGBB" 或 "#AARRGGBB")或标准颜色名称。
|
||||
/// </summary>
|
||||
/// <param name="colorString">颜色字符串。</param>
|
||||
/// <returns>解析后的 <see cref="Color"/> 对象。如果解析失败,返回白色。</returns>
|
||||
private Color ParseColor(string colorString)
|
||||
{
|
||||
if (ColorUtility.TryParseHtmlString(colorString, out var color))
|
||||
{
|
||||
return color;
|
||||
}
|
||||
Debug.LogWarning($"警告:无法解析颜色字符串 '{colorString}'。使用默认白色。");
|
||||
return Color.white; // 默认返回白色
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据类名从指定命名空间和程序集下获取并实例化一个 <see cref="EventWorkClassBase"/> 的子类。
|
||||
/// </summary>
|
||||
@ -102,7 +221,7 @@ namespace Managers
|
||||
// 1. 确定要搜索的程序集。
|
||||
if (assemblyToSearch == null)
|
||||
{
|
||||
// 默认从 EventWorkClassBase 所在的程序集查找,通常其实现类也会在这个程序集。
|
||||
// 默认从 EventWorkClassBase 所在的程序集查找,通常其实现类也位于此程序集。
|
||||
assemblyToSearch = typeof(EventWorkClassBase).Assembly;
|
||||
}
|
||||
|
||||
@ -141,14 +260,182 @@ namespace Managers
|
||||
}
|
||||
catch (MissingMethodException ex)
|
||||
{
|
||||
Debug.LogError($"错误:类 '{fullTypeName}' 没有公共的无参构造函数。详情: {ex.Message}");
|
||||
Debug.LogError($"错误:类 '{fullTypeName}' 没有公共的无参构造函数。详细信息: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"实例化类 '{fullTypeName}' 时发生未知错误。详情: {ex.Message}");
|
||||
Debug.LogError($"实例化类 '{fullTypeName}' 时发生未知错误。详细信息: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 表示一个正在播放的故事实例的状态机。
|
||||
/// </summary>
|
||||
private class StoryPlayer
|
||||
{
|
||||
/// <summary> 故事播放阶段的状态枚举。 </summary>
|
||||
private enum State
|
||||
{
|
||||
Initializing, // 刚开始,准备处理第一个阶段的 lastWaitTime
|
||||
WaitingLastTime, // 等待当前阶段的 lastWaitTime
|
||||
ExecutingStage, // 执行当前阶段的事件/消息
|
||||
WaitingNextTime, // 等待当前阶段的 nextWaitTime
|
||||
Finished // 故事播放完毕
|
||||
}
|
||||
|
||||
/// <summary> 获取当前正在播放的故事定义。 </summary>
|
||||
public StoryDef StoryDef { get; private set; }
|
||||
/// <summary> 获取故事播放所在的维度ID。 </summary>
|
||||
public string DimensionID { get; private set; }
|
||||
/// <summary> 获取一个值,表示故事是否已播放完毕。 </summary>
|
||||
public bool IsFinished { get; private set; } = false;
|
||||
|
||||
// 事件管理器实例,用于调用 Action 和 ParseColor 方法。
|
||||
private readonly EventManager _eventManager;
|
||||
// 当前故事阶段的索引。
|
||||
private int _currentStageIndex;
|
||||
// 当前阶段的等待计时器。
|
||||
private float _currentWaitTimer;
|
||||
// 当前故事播放器所处的状态。
|
||||
private State _currentState;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化 <see cref="StoryPlayer"/> 类的新实例。
|
||||
/// </summary>
|
||||
/// <param name="storyDef">要播放的故事定义。</param>
|
||||
/// <param name="dimensionId">故事播放所在的维度ID。</param>
|
||||
/// <param name="eventManager">事件管理器实例,用于执行事件和解析颜色。</param>
|
||||
public StoryPlayer(StoryDef storyDef, string dimensionId, EventManager eventManager)
|
||||
{
|
||||
StoryDef = storyDef;
|
||||
DimensionID = dimensionId;
|
||||
_eventManager = eventManager;
|
||||
_currentStageIndex = 0;
|
||||
_currentWaitTimer = 0f;
|
||||
_currentState = State.Initializing;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 每帧更新故事播放器的状态。
|
||||
/// </summary>
|
||||
/// <param name="deltaTime">自上一帧以来的时间增量。</param>
|
||||
public void Update(float deltaTime)
|
||||
{
|
||||
if (IsFinished) return;
|
||||
|
||||
// 如果故事阶段定义为null或为空,则立即标记故事完成。
|
||||
if (StoryDef.storyStage == null || StoryDef.storyStage.Length == 0)
|
||||
{
|
||||
Debug.LogWarning($"警告:故事 '{StoryDef.defName}' 没有定义任何阶段,StoryPlayer 将立即完成。");
|
||||
IsFinished = true;
|
||||
return;
|
||||
}
|
||||
|
||||
var currentStage = StoryDef.storyStage[_currentStageIndex];
|
||||
|
||||
switch (_currentState)
|
||||
{
|
||||
case State.Initializing:
|
||||
// 从当前阶段的 lastWaitTime 开始等待,如果 lastWaitTime 小于等于 0 则直接跳过等待执行阶段。
|
||||
if (currentStage.lastWaitTime > 0)
|
||||
{
|
||||
_currentState = State.WaitingLastTime;
|
||||
_currentWaitTimer = 0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 没有 lastWaitTime,直接执行阶段。
|
||||
_currentState = State.ExecutingStage;
|
||||
}
|
||||
break;
|
||||
|
||||
case State.WaitingLastTime:
|
||||
_currentWaitTimer += deltaTime;
|
||||
if (_currentWaitTimer >= currentStage.lastWaitTime)
|
||||
{
|
||||
// 等待时间已到,切换到执行阶段。
|
||||
_currentWaitTimer = 0f;
|
||||
_currentState = State.ExecutingStage;
|
||||
}
|
||||
break;
|
||||
|
||||
case State.ExecutingStage:
|
||||
// 处理事件
|
||||
if (currentStage.eventDef != null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(currentStage.eventDef.defName))
|
||||
{
|
||||
_eventManager.Action(currentStage.eventDef.defName, DimensionID);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"警告:故事 '{StoryDef.defName}' 阶段 {_currentStageIndex} 包含一个没有定义名称的事件,已跳过。");
|
||||
}
|
||||
}
|
||||
|
||||
// 处理消息
|
||||
if (currentStage.messageDef != null)
|
||||
{
|
||||
if (MessageManager.Instance == null)
|
||||
{
|
||||
Debug.LogError($"错误:MessageManager 实例不存在,无法显示故事 '{StoryDef.defName}' 的消息。");
|
||||
}
|
||||
else
|
||||
{
|
||||
var messageColor = _eventManager.ParseColor(currentStage.messageDef.color);
|
||||
MessageManager.Instance.DisplayMessage(currentStage.messageDef.text, currentStage.messageDef.type, messageColor);
|
||||
}
|
||||
}
|
||||
|
||||
// 决定下一个状态
|
||||
if (currentStage.nextWaitTime > 0)
|
||||
{
|
||||
_currentState = State.WaitingNextTime;
|
||||
_currentWaitTimer = 0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 没有 nextWaitTime,直接推进到下一个阶段。
|
||||
AdvanceToNextStage();
|
||||
}
|
||||
break;
|
||||
|
||||
case State.WaitingNextTime:
|
||||
_currentWaitTimer += deltaTime;
|
||||
if (_currentWaitTimer >= currentStage.nextWaitTime)
|
||||
{
|
||||
// 等待时间已到,推进到下一个阶段。
|
||||
_currentWaitTimer = 0f;
|
||||
AdvanceToNextStage();
|
||||
}
|
||||
break;
|
||||
|
||||
case State.Finished:
|
||||
// 故事已经完成,不再更新。
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 推进到下一个故事阶段,或标记故事完成。
|
||||
/// </summary>
|
||||
private void AdvanceToNextStage()
|
||||
{
|
||||
_currentStageIndex++;
|
||||
if (_currentStageIndex < StoryDef.storyStage.Length)
|
||||
{
|
||||
// 还有后续阶段,重置状态以处理下一个阶段的 lastWaitTime。
|
||||
_currentState = State.Initializing;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 所有阶段播放完毕,标记故事完成。
|
||||
_currentState = State.Finished;
|
||||
IsFinished = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ namespace Managers
|
||||
public class ItemResourceManager : Utils.Singleton<ItemResourceManager>, ILaunchManager
|
||||
{
|
||||
private ItemResource defaultItem;
|
||||
private readonly Dictionary<string, Item.ItemResource> _items = new();
|
||||
private readonly Dictionary<string, List<Item.ItemResource>> _itemsByName = new(); // 保持按显示名称查找的字典
|
||||
private readonly Dictionary<string, ItemResource> _items = new();
|
||||
private readonly Dictionary<string, List<ItemResource>> _itemsByName = new(); // 保持按显示名称查找的字典
|
||||
|
||||
public string StepDescription => "加载物品定义中";
|
||||
|
||||
@ -39,17 +39,17 @@ namespace Managers
|
||||
continue;
|
||||
}
|
||||
|
||||
Item.ItemResource itemResource;
|
||||
ItemResource itemResource;
|
||||
|
||||
if (def is WeaponDef currentWeaponDef)
|
||||
{
|
||||
itemResource = new Item.WeaponResource(
|
||||
itemResource = new WeaponResource(
|
||||
currentWeaponDef
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
itemResource = new Item.ItemResource(
|
||||
itemResource = new ItemResource(
|
||||
def
|
||||
);
|
||||
}
|
||||
@ -57,26 +57,26 @@ namespace Managers
|
||||
_items.Add(def.defName, itemResource);
|
||||
if (!_itemsByName.ContainsKey(itemResource.Name))
|
||||
{
|
||||
_itemsByName.Add(itemResource.Name, new List<Item.ItemResource>());
|
||||
_itemsByName.Add(itemResource.Name, new List<ItemResource>());
|
||||
}
|
||||
_itemsByName[itemResource.Name].Add(itemResource);
|
||||
}
|
||||
}
|
||||
|
||||
public Item.ItemResource GetItem(string defName)
|
||||
public ItemResource GetItem(string defName)
|
||||
{
|
||||
return _items.GetValueOrDefault(defName, defaultItem);
|
||||
}
|
||||
public Item.ItemResource FindItemByName(string itemName)
|
||||
public ItemResource FindItemByName(string itemName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(itemName)) return defaultItem;
|
||||
return _itemsByName.GetValueOrDefault(itemName)?.FirstOrDefault();
|
||||
}
|
||||
|
||||
public List<Item.ItemResource> FindAllItemsByName(string itemName)
|
||||
public List<ItemResource> FindAllItemsByName(string itemName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(itemName)) return new List<Item.ItemResource>();
|
||||
return _itemsByName.GetValueOrDefault(itemName, new List<Item.ItemResource>());
|
||||
if (string.IsNullOrEmpty(itemName)) return new List<ItemResource>();
|
||||
return _itemsByName.GetValueOrDefault(itemName, new List<ItemResource>());
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
|
206
Client/Assets/Scripts/Managers/MessageManager.cs
Normal file
206
Client/Assets/Scripts/Managers/MessageManager.cs
Normal file
@ -0,0 +1,206 @@
|
||||
using System;
|
||||
using Base;
|
||||
using Data;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement; // 新增引用
|
||||
using UnityEngine.UI; // 用于 LayoutGroup 和 RectTransform
|
||||
using TMPro; // 用于 TMP_Text
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class MessageManager:Utils.MonoSingleton<MessageManager>
|
||||
{
|
||||
private RectTransform _canvas;
|
||||
private TemporaryAnimatorText _temporaryAnimatorTextPrefab; // 重命名,表示是预制体
|
||||
private RectTransform _passiveHintContainer; // 用于PassiveHint的容器
|
||||
|
||||
// SceneManager.sceneLoaded 注册/取消注册
|
||||
private void OnEnable()
|
||||
{
|
||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
public void DisplayMessage(string message, Data.PromptDisplayCategory type,Color? color=null)
|
||||
{
|
||||
if (!_canvas)
|
||||
{
|
||||
Debug.LogWarning($"MessageManager: Canvas is not available. Message '{message}' ignored.");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case PromptDisplayCategory.FocusedEntityOverheadText:
|
||||
if (Program.Instance.FocusedEntity == null)
|
||||
return;
|
||||
// GenerateTemporaryAnimation的第三个参数是显示时间
|
||||
TemporaryAnimationManager.Instance.GenerateTemporaryAnimation(message,
|
||||
Program.Instance.FocusedEntity.Position, 5); // 5秒显示时间
|
||||
break;
|
||||
|
||||
case PromptDisplayCategory.PassiveHint:
|
||||
if (_passiveHintContainer == null)
|
||||
{
|
||||
Debug.LogWarning("Cannot display PassiveHint: PassiveHintContainer is not available. Please ensure Canvas is present.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 实例化消息文本作为PassiveHintContainer的子对象
|
||||
var hintTextInstance = Instantiate(_temporaryAnimatorTextPrefab, _passiveHintContainer.transform);
|
||||
|
||||
// 确保它在Layout Group中正确显示
|
||||
var hintTextRect = hintTextInstance.GetComponent<RectTransform>();
|
||||
|
||||
// 如果temporaryAnimatorText有ContentSizeFitter,这里可能不需要设置sizeDelta,但为了LayoutGroup能识别,可以添加LayoutElement
|
||||
var layoutElement = hintTextInstance.GetComponent<LayoutElement>();
|
||||
if (layoutElement == null) layoutElement = hintTextInstance.gameObject.AddComponent<LayoutElement>();
|
||||
layoutElement.minHeight = 30; // 最小高度
|
||||
layoutElement.preferredWidth = _passiveHintContainer.sizeDelta.x; // 适应容器的宽度
|
||||
|
||||
// 设置字体样式
|
||||
var hintTmpText = hintTextInstance.GetComponent<TMP_Text>();
|
||||
if (hintTmpText != null)
|
||||
{
|
||||
hintTmpText.fontSize = 24; // 较小的字体
|
||||
hintTmpText.alignment = TextAlignmentOptions.TopLeft; // 左上对齐
|
||||
hintTmpText.enableAutoSizing = false; // 关闭自动调整字体,保持统一
|
||||
hintTmpText.SetText(message); // 先设置文本,确保布局计算正确
|
||||
if(color.HasValue)
|
||||
hintTmpText.color = color.Value;
|
||||
}
|
||||
|
||||
hintTextInstance.Init(message); // Init 方法会处理动画和生命周期
|
||||
// TemporaryAnimatorText 应该在 Init 内部设置好 lifeTime 并自动销毁。
|
||||
break;
|
||||
|
||||
case PromptDisplayCategory.ScreenCenterLargeText:
|
||||
var textInstance = GameObject.Instantiate(_temporaryAnimatorTextPrefab, _canvas.transform); // 使用预制体变量
|
||||
|
||||
// 设置RectTransform实现全屏
|
||||
var textRect = textInstance.GetComponent<RectTransform>();
|
||||
textRect.anchorMin = Vector2.zero; // (0,0)
|
||||
textRect.anchorMax = Vector2.one; // (1,1)
|
||||
textRect.offsetMin = Vector2.zero; // 左下角偏移为(0,0)
|
||||
textRect.offsetMax = Vector2.zero; // 右上角偏移为(0,0)
|
||||
|
||||
|
||||
// 获取TMP_Text并设置字体等
|
||||
var tmpText = textInstance.GetComponent<TMP_Text>();
|
||||
if (tmpText != null)
|
||||
{
|
||||
tmpText.fontSize = 80; // 设置一个较大的字体大小
|
||||
tmpText.alignment = TextAlignmentOptions.Center; // 居中对齐
|
||||
tmpText.enableAutoSizing = true; // 允许自动调整字体大小以适应文本框 (可选)
|
||||
tmpText.SetText(message); // 先设置文本,确保布局计算正确
|
||||
if(color.HasValue)
|
||||
tmpText.color = color.Value;
|
||||
}
|
||||
|
||||
textInstance.Init(message); // Init 方法会处理动画和生命周期
|
||||
// textInstance.lifeTime 可以在 Init 方法内部设置,如果 Init 没有提供参数,这里就无法直接设置。
|
||||
// 假设 Init 已经处理好生命周期。
|
||||
break;
|
||||
case PromptDisplayCategory.Default:
|
||||
break;
|
||||
case PromptDisplayCategory.FocusedEntityChatBubble:
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(type), type, null);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
// 在单例第一次初始化时加载预制体
|
||||
_temporaryAnimatorTextPrefab =
|
||||
Resources.Load<TemporaryAnimatorText>("Prefab/TemporaryAnimation/UITemporaryAnimationText");
|
||||
|
||||
if (_temporaryAnimatorTextPrefab == null)
|
||||
{
|
||||
Debug.LogError("Failed to load TemporaryAnimatorText prefab. Check the path: Prefab/TemporaryAnimation/UITemporaryAnimationText");
|
||||
}
|
||||
|
||||
// 首次启动时也尝试查找 Canvas 和创建 PassiveHint 容器
|
||||
// 这可以处理管理器在 Canvas 和其他 UI 元素之前启动的情况
|
||||
if (SceneManager.GetActiveScene().isLoaded)
|
||||
{
|
||||
OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 场景加载完成回调,用于查找并设置Canvas和PassiveHint容器
|
||||
/// </summary>
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
// 尝试查找Canvas
|
||||
var mainCanvas = FindAnyObjectByType<Canvas>(); // 查找场景中的第一个Canvas
|
||||
if (mainCanvas != null)
|
||||
{
|
||||
_canvas = mainCanvas.GetComponent<RectTransform>();
|
||||
InitializePassiveHintContainer();
|
||||
}
|
||||
else
|
||||
{
|
||||
_canvas = null;
|
||||
// 如果没有Canvas,确保容器也被清除,避免引用无效对象
|
||||
if (_passiveHintContainer != null)
|
||||
{
|
||||
Destroy(_passiveHintContainer.gameObject);
|
||||
_passiveHintContainer = null;
|
||||
}
|
||||
Debug.LogWarning($"MessageManager: No Canvas found in scene '{scene.name}'. UI messages might not display.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建并初始化PassiveHint的UI容器
|
||||
/// </summary>
|
||||
private void InitializePassiveHintContainer()
|
||||
{
|
||||
if (_canvas == null)
|
||||
{
|
||||
Debug.LogError("Cannot initialize PassiveHint container: Canvas is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果容器已经存在,先销毁旧的,确保每次场景加载都是新的容器
|
||||
if (_passiveHintContainer != null)
|
||||
{
|
||||
Destroy(_passiveHintContainer.gameObject);
|
||||
_passiveHintContainer = null;
|
||||
}
|
||||
|
||||
// 创建一个新的GameObject作为容器
|
||||
var containerGO = new GameObject("PassiveHintContainer");
|
||||
_passiveHintContainer = containerGO.AddComponent<RectTransform>();
|
||||
_passiveHintContainer.SetParent(_canvas, false); // false表示不保留世界坐标
|
||||
|
||||
// 设置容器的位置和大小:左侧中间
|
||||
_passiveHintContainer.anchorMin = new Vector2(0, 0.5f);
|
||||
_passiveHintContainer.anchorMax = new Vector2(0, 0.5f);
|
||||
_passiveHintContainer.pivot = new Vector2(0, 0.5f); // 锚点和枢轴都在左中
|
||||
_passiveHintContainer.anchoredPosition = new Vector2(100, 0); // 从左边距100(示例值),Y轴中心
|
||||
_passiveHintContainer.sizeDelta = new Vector2(400, 600); // 示例宽度和高度
|
||||
|
||||
// 添加VerticalLayoutGroup
|
||||
var layoutGroup = containerGO.AddComponent<VerticalLayoutGroup>();
|
||||
layoutGroup.childAlignment = TextAnchor.UpperLeft; // 子元素靠左上排布
|
||||
layoutGroup.spacing = 10; // 消息之间的间距
|
||||
layoutGroup.padding = new RectOffset(10, 10, 10, 10); // 容器内边距
|
||||
|
||||
// 添加ContentSizeFitter,以便容器根据内容调整大小 (可选,根据实际需求)
|
||||
// fitter 会自动调整自身大小以适应子内容
|
||||
var fitter = containerGO.AddComponent<ContentSizeFitter>();
|
||||
fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Managers/MessageManager.cs.meta
Normal file
3
Client/Assets/Scripts/Managers/MessageManager.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 067858a7493442389cc3f7624b83e1f3
|
||||
timeCreated: 1757231873
|
@ -18,9 +18,6 @@ namespace Managers
|
||||
[SerializeField] private TemporaryAnimatorSprite temporaryAnimatorSpritePrefab;
|
||||
[SerializeField] private TemporaryAnimatorText temporaryAnimatorTextPrefab;
|
||||
[SerializeField] private TemporaryAnimatorText temporaryAnimatorUITextPrefab;
|
||||
|
||||
private Func<float, float> defaultXoffset = f => Mathf.Sin(f * 3);
|
||||
private Func<float, float> defaultYoffset = f => f;
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
@ -89,6 +86,15 @@ namespace Managers
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成一个非UI文本临时动画。
|
||||
/// </summary>
|
||||
/// <param name="str">动画的文本内容。</param>
|
||||
/// <param name="position">动画在世界坐标系中的初始位置。</param>
|
||||
/// <param name="lifeTime">动画的生命周期(秒)。</param>
|
||||
/// <param name="fps">动画帧率。</param>
|
||||
/// <param name="xShift">X轴位移函数。</param>
|
||||
/// <param name="yShift">Y轴位移函数。</param>
|
||||
public void GenerateTemporaryAnimation(string str, Vector3 position, float lifeTime = 3, float fps = 3,
|
||||
Func<float, float> xShift = null,
|
||||
Func<float, float> yShift = null)
|
||||
@ -99,13 +105,42 @@ namespace Managers
|
||||
return;
|
||||
}
|
||||
|
||||
xShift ??= defaultXoffset;
|
||||
yShift ??= defaultYoffset;
|
||||
var textObj = Instantiate(temporaryAnimatorTextPrefab, _temporaryAnimationLevel.transform);
|
||||
textObj.transform.position = new Vector3(position.x,position.y);
|
||||
textObj.Init(str, fps);
|
||||
textObj.SetAnimationFunctions(xShift, yShift);
|
||||
textObj.lifeTime = lifeTime;
|
||||
textObj.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成一个非UI文本临时动画,可指定父级Transform。
|
||||
/// </summary>
|
||||
/// <param name="str">动画的文本内容。</param>
|
||||
/// <param name="position">动画在世界坐标系中的初始位置。</param>
|
||||
/// <param name="parent">动画的父级Transform。如果为null,则使用默认的非UI动画层。</param>
|
||||
/// <param name="lifeTime">动画的生命周期(秒)。</param>
|
||||
/// <param name="fps">动画帧率。</param>
|
||||
/// <param name="xShift">X轴位移函数。</param>
|
||||
/// <param name="yShift">Y轴位移函数。</param>
|
||||
public void GenerateTemporaryAnimation(string str, Vector3 position, Transform parent, float lifeTime = 3, float fps = 3,
|
||||
Func<float, float> xShift = null,
|
||||
Func<float, float> yShift = null)
|
||||
{
|
||||
Transform actualParent = parent ?? _temporaryAnimationLevel?.transform;
|
||||
|
||||
if (actualParent == null)
|
||||
{
|
||||
Debug.LogError("TemporaryAnimationLevel or specified parent is not initialized. Cannot generate non-UI animation.");
|
||||
return;
|
||||
}
|
||||
|
||||
var textObj = Instantiate(temporaryAnimatorTextPrefab, actualParent);
|
||||
textObj.transform.position = position; // 外部传入的position应视为世界坐标
|
||||
textObj.Init(str, fps);
|
||||
textObj.SetAnimationFunctions(xShift, yShift);
|
||||
textObj.lifeTime = lifeTime;
|
||||
textObj.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -113,32 +148,64 @@ namespace Managers
|
||||
/// </summary>
|
||||
/// <param name="sprites">动画的精灵帧数组。</param>
|
||||
/// <param name="position">动画在世界坐标系中的初始位置。</param>
|
||||
/// <param name="lifeTime">动画的生命周期(秒)。</param>
|
||||
/// <param name="fps">动画帧率。</param>
|
||||
/// <param name="xShift">X轴位移函数。</param>
|
||||
/// <param name="yShift">Y轴位移函数。</param>
|
||||
public void GenerateTemporaryAnimation(Sprite[] sprites, Vector3 position,float lifeTime = 3, float fps = 3, Func<float, float> xShift = null,
|
||||
Func<float, float> yShift = null)
|
||||
{
|
||||
if (_temporaryAnimationLevel == null)
|
||||
if (!_temporaryAnimationLevel)
|
||||
{
|
||||
Debug.LogError("TemporaryAnimationLevel is not initialized. Cannot generate non-UI animation.");
|
||||
return;
|
||||
}
|
||||
|
||||
xShift ??= defaultXoffset;
|
||||
yShift ??= defaultYoffset;
|
||||
var obj = Instantiate(temporaryAnimatorSpritePrefab, _temporaryAnimationLevel.transform);
|
||||
obj.transform.position = new Vector3(position.x,position.y);
|
||||
obj.Init(sprites, fps);
|
||||
obj.SetAnimationFunctions(xShift, yShift);
|
||||
obj.lifeTime = lifeTime;
|
||||
obj.gameObject.SetActive(true);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成一个非UI精灵临时动画,可指定父级Transform。
|
||||
/// </summary>
|
||||
/// <param name="sprites">动画的精灵帧数组。</param>
|
||||
/// <param name="position">动画在世界坐标系中的初始位置。</param>
|
||||
/// <param name="parent">动画的父级Transform。如果为null,则使用默认的非UI动画层。</param>
|
||||
/// <param name="lifeTime">动画的生命周期(秒)。</param>
|
||||
/// <param name="fps">动画帧率。</param>
|
||||
/// <param name="xShift">X轴位移函数。</param>
|
||||
/// <param name="yShift">Y轴位移函数。</param>
|
||||
public void GenerateTemporaryAnimation(Sprite[] sprites, Vector3 position, Transform parent, float lifeTime = 3, float fps = 3, Func<float, float> xShift = null,
|
||||
Func<float, float> yShift = null)
|
||||
{
|
||||
Transform actualParent = parent ?? _temporaryAnimationLevel?.transform;
|
||||
|
||||
if (actualParent == null)
|
||||
{
|
||||
Debug.LogError("TemporaryAnimationLevel or specified parent is not initialized. Cannot generate non-UI animation.");
|
||||
return;
|
||||
}
|
||||
|
||||
var obj = Instantiate(temporaryAnimatorSpritePrefab, actualParent);
|
||||
obj.transform.position = position; // 外部传入的position应视为世界坐标
|
||||
obj.Init(sprites, fps);
|
||||
obj.SetAnimationFunctions(xShift, yShift);
|
||||
obj.lifeTime = lifeTime;
|
||||
obj.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成一个UI文本临时动画。
|
||||
/// 此重载将传入的 <paramref name="position"/> 视为屏幕坐标 (如鼠标位置),并将其自动转换为Canvas局部坐标。
|
||||
/// </summary>
|
||||
/// <param name="str">动画的文本内容。</param>
|
||||
/// <param name="position">动画在UI坐标系(RectTransform.anchoredPosition)中的初始位置。</param>
|
||||
/// <param name="position">动画在屏幕坐标系中的初始位置(如鼠标位置)。</param>
|
||||
/// <param name="lifeTime">动画的生命周期(秒)。</param>
|
||||
/// <param name="fps">动画帧率。</param>
|
||||
/// <param name="xShift">X轴位移函数。</param>
|
||||
/// <param name="yShift">Y轴位移函数。</param>
|
||||
@ -150,22 +217,86 @@ namespace Managers
|
||||
Debug.LogError("TemporaryAnimationUILevel is not initialized. Cannot generate UI animation.");
|
||||
return;
|
||||
}
|
||||
|
||||
xShift ??= defaultXoffset;
|
||||
yShift ??= defaultYoffset;
|
||||
|
||||
var textObj = Instantiate(temporaryAnimatorUITextPrefab, _temporaryAnimationUILevel.transform);
|
||||
textObj.transform.position = position;
|
||||
|
||||
RectTransform canvasRectTransform = _temporaryAnimationUILevel.transform as RectTransform;
|
||||
if (canvasRectTransform != null)
|
||||
{
|
||||
// 获取Canvas组件,特别是RenderMode为ScreenSpaceCamera时需要worldCamera
|
||||
Canvas rootCanvas = canvasRectTransform.root.GetComponent<Canvas>();
|
||||
Camera eventCamera = null;
|
||||
if (rootCanvas.renderMode == RenderMode.ScreenSpaceCamera || rootCanvas.renderMode == RenderMode.WorldSpace)
|
||||
{
|
||||
eventCamera = rootCanvas.worldCamera; // 使用Canvas指定的相机
|
||||
if (eventCamera == null) // 如果Canvas没有指定相机,尝试主相机
|
||||
{
|
||||
eventCamera = Camera.main;
|
||||
}
|
||||
}
|
||||
|
||||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, position, eventCamera, out var localPoint))
|
||||
{
|
||||
textObj.rectTransform.anchoredPosition = localPoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
textObj.rectTransform.anchoredPosition = position;
|
||||
Debug.LogWarning("Failed to convert screen point to local point for UI text animation. Using raw position.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
textObj.rectTransform.anchoredPosition = position;
|
||||
Debug.LogWarning("TemporaryAnimationUILevel is not a RectTransform. Unable to convert screen point. Using raw position.");
|
||||
}
|
||||
|
||||
textObj.Init(str, fps);
|
||||
textObj.SetAnimationFunctions(xShift, yShift);
|
||||
textObj.lifeTime = lifeTime;
|
||||
textObj.gameObject.SetActive(true);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成一个UI文本临时动画,可指定父级RectTransform。
|
||||
/// 此重载将传入的 <paramref name="position"/> 视为相对于父级RectTransform的 anchoredPosition。
|
||||
/// </summary>
|
||||
/// <param name="str">动画的文本内容。</param>
|
||||
/// <param name="position">动画在UI坐标系(RectTransform.anchoredPosition)中的初始位置。</param>
|
||||
/// <param name="parent">动画的父级RectTransform。如果为null,则使用默认的UI动画层。</param>
|
||||
/// <param name="lifeTime">动画的生命周期(秒)。</param>
|
||||
/// <param name="fps">动画帧率。</param>
|
||||
/// <param name="xShift">X轴位移函数。</param>
|
||||
/// <param name="yShift">Y轴位移函数。</param>
|
||||
public void GenerateTemporaryAnimationUI(string str, Vector2 position, RectTransform parent, float lifeTime = 3, float fps = 3, Func<float, float> xShift = null,
|
||||
Func<float, float> yShift = null)
|
||||
{
|
||||
RectTransform actualParent = parent ?? (_temporaryAnimationUILevel?.transform as RectTransform);
|
||||
|
||||
if (actualParent == null)
|
||||
{
|
||||
Debug.LogError("TemporaryAnimationUILevel or specified parent is not initialized or not a RectTransform. Cannot generate UI animation.");
|
||||
return;
|
||||
}
|
||||
|
||||
var textObj = Instantiate(temporaryAnimatorUITextPrefab);
|
||||
textObj.rectTransform.SetParent(actualParent, false); // SetParent with worldPositionStay: false
|
||||
textObj.rectTransform.anchoredPosition = position;
|
||||
textObj.Init(str, fps);
|
||||
textObj.SetAnimationFunctions(xShift, yShift);
|
||||
textObj.lifeTime = lifeTime;
|
||||
textObj.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 生成一个UI精灵临时动画。
|
||||
/// 此重载将传入的 <paramref name="position"/> 视为屏幕坐标 (如鼠标位置),并将其自动转换为Canvas局部坐标。
|
||||
/// </summary>
|
||||
/// <param name="sprites">动画的精灵帧数组。</param>
|
||||
/// <param name="position">动画在UI坐标系(RectTransform.anchoredPosition)中的初始位置。</param>
|
||||
/// <param name="position">动画在屏幕坐标系中的初始位置(如鼠标位置)。</param>
|
||||
/// <param name="lifeTime">动画的生命周期(秒)。</param>
|
||||
/// <param name="fps">动画帧率。</param>
|
||||
/// <param name="xShift">X轴位移函数。</param>
|
||||
/// <param name="yShift">Y轴位移函数。</param>
|
||||
@ -178,13 +309,76 @@ namespace Managers
|
||||
return;
|
||||
}
|
||||
|
||||
xShift ??= defaultXoffset;
|
||||
yShift ??= defaultYoffset;
|
||||
var obj = Instantiate(temporaryAnimatorImageUIPrefab, _temporaryAnimationUILevel.transform);
|
||||
obj.transform.position = position;
|
||||
|
||||
RectTransform canvasRectTransform = _temporaryAnimationUILevel.transform as RectTransform;
|
||||
if (canvasRectTransform != null)
|
||||
{
|
||||
// 获取Canvas组件,特别是RenderMode为ScreenSpaceCamera时需要worldCamera
|
||||
Canvas rootCanvas = canvasRectTransform.root.GetComponent<Canvas>();
|
||||
Camera eventCamera = null;
|
||||
if (rootCanvas.renderMode == RenderMode.ScreenSpaceCamera || rootCanvas.renderMode == RenderMode.WorldSpace)
|
||||
{
|
||||
eventCamera = rootCanvas.worldCamera; // 使用Canvas指定的相机
|
||||
if (eventCamera == null) // 如果Canvas没有指定相机,尝试主相机
|
||||
{
|
||||
eventCamera = Camera.main;
|
||||
}
|
||||
}
|
||||
|
||||
Vector2 localPoint;
|
||||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, position, eventCamera, out localPoint))
|
||||
{
|
||||
obj.rectTransform.anchoredPosition = localPoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
obj.rectTransform.anchoredPosition = position;
|
||||
Debug.LogWarning("Failed to convert screen point to local point for UI sprite animation. Using raw position.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
obj.rectTransform.anchoredPosition = position;
|
||||
Debug.LogWarning("TemporaryAnimationUILevel is not a RectTransform. Unable to convert screen point. Using raw position.");
|
||||
}
|
||||
|
||||
obj.Init(sprites, fps);
|
||||
obj.SetAnimationFunctions(xShift, yShift);
|
||||
obj.lifeTime = lifeTime;
|
||||
obj.gameObject.SetActive(true);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成一个UI精灵临时动画,可指定父级RectTransform。
|
||||
/// 此重载将传入的 <paramref name="position"/> 视为相对于父级RectTransform的 anchoredPosition。
|
||||
/// </summary>
|
||||
/// <param name="sprites">动画的精灵帧数组。</param>
|
||||
/// <param name="position">动画在UI坐标系(RectTransform.anchoredPosition)中的初始位置。</param>
|
||||
/// <param name="parent">动画的父级RectTransform。如果为null,则使用默认的UI动画层。</param>
|
||||
/// <param name="lifeTime">动画的生命周期(秒)。</param>
|
||||
/// <param name="fps">动画帧率。</param>
|
||||
/// <param name="xShift">X轴位移函数。</param>
|
||||
/// <param name="yShift">Y轴位移函数。</param>
|
||||
public void GenerateTemporaryAnimationUI(Sprite[] sprites, Vector2 position, RectTransform parent, float lifeTime = 3, float fps = 3, Func<float, float> xShift = null,
|
||||
Func<float, float> yShift = null)
|
||||
{
|
||||
RectTransform actualParent = parent ?? (_temporaryAnimationUILevel?.transform as RectTransform);
|
||||
|
||||
if (actualParent == null)
|
||||
{
|
||||
Debug.LogError("TemporaryAnimationUILevel or specified parent is not initialized or not a RectTransform. Cannot generate UI animation.");
|
||||
return;
|
||||
}
|
||||
|
||||
var obj = Instantiate(temporaryAnimatorImageUIPrefab);
|
||||
obj.rectTransform.SetParent(actualParent, false); // SetParent with worldPositionStay: false
|
||||
obj.rectTransform.anchoredPosition = position;
|
||||
obj.Init(sprites, fps);
|
||||
obj.SetAnimationFunctions(xShift, yShift);
|
||||
obj.lifeTime = lifeTime;
|
||||
obj.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
|
||||
@ -198,20 +392,23 @@ namespace Managers
|
||||
var existingCanvas = FindFirstObjectByType<Canvas>();
|
||||
if (existingCanvas != null)
|
||||
{
|
||||
EnsureEventSystemExists(); // 即使Canvas已存在,也要确保EventSystem存在
|
||||
EnsureEventSystemExists();
|
||||
return existingCanvas;
|
||||
}
|
||||
|
||||
// 创建 Canvas GameObject
|
||||
var canvasGO = new GameObject("Canvas");
|
||||
canvasGO.layer = LayerMask.NameToLayer("UI"); // 建议将UI设置为UI层
|
||||
// 添加 Canvas 组件
|
||||
canvasGO.layer = LayerMask.NameToLayer("UI");
|
||||
var newCanvas = canvasGO.AddComponent<Canvas>();
|
||||
newCanvas.renderMode = RenderMode.ScreenSpaceOverlay; // 最常见的UI渲染模式
|
||||
// 添加其他必要的UI组件
|
||||
newCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
|
||||
// 确保有相机,以便RectTransformUtility.ScreenPointToLocalPointInRectangle可以工作
|
||||
// 对于ScreenSpaceOverlay,worldCamera可以为null,但为了通用性和健壮性,这里设置为Camera.main,
|
||||
// 以防Canvas.renderMode将来调整为ScreenSpaceCamera或WorldSpace
|
||||
newCanvas.worldCamera = Camera.main;
|
||||
|
||||
canvasGO.AddComponent<CanvasScaler>();
|
||||
canvasGO.AddComponent<GraphicRaycaster>();
|
||||
// 确保场景中存在 EventSystem,以便UI交互(按钮点击等)能正常工作
|
||||
EnsureEventSystemExists();
|
||||
return newCanvas;
|
||||
}
|
||||
|
Reference in New Issue
Block a user