TimerScript.cs 2.3 KB

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