TimerScript.cs 2.3 KB

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