CreateNotification.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 System.Collections;
  9. using UnityEngine;
  10. using TMPro;
  11. using UnityEngine.UI;
  12. namespace Immersal.Samples.Mapping
  13. {
  14. [RequireComponent(typeof(RectTransform))]
  15. [RequireComponent(typeof(CanvasGroup))]
  16. public class CreateNotification : MonoBehaviour
  17. {
  18. [HideInInspector]
  19. public enum NotificationType { Success, Info, Warning, Error}
  20. [SerializeField]
  21. private RectTransform m_RectTransform = null;
  22. [SerializeField]
  23. private TextMeshProUGUI m_TextMeshProUGUI = null;
  24. [SerializeField]
  25. private Image m_Image = null;
  26. [SerializeField]
  27. private float m_DurationBeforeFadeOut = 0.5f;
  28. [SerializeField]
  29. private float m_FadeOutDuration = 1f;
  30. [SerializeField]
  31. private Sprite m_SuccessIcon = null;
  32. [SerializeField]
  33. private Sprite m_InfoIcon = null;
  34. [SerializeField]
  35. private Sprite m_WarningIcon = null;
  36. [SerializeField]
  37. private Sprite m_ErrorIcon = null;
  38. private int m_MinWidth = 192;
  39. private int m_MaxWidth = 800;
  40. private int m_MinHeight = 128;
  41. private int m_MaxHeight = 512;
  42. private int m_Padding = 32;
  43. public void SetIconAndText(NotificationType notificationType, string text)
  44. {
  45. m_TextMeshProUGUI.text = text;
  46. Sprite icon = m_InfoIcon;
  47. int iconSize = 64;
  48. switch (notificationType)
  49. {
  50. case NotificationType.Success:
  51. icon = m_SuccessIcon;
  52. break;
  53. case NotificationType.Info:
  54. icon = m_InfoIcon;
  55. break;
  56. case NotificationType.Warning:
  57. icon = m_WarningIcon;
  58. break;
  59. case NotificationType.Error:
  60. icon = m_ErrorIcon;
  61. break;
  62. default:
  63. icon = m_InfoIcon;
  64. break;
  65. }
  66. m_Image.sprite = icon;
  67. Vector2 size = m_TextMeshProUGUI.GetPreferredValues(m_TextMeshProUGUI.text);
  68. float lines = Mathf.Ceil(size.x / m_MaxWidth);
  69. int width = Mathf.Max(Mathf.Min((int)size.x, m_MaxWidth), m_MinWidth) + 3 * iconSize;
  70. int height = Mathf.Max(Mathf.Min((int)(size.y * lines), m_MaxHeight), m_MinHeight) + m_Padding;
  71. m_RectTransform.sizeDelta = new Vector2(width, height);
  72. m_TextMeshProUGUI.ForceMeshUpdate();
  73. StartCoroutine(FadeOut(m_FadeOutDuration, m_DurationBeforeFadeOut));
  74. }
  75. private void Start()
  76. {
  77. StartCoroutine(FadeOut(m_FadeOutDuration, m_DurationBeforeFadeOut));
  78. }
  79. IEnumerator FadeOut(float duration, float startAfter)
  80. {
  81. CanvasGroup canvasGroup = GetComponent<CanvasGroup>();
  82. for (float t = -startAfter; t < 1f; t += (Time.deltaTime / duration))
  83. {
  84. float alpha = Mathf.Lerp(1f, 0f, Mathf.Pow(Mathf.Max(t, 0f), 1.6f));
  85. canvasGroup.alpha = alpha;
  86. yield return null;
  87. }
  88. gameObject.SetActive(false);
  89. }
  90. }
  91. }