You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
1 month ago
|
|
||
|
using UnityEngine;
|
||
|
using TMPro;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
public class EmailUIController : MonoBehaviour
|
||
|
{
|
||
|
public TextMeshProUGUI senderInitial;
|
||
|
public Image senderBg;
|
||
|
public TextMeshProUGUI senderNameText;
|
||
|
public TextMeshProUGUI subjectText;
|
||
|
public TextMeshProUGUI previewText;
|
||
|
public TextMeshProUGUI timeOrDateText;
|
||
|
|
||
|
public Sprite[] backgroundSprites;
|
||
|
public EmailData data;
|
||
|
|
||
|
// Cache for consistency
|
||
|
[SerializeField, HideInInspector]
|
||
|
public string cachedInitial;
|
||
|
[SerializeField, HideInInspector]
|
||
|
public Sprite cachedSprite;
|
||
|
|
||
|
public void Setup(EmailData emailData)
|
||
|
{
|
||
|
data = emailData;
|
||
|
|
||
|
senderNameText.text = data.senderName;
|
||
|
subjectText.text = data.subject;
|
||
|
previewText.text = GeneratePreview(data.fullBodyText);
|
||
|
timeOrDateText.text = data.timeOrDate;
|
||
|
|
||
|
cachedInitial = data.senderName[0].ToString().ToUpper();
|
||
|
cachedSprite = backgroundSprites[Random.Range(0, backgroundSprites.Length)];
|
||
|
|
||
|
senderInitial.text = cachedInitial;
|
||
|
senderBg.sprite = cachedSprite;
|
||
|
}
|
||
|
|
||
|
string GeneratePreview(string body)
|
||
|
{
|
||
|
int max = 60;
|
||
|
string clean = body.Replace("\n", " ").Trim();
|
||
|
return clean.Length <= max ? clean : clean.Substring(0, max) + "...";
|
||
|
}
|
||
|
|
||
|
public void OnClick()
|
||
|
{
|
||
|
GameObject panel = Instantiate(Resources.Load<GameObject>("EmailOpenPanel"), transform.root);
|
||
|
EmailOpenPanel controller = panel.GetComponent<EmailOpenPanel>();
|
||
|
controller.Setup(data, cachedInitial, cachedSprite);
|
||
|
}
|
||
|
}
|