Files
Deroc_Virtual_3D/Assets/FPS/Scripts/UI/NotificationHUDManager.cs
T
Alex38Lyon 878ea46cac update
2025-06-03 12:00:47 +02:00

63 lines
2.1 KiB
C#

using Unity.FPS.Game;
using Unity.FPS.Gameplay;
using UnityEngine;
namespace Unity.FPS.UI
{
public class NotificationHUDManager : MonoBehaviour
{
[Tooltip("UI panel containing the layoutGroup for displaying notifications")]
public RectTransform NotificationPanel;
[Tooltip("Prefab for the notifications")]
public GameObject NotificationPrefab;
void Awake()
{
PlayerWeaponsManager playerWeaponsManager = FindFirstObjectByType<PlayerWeaponsManager>();
DebugUtility.HandleErrorIfNullFindObject<PlayerWeaponsManager, NotificationHUDManager>(playerWeaponsManager,
this);
playerWeaponsManager.OnAddedWeapon += OnPickupWeapon;
Jetpack jetpack = FindFirstObjectByType<Jetpack>();
DebugUtility.HandleErrorIfNullFindObject<Jetpack, NotificationHUDManager>(jetpack, this);
jetpack.OnUnlockJetpack += OnUnlockJetpack;
EventManager.AddListener<ObjectiveUpdateEvent>(OnObjectiveUpdateEvent);
}
void OnObjectiveUpdateEvent(ObjectiveUpdateEvent evt)
{
if (!string.IsNullOrEmpty(evt.NotificationText))
CreateNotification(evt.NotificationText);
}
void OnPickupWeapon(WeaponController weaponController, int index)
{
if (index != 0)
CreateNotification("Picked up weapon : " + weaponController.WeaponName);
}
void OnUnlockJetpack(bool unlock)
{
CreateNotification("Jetpack unlocked");
}
public void CreateNotification(string text)
{
GameObject notificationInstance = Instantiate(NotificationPrefab, NotificationPanel);
notificationInstance.transform.SetSiblingIndex(0);
NotificationToast toast = notificationInstance.GetComponent<NotificationToast>();
if (toast)
{
toast.Initialize(text);
}
}
void OnDestroy()
{
EventManager.RemoveListener<ObjectiveUpdateEvent>(OnObjectiveUpdateEvent);
}
}
}