prinzbartu

GameData Script

Jan 6th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using UnityEngine.SceneManagement;
  6. using System.Runtime.CompilerServices;
  7.  
  8. [System.Serializable]
  9. public class HighScoreData
  10. {
  11. public int highscore1P;
  12. public int highscore2P;
  13. }
  14.  
  15. [System.Serializable]
  16. public class CurrencyData
  17. {
  18. public int LifeEssence;
  19. }
  20.  
  21. [System.Serializable]
  22. public class ShopData
  23. {
  24. public int HealthCost;
  25. }
  26.  
  27. [System.Serializable]
  28. public class PlayerData
  29. {
  30. public int HealthAmount;
  31. }
  32.  
  33. public class GameData : MonoBehaviour
  34. {
  35. public static GameData instance;
  36.  
  37. public HighScoreData highScoreData;
  38. public CurrencyData currencyData;
  39. public ShopData shopData;
  40. public PlayerData playerData;
  41.  
  42. void Awake()
  43. {
  44. if (instance == null)
  45. {
  46. instance = this;
  47. DontDestroyOnLoad(gameObject);
  48.  
  49. if (!PlayerPrefs.HasKey("FirstEverStartedGame"))
  50. {
  51. PlayerPrefs.SetInt("FirstEverStartedGame", 1);
  52.  
  53. currencyData.LifeEssence = 0;
  54. shopData.HealthCost = 1;
  55. playerData.HealthAmount = 4;
  56.  
  57. SaveCurrency(currencyData.LifeEssence);
  58. SaveShopData(shopData.HealthCost);
  59. SavePlayerData(playerData.HealthAmount);
  60. }
  61.  
  62. LoadHighScore();
  63. LoadCurrency();
  64. LoadShopData();
  65. LoadPlayerData();
  66. }
  67. else
  68. {
  69. Destroy(gameObject);
  70. }
  71. }
  72.  
  73. public void SaveHighScore(int newHighScore)
  74. {
  75. Scene currentScene = SceneManager.GetActiveScene();
  76.  
  77. string sceneName = currentScene.name;
  78.  
  79. if (sceneName == "MainGame1P")
  80. {
  81. highScoreData.highscore1P = newHighScore;
  82. string jsonData = JsonUtility.ToJson(highScoreData);
  83. File.WriteAllText("HighscoreData.json", jsonData);
  84. }
  85. else if (sceneName == "MainGame2P")
  86. {
  87. highScoreData.highscore2P = newHighScore;
  88. string jsonData = JsonUtility.ToJson(highScoreData);
  89. File.WriteAllText("HighscoreData.json", jsonData);
  90. }
  91. }
  92.  
  93. void LoadHighScore()
  94. {
  95. if (File.Exists("HighscoreData.json"))
  96. {
  97. string jsonData = File.ReadAllText("HighscoreData.json");
  98. highScoreData = JsonUtility.FromJson<HighScoreData>(jsonData);
  99. }
  100. }
  101.  
  102. public void SaveCurrency(int newLifeEssence)
  103. {
  104. currencyData.LifeEssence = newLifeEssence;
  105. string jsonData = JsonUtility.ToJson(currencyData);
  106. File.WriteAllText("CurrencyData.json", jsonData);
  107. }
  108.  
  109. void LoadCurrency()
  110. {
  111. if (File.Exists("CurrencyData.json"))
  112. {
  113. string jsonData = File.ReadAllText("CurrencyData.json");
  114. currencyData = JsonUtility.FromJson<CurrencyData>(jsonData);
  115. }
  116. }
  117.  
  118. public void SaveShopData(int newHealthCost)
  119. {
  120. shopData.HealthCost = newHealthCost;
  121. string jsonData = JsonUtility.ToJson(shopData);
  122. File.WriteAllText("ShopData.json", jsonData);
  123. }
  124.  
  125. void LoadShopData()
  126. {
  127. if (File.Exists("ShopData.json"))
  128. {
  129. string jsonData = File.ReadAllText("ShopData.json");
  130. shopData = JsonUtility.FromJson<ShopData>(jsonData);
  131. }
  132. }
  133.  
  134. public void SavePlayerData(int newHealthAmount)
  135. {
  136. playerData.HealthAmount = newHealthAmount;
  137. string jsonData = JsonUtility.ToJson(playerData);
  138. File.WriteAllText("PlayerData.json", jsonData);
  139. }
  140.  
  141. void LoadPlayerData()
  142. {
  143. if (File.Exists("PlayerData.json"))
  144. {
  145. string jsonData = File.ReadAllText("PlayerData.json");
  146. playerData = JsonUtility.FromJson<PlayerData>(jsonData);
  147. }
  148. }
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment