TimerScript.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class TimerScript : MonoBehaviour
  7. {
  8. Image circleImage;
  9. Color circleColor;
  10. public Text timerText;
  11. float time;
  12. float timeAmount;
  13. public bool mainTimer;
  14. bool timerRunning = false;
  15. Color finalColor = new Color32(130,0,16,220);
  16. Color startlColor = new Color32(16, 0, 255, 200);
  17. GameManagerScript gms;
  18. [SerializeField] GameObject dialogTimer;
  19. TimerScript dialogTimerScript;
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. dialogTimerScript = dialogTimer.GetComponent<TimerScript>();
  24. gms = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
  25. circleImage = this.GetComponent<Image>();
  26. circleColor = circleImage.color;
  27. timerText.text = time.ToString("F0");
  28. }
  29. public void StopTimer() {
  30. timerRunning = false;
  31. dialogTimerScript.timerRunning = false;
  32. }
  33. public void ResetTimer() {
  34. time = gms.QuestionTimer;
  35. // time = Database.Instance.GetQuestionTimer(gms.GameId, gms.GameMode);
  36. timeAmount = time;
  37. circleColor = Color.Lerp(startlColor, finalColor, 1 - (time / timeAmount));
  38. dialogTimerScript.time = time;
  39. dialogTimerScript.timeAmount = timeAmount;
  40. dialogTimerScript.circleImage.color = circleColor;
  41. dialogTimerScript.timerText.text = time.ToString("F0");
  42. circleImage.color = circleColor;
  43. timerText.text = time.ToString("F0");
  44. }
  45. // Update is called once per frame
  46. void Update()
  47. {
  48. if (timerRunning) {
  49. circleColor = circleImage.color;
  50. if (time > 0) {
  51. time -= Time.deltaTime;
  52. circleColor = Color.Lerp(startlColor, finalColor, 1 - (time / timeAmount));
  53. circleImage.color = circleColor;
  54. timerText.text = time.ToString("F0");
  55. } else {
  56. timerRunning = false;
  57. // send message to fail question
  58. if (mainTimer) {
  59. EventManager.TriggerEvent("TimerEvent");
  60. }
  61. }
  62. }
  63. }
  64. internal void StartTimer() {
  65. timerRunning = true;
  66. dialogTimerScript.timerRunning = true;
  67. }
  68. }