NotificationManager.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using UnityEngine;
  9. using TMPro;
  10. namespace Immersal.Samples.Mapping
  11. {
  12. public class NotificationManager : MonoBehaviour
  13. {
  14. [SerializeField]
  15. private GameObject m_Notification = null;
  16. private CreateNotification m_CreateNotification = null;
  17. public static NotificationManager Instance
  18. {
  19. get
  20. {
  21. #if UNITY_EDITOR
  22. if (instance == null && !Application.isPlaying)
  23. {
  24. instance = UnityEngine.Object.FindObjectOfType<NotificationManager>();
  25. }
  26. #endif
  27. if (instance == null)
  28. {
  29. Debug.LogError("No NotificationManager instance found. Ensure one exists in the scene.");
  30. }
  31. return instance;
  32. }
  33. }
  34. private static NotificationManager instance = null;
  35. void Awake()
  36. {
  37. if (instance == null)
  38. {
  39. instance = this;
  40. m_Notification.SetActive(false);
  41. m_CreateNotification = m_Notification.GetComponent<CreateNotification>();
  42. }
  43. if (instance != this)
  44. {
  45. Debug.LogError("There must be only one NotificationManager object in a scene.");
  46. UnityEngine.Object.DestroyImmediate(this);
  47. return;
  48. }
  49. }
  50. private void InitNotification(CreateNotification.NotificationType type, string text)
  51. {
  52. m_Notification.SetActive(true);
  53. m_CreateNotification.SetIconAndText(type, text);
  54. }
  55. public void GenerateNotification(string text)
  56. {
  57. InitNotification(CreateNotification.NotificationType.Info, text);
  58. }
  59. public void GenerateWarning(string text)
  60. {
  61. InitNotification(CreateNotification.NotificationType.Warning, text);
  62. }
  63. public void GenerateError(string text)
  64. {
  65. InitNotification(CreateNotification.NotificationType.Error, text);
  66. }
  67. public void GenerateSuccess(string text)
  68. {
  69. InitNotification(CreateNotification.NotificationType.Success, text);
  70. }
  71. }
  72. }