2023-08-08 16:11:25 +00:00
using System.Collections.Generic ;
using TMPro ;
using UnityEngine ;
public class MessageBoxController : MonoBehaviour
{
2023-08-15 10:47:14 +00:00
public int maxMessageNum = 50 ;
public string defaultColor = "white" ;
2023-08-22 17:58:50 +00:00
public string warningColor = "#ffa500ff" ;
public string errorColor = "#800000ff" ;
2023-10-12 13:48:02 +00:00
public string goodColor = "#00ff00ff" ;
2023-08-08 16:11:25 +00:00
public GameObject messagePanelObj ;
public GameObject messageTextPrefab ;
[SerializeField]
private List < Message > messages = new List < Message > ( ) ;
2023-09-14 11:13:53 +00:00
/// <summary>
/// Pushes a simple message to the message list.
/// </summary>
/// <param name="text">The message text.</param>
/// <remarks>
/// This method pushes a simple text message to the message list and handles message overflow to ensure that the message list does not grow indefinitely.
/// </remarks>
2023-08-08 16:11:25 +00:00
public void PushMessage ( string text )
{
2023-08-15 10:47:14 +00:00
// push simple message to message list
MessageOverflowHandler ( ) ;
2023-08-08 16:11:25 +00:00
Message newMessage = new Message ( ) ;
newMessage . text = text ;
GameObject newText = Instantiate ( messageTextPrefab , messagePanelObj . transform ) ;
newMessage . textObject = newText . GetComponent < TextMeshProUGUI > ( ) ;
newMessage . textObject . text = newMessage . text ;
messages . Add ( newMessage ) ;
}
2023-09-14 11:13:53 +00:00
/// <summary>
/// Pushes multi-color messages to the message list.
/// </summary>
/// <param name="messageList">The list of message texts.</param>
/// <param name="colorList">The list of colors.</param>
/// <remarks>
/// This method pushes multi-color text messages to the message list and handles message overflow to ensure that the message list does not grow indefinitely.
/// If the lengths of the message text list and the color list do not match, it either removes excess colors or adds white color to the extra messages.
/// </remarks>
2023-08-15 10:47:14 +00:00
public void PushMessage ( List < string > messageList , List < string > colorList )
{
// check messages and colors list length match
if ( messageList . Count ! = colorList . Count )
{
2023-09-07 22:15:24 +00:00
// delete extra colors or add white color to extra messages
2023-08-15 10:47:14 +00:00
if ( messageList . Count > colorList . Count )
{
2023-09-07 22:15:24 +00:00
while ( messageList . Count > colorList . Count )
2023-08-15 10:47:14 +00:00
{
colorList . Add ( defaultColor ) ;
}
}
else
{
colorList . RemoveRange ( messageList . Count , colorList . Count - messageList . Count ) ;
}
}
MessageOverflowHandler ( ) ;
Message newMessage = new Message ( ) ;
newMessage . text = "" ;
// assemble message text with color
for ( int i = 0 ; i < messageList . Count ; i + + )
{
newMessage . text + = "<color=" + colorList [ i ] + ">" + messageList [ i ] + "</color>" ;
}
GameObject newText = Instantiate ( messageTextPrefab , messagePanelObj . transform ) ;
newMessage . textObject = newText . GetComponent < TextMeshProUGUI > ( ) ;
newMessage . textObject . text = newMessage . text ;
messages . Add ( newMessage ) ;
}
2023-08-08 16:11:25 +00:00
[System.Serializable]
public class Message
{
public string text ;
public TMPro . TextMeshProUGUI textObject ;
}
2023-08-15 10:47:14 +00:00
private void MessageOverflowHandler ( )
{
// destroy the oldest message if message list is full
if ( messages . Count > = maxMessageNum )
{
Destroy ( messages [ 0 ] . textObject . gameObject ) ;
messages . RemoveAt ( 0 ) ;
}
}
2023-08-08 16:11:25 +00:00
}