Advertisement
Jon50

Untitled

Sep 15th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. //SCENELOADER
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4.  
  5. public class SceneLoader : MonoBehaviour
  6. {
  7. public void LoadNextScene()
  8. {
  9. int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
  10.  
  11. SceneManager.LoadScene(currentSceneIndex + 1);
  12. }
  13.  
  14. public void LoadStartScene()
  15. {
  16. SceneManager.LoadScene(0);
  17.  
  18. FindObjectOfType<GameSession>().ResetGame();
  19. }
  20.  
  21. public void QuitGame()
  22. {
  23. Application.Quit();
  24. }
  25.  
  26. //This will return the current scene build index
  27. public int GetSceneIndex()
  28. {
  29. return SceneManager.GetActiveScene().buildIndex;
  30. }
  31. }
  32.  
  33.  
  34.  
  35.  
  36. //SCORE
  37. using UnityEngine;
  38. using TMPro;
  39.  
  40. public class Score : MonoBehaviour
  41. {
  42. [SerializeField]
  43. int pointsPerBlockDestroyed = 83;
  44.  
  45. [SerializeField]
  46. int currentScore = 0;
  47.  
  48. [SerializeField]
  49. TextMeshProUGUI scoreText;
  50.  
  51. //Get the SceneLoader to be able to call GetSceneIndex()
  52. //Check SceneLoader script
  53. [SerializeField]
  54. SceneLoader sceneLoader = null;
  55.  
  56. //Use an array to have all the different points based on the current level
  57. [SerializeField]
  58. int[] points = null;
  59.  
  60. //To check if we changed scenes and get the correct points for the current scene
  61. private int checkSceneIndex = 0;
  62.  
  63. private void Start()
  64. {
  65. //Get the SceneLoader component
  66. sceneLoader = sceneLoader.GetComponent<SceneLoader>();
  67.  
  68. //Get the current scene index and assign the points
  69. pointsPerBlockDestroyed = points[sceneLoader.GetSceneIndex()];
  70.  
  71. //Get the current scene index
  72. checkSceneIndex = sceneLoader.GetSceneIndex();
  73.  
  74. scoreText.text = currentScore.ToString();
  75. }
  76.  
  77. private void Update()
  78. {
  79. if (Input.GetKeyDown(KeyCode.U))
  80. {
  81. AddToScore();
  82. }
  83.  
  84. //Get the current scene index, if the has scene changed,
  85. //then checkSceneIndex is not equal to currentSceneIndex,
  86. //if that's the case, update the points
  87. int currentSceneIndex = sceneLoader.GetSceneIndex();
  88.  
  89. if (checkSceneIndex != currentSceneIndex)
  90. {
  91. pointsPerBlockDestroyed = points[currentSceneIndex];
  92. checkSceneIndex = currentSceneIndex;
  93. }
  94. }
  95.  
  96. public void AddToScore()
  97. {
  98. currentScore += pointsPerBlockDestroyed;
  99. scoreText.text = currentScore.ToString();
  100. }
  101. }
  102.  
  103.  
  104.  
  105.  
  106. //GAMESESSION
  107. using UnityEngine;
  108.  
  109. public class GameSession : MonoBehaviour
  110. {
  111. // config params
  112. [Range(0.1f, 10f)]
  113. [SerializeField]
  114. float gameSpeed = 1f;
  115.  
  116. [SerializeField]
  117. bool isAutoPlayEnabled;
  118.  
  119. private void Awake()
  120. {
  121. int gameStatusCount = FindObjectsOfType<GameSession>().Length;
  122. if (gameStatusCount > 1)
  123. {
  124. gameObject.SetActive(false);
  125. Destroy(gameObject);
  126. }
  127. else
  128. {
  129. DontDestroyOnLoad(gameObject);
  130. }
  131. }
  132.  
  133. void Update()
  134. {
  135. Time.timeScale = gameSpeed;
  136. }
  137.  
  138. public void ResetGame()
  139. {
  140. Destroy(gameObject);
  141.  
  142. }
  143. public bool IsAutoPlayEnabled()
  144. {
  145. return isAutoPlayEnabled;
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement