Advertisement
fuccpuff

Untitled

May 16th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3.  
  4. public class GameManager : MonoBehaviour
  5. {
  6. public goalScript blue, green, red, orange;
  7. private bool isGameOver = false;
  8. private GUIStyle gameOverStyle;
  9. private GUIStyle buttonStyle;
  10. private float elapsedTime = 0f;
  11.  
  12. void Start()
  13. {
  14. blue.OnSolve += CheckGameOver;
  15. green.OnSolve += CheckGameOver;
  16. red.OnSolve += CheckGameOver;
  17. orange.OnSolve += CheckGameOver;
  18. }
  19.  
  20. void Update()
  21. {
  22. if (!isGameOver)
  23. {
  24. elapsedTime += Time.deltaTime;
  25. }
  26. }
  27.  
  28. GUIStyle GetGameOverStyle()
  29. {
  30. if (gameOverStyle == null)
  31. {
  32. gameOverStyle = new GUIStyle(GUI.skin.box)
  33. {
  34. fontSize = 24,
  35. alignment = TextAnchor.MiddleCenter
  36. };
  37. }
  38. return gameOverStyle;
  39. }
  40.  
  41. GUIStyle GetButtonStyle()
  42. {
  43. if (buttonStyle == null)
  44. {
  45. buttonStyle = new GUIStyle(GUI.skin.button)
  46. {
  47. fontSize = 20,
  48. normal = { textColor = Color.white, background = MakeTex(2, 2, new Color(0.5f, 0.5f, 0.5f, 1.0f)) },
  49. hover = { background = MakeTex(2, 2, new Color(0.7f, 0.7f, 0.7f, 1.0f)) },
  50. active = { background = MakeTex(2, 2, new Color(0.3f, 0.3f, 0.3f, 1.0f)) }
  51. };
  52. }
  53. return buttonStyle;
  54. }
  55.  
  56. void CheckGameOver()
  57. {
  58. isGameOver = blue.isSolved && green.isSolved && red.isSolved && orange.isSolved;
  59. }
  60.  
  61. void OnGUI()
  62. {
  63. if (isGameOver)
  64. {
  65. Rect rect = new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 100);
  66. GUI.Box(rect, "Game Over", GetGameOverStyle());
  67. Rect rect2 = new Rect(Screen.width / 2 - 50, Screen.height / 2 + 55, 100, 50);
  68. if (GUI.Button(rect2, "Restart", GetButtonStyle()))
  69. {
  70. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  71. }
  72.  
  73. Rect rect3 = new Rect(Screen.width / 2 - 100, Screen.height / 2 + 110, 200, 50);
  74. GUI.Box(rect3, "Time: " + elapsedTime.ToString("F2") + " seconds", GetGameOverStyle());
  75. }
  76. else
  77. {
  78. Rect rect4 = new Rect(10, 10, 300, 50);
  79. GUI.Box(rect4, "Time: " + elapsedTime.ToString("F2") + " seconds", GetGameOverStyle());
  80. }
  81. }
  82.  
  83. void OnDestroy()
  84. {
  85. blue.OnSolve -= CheckGameOver;
  86. green.OnSolve -= CheckGameOver;
  87. red.OnSolve -= CheckGameOver;
  88. orange.OnSolve -= CheckGameOver;
  89. }
  90.  
  91. Texture2D MakeTex(int width, int height, Color col)
  92. {
  93. Color[] pix = new Color[width * height];
  94. for (int i = 0; i < pix.Length; ++i)
  95. {
  96. pix[i] = col;
  97. }
  98. Texture2D result = new Texture2D(width, height);
  99. result.SetPixels(pix);
  100. result.Apply();
  101. return result;
  102. }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement