131 lines
3.7 KiB
C#
131 lines
3.7 KiB
C#
using UnityEngine;
|
||
using Logic;
|
||
using RuntimeData;
|
||
using UnityEngine.UI;
|
||
using Animancer;
|
||
using TMPro;
|
||
|
||
public class MessageUI
|
||
{
|
||
private Main _main;
|
||
private MapData _mapData;
|
||
public GameObject ROMessageUI;
|
||
|
||
public bool NeedShow = false; // 外部可设置,控制显示状态
|
||
|
||
private bool _isShowing = false; // 当前逻辑状态
|
||
private bool _isAnimating = false; // 是否正在播放动画
|
||
private float _fadeDuration = 0.2f;
|
||
|
||
public MessageUI(Main main, MapData mapData)
|
||
{
|
||
_main = main;
|
||
_mapData = mapData;
|
||
ROMessageUI = _main.UIManager.ROUIManager.transform.Find("MessagePanel").gameObject;
|
||
ROMessageUI.gameObject.SetActive(false);
|
||
}
|
||
|
||
public void Update()
|
||
{
|
||
if (_isAnimating) return;
|
||
|
||
if (NeedShow && !ROMessageUI.activeSelf)
|
||
{
|
||
Show();
|
||
}
|
||
else if (!NeedShow && ROMessageUI.activeSelf)
|
||
{
|
||
Hide();
|
||
}
|
||
}
|
||
|
||
public void AddMessageData(CenterMessageData data)
|
||
{
|
||
// 找到 Content 容器
|
||
Transform content = ROMessageUI.transform
|
||
.Find("MsgList/ScrollView/Viewport/Content");
|
||
if (content == null)
|
||
{
|
||
Debug.LogError("Content not found in MessagePanel hierarchy.");
|
||
return;
|
||
}
|
||
|
||
// 加载并实例化预制体
|
||
GameObject prefab = Resources.Load<GameObject>("Prefab/UI/MessageItem");
|
||
if (prefab == null)
|
||
{
|
||
Debug.LogError("MessageItem prefab not found in Resources/Prefab/UI.");
|
||
return;
|
||
}
|
||
|
||
GameObject instance = GameObject.Instantiate(prefab, content);
|
||
instance.transform.SetSiblingIndex(0);
|
||
|
||
// 设置图标 sprite
|
||
Image iconImage = instance.transform.Find("Icon/Mask/Img")?.GetComponent<Image>();
|
||
if (iconImage != null)
|
||
{
|
||
iconImage.sprite = data.Sprite;
|
||
}
|
||
|
||
// 设置类型文本(用来显示 data.Type)
|
||
TextMeshProUGUI typeText = instance.transform.Find("Title/Text")?.GetComponent<TextMeshProUGUI>();
|
||
if (typeText != null)
|
||
{
|
||
typeText.text = data.HasChat ? "遭遇" : "奇观";
|
||
}
|
||
|
||
// 设置标题和信息文本
|
||
TextMeshProUGUI titleText = instance.transform.Find("Info/TextTitle")?.GetComponent<TextMeshProUGUI>();
|
||
if (titleText != null)
|
||
{
|
||
titleText.text = data.Title;
|
||
}
|
||
|
||
TextMeshProUGUI messageText = instance.transform.Find("Info/TextDesc")?.GetComponent<TextMeshProUGUI>();
|
||
if (messageText != null)
|
||
{
|
||
messageText.text = data.Message;
|
||
}
|
||
}
|
||
|
||
public void Show()
|
||
{
|
||
if (_isShowing || _isAnimating) return;
|
||
|
||
_isShowing = true;
|
||
_isAnimating = true;
|
||
ROMessageUI.SetActive(true);
|
||
|
||
var animancer = ROMessageUI.GetComponent<AnimancerComponent>();
|
||
var fadeIn = Resources.Load<AnimationClip>("Animations/UI/MessagePanelFadeIn");
|
||
if (fadeIn != null)
|
||
{
|
||
animancer.Play(fadeIn);
|
||
}
|
||
|
||
Timer.Instance.TimerRegister(ROMessageUI, () => { _isAnimating = false; }, _fadeDuration);
|
||
}
|
||
|
||
public void Hide()
|
||
{
|
||
if (!_isShowing || _isAnimating) return;
|
||
|
||
_isShowing = false;
|
||
_isAnimating = true;
|
||
|
||
var animancer = ROMessageUI.GetComponent<AnimancerComponent>();
|
||
var fadeOut = Resources.Load<AnimationClip>("Animations/UI/MessagePanelFadeOut");
|
||
if (fadeOut != null)
|
||
{
|
||
animancer.Play(fadeOut);
|
||
}
|
||
|
||
Timer.Instance.TimerRegister(ROMessageUI, () =>
|
||
{
|
||
ROMessageUI.SetActive(false);
|
||
_isAnimating = false;
|
||
}, _fadeDuration);
|
||
}
|
||
}
|