evershade/Assets/Scripts/Managers/NotificationManager.cs

71 lines
1.9 KiB
C#

using FishNet.Connection;
using FishNet.Object;
using TMPro;
using UnityEngine;
namespace Managers
{
// each player will have its own instance of notification maanger.
public class NotificationManager: NetworkBehaviour
{
[SerializeField] private GameObject notificationPrefab;
[SerializeField] private Transform notificationParent;
private GameObject _currentNotification;
[Server]
public void SendTargetedNotification(string message, NetworkConnection target)
{
TargetShowNotification(target, message);
}
[Server]
public void BroadcastNotification(string message)
{
ShowNotificationObserverRpc(message);
}
[ObserversRpc]
private void ShowNotificationObserverRpc(string message)
{
ShowNotification(message);
}
[TargetRpc]
private void TargetShowNotification(NetworkConnection target, string message)
{
ShowNotification(message);
}
private void ShowNotification(string message)
{
if (_currentNotification != null)
{
Destroy(_currentNotification);
}
_currentNotification = Instantiate(notificationPrefab, notificationParent);
TMP_Text notificationText = _currentNotification.GetComponentInChildren<TMP_Text>();
if (notificationText != null)
{
notificationText.text = message;
}
StartCoroutine(AutoHideNotification(5f));
}
private System.Collections.IEnumerator AutoHideNotification(float delay)
{
yield return new WaitForSeconds(delay);
if (_currentNotification != null)
{
Destroy(_currentNotification);
_currentNotification = null;
}
}
}
}