SanAndreasUnity/Assets/Scripts/Utilities/UI/OnScreenMessageManager.cs

61 lines
1.2 KiB
C#
Raw Normal View History

2020-05-18 22:57:16 +00:00
using UnityEngine;
using System.Collections.Generic;
namespace SanAndreasUnity.Utilities
{
public class OnScreenMessageManager : MonoBehaviour
{
2020-06-06 16:09:26 +00:00
public GameObject messagePrefab;
public RectTransform messagesContainer;
2020-05-18 22:57:16 +00:00
private List<OnScreenMessage> m_onScreenMessages = new List<OnScreenMessage>();
public IReadOnlyList<OnScreenMessage> Messages => m_onScreenMessages;
2020-06-06 16:09:26 +00:00
public int messagePoolSize = 10;
2020-05-18 22:57:16 +00:00
public static OnScreenMessageManager Instance { get; private set; }
void Awake()
{
Instance = this;
}
void Update()
{
2020-06-06 16:09:26 +00:00
m_onScreenMessages.RemoveDeadObjects();
2020-05-18 22:57:16 +00:00
foreach (var msg in m_onScreenMessages)
{
msg.timeLeft -= Time.deltaTime;
2020-06-06 16:09:26 +00:00
msg.ScreenPos += msg.velocity * Time.deltaTime;
if (msg.timeLeft <= 0)
{
2020-06-06 16:41:50 +00:00
Object.Destroy(msg.gameObject);
2020-06-06 16:09:26 +00:00
}
2020-05-18 22:57:16 +00:00
}
}
2020-06-06 16:09:26 +00:00
public OnScreenMessage CreateMessage()
2020-05-18 22:57:16 +00:00
{
2020-06-06 16:09:26 +00:00
messagePrefab.GetComponentOrThrow<OnScreenMessage>();
2020-05-18 22:57:16 +00:00
2020-06-06 16:09:26 +00:00
GameObject messageGo = Object.Instantiate(messagePrefab);
messageGo.transform.SetParent(messagesContainer, false);
2020-05-18 22:57:16 +00:00
2020-06-06 16:09:26 +00:00
OnScreenMessage message = messageGo.GetComponent<OnScreenMessage>();
2020-05-18 22:57:16 +00:00
2020-06-06 16:09:26 +00:00
m_onScreenMessages.Add(message);
2020-05-18 22:57:16 +00:00
2020-06-06 16:09:26 +00:00
return message;
2020-05-18 22:57:16 +00:00
}
}
}