ActivityMessageUi.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. namespace KairoEngine.UI.ActivityMessages
  9. {
  10. public class ActivityMessageUi : MonoBehaviour
  11. {
  12. public TextMeshProUGUI textUi;
  13. public Image iconImage;
  14. public Image buttonImage;
  15. public ActivityMessage message;
  16. public bool autoHide = true;
  17. public float hideTimer = 5f;
  18. private float hideCounter = 0f;
  19. public bool fadeIn = true;
  20. public bool fadeOut = true;
  21. public bool hideOnClick = true;
  22. public float fadeTimer = 0.5f;
  23. private float fadeCounter = 0f;
  24. private bool isFading = false;
  25. private bool hasFadedIn = false;
  26. private ActivityMessageController controller;
  27. // private void Update()
  28. // {
  29. // if (fadeIn && !hasFadedIn)
  30. // {
  31. // fadeCounter += Time.deltaTime;
  32. // Color color = textUi.color;
  33. // color.a = fadeCounter / fadeTimer;
  34. // textUi.color = color;
  35. // buttonImage.color = color;
  36. // if (fadeCounter >= fadeTimer)
  37. // {
  38. // fadeCounter = 0f;
  39. // hasFadedIn = true;
  40. // }
  41. // }
  42. // if (autoHide && hasFadedIn || fadeIn == false)
  43. // {
  44. // hideCounter += Time.deltaTime;
  45. // if(hideCounter >= hideTimer)
  46. // {
  47. // hideCounter = 0f;
  48. // isFading = true;
  49. // }
  50. // }
  51. // if (fadeOut && isFading)
  52. // {
  53. // fadeCounter += Time.deltaTime;
  54. // Color color = textUi.color;
  55. // color.a = 1f - (fadeCounter / fadeTimer);
  56. // textUi.color = color;
  57. // buttonImage.color = color;
  58. // if (fadeCounter >= fadeTimer)
  59. // {
  60. // fadeCounter = 0f;
  61. // if(controller != null)
  62. // {
  63. // //controller.messages.Remove(message);
  64. // controller.activeMessages.Remove(this);
  65. // }
  66. // Destroy(this.gameObject);
  67. // }
  68. // }
  69. // }
  70. public void OnClick()
  71. {
  72. if(message.action != null) message.action();
  73. if(hideOnClick)
  74. {
  75. //isFading = true;
  76. Remove();
  77. }
  78. }
  79. public void Setup(ActivityMessageController controller, ActivityMessage newMessage)
  80. {
  81. this.controller = controller;
  82. message = newMessage;
  83. textUi.text = message.text;
  84. hideTimer = message.time;
  85. autoHide = !message.sticky;
  86. hideOnClick = message.hideOnClick;
  87. if(iconImage != null && message.icon != null)
  88. {
  89. iconImage.sprite = message.icon;
  90. iconImage.gameObject.SetActive(true);
  91. }
  92. else iconImage.gameObject.SetActive(false);
  93. if(!message.sticky)
  94. {
  95. Timer.ExecuteRealTime(message.time * 1000, () => {
  96. Remove();
  97. });
  98. }
  99. }
  100. public void Remove()
  101. {
  102. controller.RemoveActivityMessageFromList(message);
  103. if(this == null) return;
  104. UiAnimator uiAnimator = gameObject.GetComponent<UiAnimator>();
  105. if(uiAnimator != null)
  106. {
  107. uiAnimator.Disable();
  108. Timer.ExecuteRealTime(uiAnimator.animationTime * 1000, () => {
  109. if(this != null) Destroy(this.gameObject);
  110. });
  111. }
  112. else if(this != null) Destroy(this.gameObject);
  113. }
  114. }
  115. }