NRNotificationWindow.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using System;
  12. namespace NRKernal
  13. {
  14. /// <summary> Form for viewing the nr notification. </summary>
  15. public class NRNotificationWindow : MonoBehaviour
  16. {
  17. /// <summary> (Serializable) information about the notification. </summary>
  18. [Serializable]
  19. public struct NotificationInfo
  20. {
  21. /// <summary> The sprite. </summary>
  22. public Sprite sprite;
  23. /// <summary> The title. </summary>
  24. public string title;
  25. /// <summary> The message. </summary>
  26. public string message;
  27. }
  28. /// <summary> Information describing the high level. </summary>
  29. public NotificationInfo m_HighLevelInfo;
  30. /// <summary> Information describing the middle level. </summary>
  31. public NotificationInfo m_MiddleLevelInfo;
  32. /// <summary> The icon. </summary>
  33. [SerializeField] Image m_Icon;
  34. /// <summary> The title. </summary>
  35. [SerializeField] Text m_Title;
  36. /// <summary> The message. </summary>
  37. [SerializeField] Text m_Message;
  38. /// <summary> The confirm control. </summary>
  39. [SerializeField] Button m_ConfirmBtn;
  40. protected NRNotificationListener.Level m_Level = NRNotificationListener.Level.Low;
  41. protected event Action OnConfirm;
  42. protected float m_Duration = 2f;
  43. private string m_TitleExtra;
  44. private string m_MessageExtra;
  45. /// <summary> Fill data. </summary>
  46. /// <param name="level"> The level.</param>
  47. /// <param name="duration"> (Optional) The duration.</param>
  48. public virtual NRNotificationWindow Build()
  49. {
  50. NotificationInfo info;
  51. switch (m_Level)
  52. {
  53. case NRNotificationListener.Level.High:
  54. info = m_HighLevelInfo;
  55. break;
  56. case NRNotificationListener.Level.Middle:
  57. info = m_MiddleLevelInfo;
  58. m_ConfirmBtn?.gameObject.SetActive(false);
  59. break;
  60. case NRNotificationListener.Level.Low:
  61. default:
  62. GameObject.Destroy(gameObject);
  63. return this;
  64. }
  65. m_Icon.sprite = info.sprite;
  66. if (!string.IsNullOrEmpty(m_TitleExtra))
  67. {
  68. m_Title.text = m_TitleExtra;
  69. }
  70. else
  71. {
  72. m_Title.text = info.title;
  73. }
  74. if (!string.IsNullOrEmpty(m_MessageExtra))
  75. {
  76. m_Message.text = m_MessageExtra;
  77. }
  78. else
  79. {
  80. m_Message.text = info.message;
  81. }
  82. m_ConfirmBtn?.onClick.AddListener(() =>
  83. {
  84. OnConfirm?.Invoke();
  85. AutoDestroy();
  86. });
  87. if (m_Duration > 0)
  88. {
  89. Invoke("AutoDestroy", m_Duration);
  90. }
  91. return this;
  92. }
  93. public NRNotificationWindow SetTitle(string title)
  94. {
  95. this.m_TitleExtra = title;
  96. return this;
  97. }
  98. public NRNotificationWindow SetContent(string content)
  99. {
  100. this.m_MessageExtra = content;
  101. return this;
  102. }
  103. public NRNotificationWindow SetLevle(NRNotificationListener.Level level)
  104. {
  105. this.m_Level = level;
  106. return this;
  107. }
  108. public NRNotificationWindow SetConfirmAction(Action callback)
  109. {
  110. this.OnConfirm += callback;
  111. return this;
  112. }
  113. public NRNotificationWindow SetDuration(float duration)
  114. {
  115. this.m_Duration = duration;
  116. return this;
  117. }
  118. /// <summary> Automatic destroy. </summary>
  119. private void AutoDestroy()
  120. {
  121. GameObject.Destroy(gameObject);
  122. }
  123. }
  124. }