ActivityMessageUi.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using Sirenix.OdinInspector;
  7. namespace KairoEngine.UI.ActivityMessages
  8. {
  9. public class ActivityMessageUi : MonoBehaviour
  10. {
  11. public TextMeshProUGUI textUi;
  12. public Image buttonImage;
  13. public ActivityMessage message;
  14. public bool autoHide = true;
  15. public float hideTimer = 5f;
  16. private float hideCounter = 0f;
  17. public bool fadeIn = true;
  18. public bool fadeOut = true;
  19. public float fadeTimer = 0.5f;
  20. private float fadeCounter = 0f;
  21. private bool isFading = false;
  22. private bool hasFadedIn = false;
  23. private ActivityMessageController controller;
  24. private void Update()
  25. {
  26. if (fadeIn && !hasFadedIn)
  27. {
  28. fadeCounter += Time.deltaTime;
  29. Color color = textUi.color;
  30. color.a = fadeCounter / fadeTimer;
  31. textUi.color = color;
  32. buttonImage.color = color;
  33. if (fadeCounter >= fadeTimer)
  34. {
  35. fadeCounter = 0f;
  36. hasFadedIn = true;
  37. }
  38. }
  39. if (autoHide && hasFadedIn || fadeIn == false)
  40. {
  41. hideCounter += Time.deltaTime;
  42. if(hideCounter >= hideTimer)
  43. {
  44. hideCounter = 0f;
  45. isFading = true;
  46. }
  47. }
  48. if (fadeOut && isFading)
  49. {
  50. fadeCounter += Time.deltaTime;
  51. Color color = textUi.color;
  52. color.a = 1f - (fadeCounter / fadeTimer);
  53. textUi.color = color;
  54. buttonImage.color = color;
  55. if (fadeCounter >= fadeTimer)
  56. {
  57. fadeCounter = 0f;
  58. if(controller != null)
  59. {
  60. controller.messages.Remove(message);
  61. controller.activeMessages.Remove(this);
  62. }
  63. Destroy(this.gameObject);
  64. }
  65. }
  66. }
  67. public void OnClick()
  68. {
  69. isFading = true;
  70. }
  71. public void Setup(ActivityMessageController controller, ActivityMessage newMessage, bool sticky = false)
  72. {
  73. this.controller = controller;
  74. message = newMessage;
  75. textUi.text = message.text;
  76. autoHide = !sticky;
  77. }
  78. public void Setup(ActivityMessageController controller, ActivityMessage newMessage, float time, bool sticky = false)
  79. {
  80. this.controller = controller;
  81. message = newMessage;
  82. textUi.text = message.text;
  83. hideTimer = time;
  84. autoHide = !sticky;
  85. }
  86. public void Remove(ActivityMessageController controller)
  87. {
  88. this.controller = controller;
  89. autoHide = true;
  90. hideTimer = 0f;
  91. }
  92. }
  93. }