53 lines
1.9 KiB
C#
53 lines
1.9 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Continentis.MainGame.UI
|
|
{
|
|
public class InformationBox : UIElementBase
|
|
{
|
|
public RectTransform mainBox;
|
|
public Image titleBackground, contentBackground;
|
|
public TMP_Text titleText, contentText;
|
|
|
|
/// <summary>
|
|
/// 卡牌的关键词和描述
|
|
/// </summary>
|
|
public void Initialize(string title, string content)
|
|
{
|
|
titleText.text = title;
|
|
contentText.text = content;
|
|
SetBackground();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Buff的名称和描述
|
|
/// </summary>
|
|
public void Initialize(string title, string content, Vector2 basePosition)
|
|
{
|
|
titleText.text = title;
|
|
contentText.text = content;
|
|
|
|
SetBackground();
|
|
SetPosition(basePosition);
|
|
}
|
|
|
|
private void SetBackground()
|
|
{
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(mainBox);
|
|
titleBackground.rectTransform.sizeDelta = new Vector2(0, titleText.preferredHeight + 10);
|
|
contentBackground.rectTransform.sizeDelta = new Vector2(0, contentText.preferredHeight + 10);
|
|
LayoutRebuilder.ForceRebuildLayoutImmediate(mainBox);
|
|
rectTransform.sizeDelta = mainBox.sizeDelta;
|
|
}
|
|
|
|
private void SetPosition(Vector2 basePosition)
|
|
{
|
|
float xOffset = basePosition.x < 0 ? rectTransform.sizeDelta.x / 2 : -rectTransform.sizeDelta.x / 2;
|
|
float yOffset = basePosition.y < Screen.height * 0.25f ? rectTransform.sizeDelta.y / 2 : -rectTransform.sizeDelta.y / 2;
|
|
rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
|
|
rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
|
|
rectTransform.anchoredPosition = new Vector2(basePosition.x + xOffset, basePosition.y + yOffset);
|
|
}
|
|
}
|
|
} |