Advertisement
Vyocs

Untitled

Oct 15th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6.  
  7. [System.Serializable]
  8. public class PlayerData {
  9.  
  10. public int EventNumbCollected;
  11. public int level;
  12. public int health;
  13. public float currentMission;
  14. public int Fortschritt;
  15. public float RedBar;
  16. public int TimerText;
  17. public int CoinCounterr;
  18. public Text CoinCounterrtext;
  19. ShopManager shopManager;
  20.  
  21. //public Image Fortschritsbar;
  22. void Start()
  23. {
  24.  
  25. }
  26.  
  27.  
  28. public PlayerData (Challenges challenges)
  29. {
  30.  
  31. Fortschritt = challenges.Fortschritt;
  32. currentMission = challenges.currentMission;
  33. // Fortschritsbar = player.Fortschritsbar;
  34. TimerText = challenges.TimerText;
  35. CoinCounterr = challenges.CoinCounterr;
  36. CoinCounterrtext = challenges.CoinCounterrtext;
  37. }
  38. }
  39.  
  40.  
  41. //////////////////////////////////////////////////////////////////////////////////////////
  42.  
  43.  
  44. using UnityEngine;
  45. using System.IO;
  46. using System.Runtime.Serialization.Formatters.Binary;
  47.  
  48.  
  49. public static class SaveSystem {
  50.  
  51. public static void SavePlayer (Challenges challenges)
  52. {
  53. BinaryFormatter formatter = new BinaryFormatter();
  54. string path = Application.persistentDataPath + "/Player.fun";
  55. FileStream stream = new FileStream(path, FileMode.Create);
  56.  
  57. PlayerData data = new PlayerData(challenges);
  58.  
  59. formatter.Serialize(stream, data);
  60. stream.Close();
  61.  
  62. }
  63.  
  64. public static PlayerData LoadPlayer()
  65. {
  66. string path = Application.persistentDataPath + "/Player.fun";
  67. if (File.Exists(path))
  68. {
  69. BinaryFormatter formatter = new BinaryFormatter();
  70. FileStream stream = new FileStream(path, FileMode.Open);
  71.  
  72. PlayerData data = formatter.Deserialize(stream) as PlayerData;
  73. stream.Close();
  74.  
  75. return data;
  76.  
  77. }
  78. else {
  79. return null;
  80. }
  81. }
  82. }
  83. ////////////////////////////////////////////////////////////////////////////////////////////////////
  84.  
  85.  
  86. using System.Collections;
  87. using System.Collections.Generic;
  88. using UnityEngine;
  89. using System;
  90. using System.IO;
  91. using System.Runtime.Serialization.Formatters.Binary;
  92.  
  93. [Serializable]
  94. public class SaveData{
  95. public bool[] isActive;
  96. public int[] highScores;
  97. public int[] stars;
  98.  
  99. }
  100.  
  101. public class GameData : MonoBehaviour {
  102.  
  103. public static GameData gameData;
  104. public SaveData saveData;
  105.  
  106. // Use this for initialization
  107. void Awake () {
  108. if(gameData == null)
  109. {
  110. DontDestroyOnLoad(this.gameObject);
  111. gameData = this;
  112. }else{
  113. Destroy(this.gameObject);
  114. }
  115. Load();
  116. }
  117.  
  118. private void Start()
  119. {
  120.  
  121. }
  122.  
  123. public void Save()
  124. {
  125.  
  126. //Create a binary formatter which can read binary files //Erstellen Sie einen binären Formatierer, der Binärdateien lesen kann.
  127. BinaryFormatter formatter = new BinaryFormatter();
  128.  
  129. //Create a route from the program to the file //Erstellen Sie eine Route vom Programm zur Datei.
  130. FileStream file = File.Create(Application.persistentDataPath + "/player.dat");
  131.  
  132. //Create a copy of save data //Erstellen einer Kopie der Sicherungsdaten
  133. SaveData data = new SaveData();
  134. data = saveData;
  135.  
  136. //Actually save the data in the file //Die Daten tatsächlich in der Datei speichern
  137. formatter.Serialize(file, data);
  138.  
  139. //Close the data stream //Schließen des Datenstroms
  140. file.Close();
  141.  
  142.  
  143.  
  144. }
  145.  
  146. public void Load()
  147. {
  148. //Check if the save game file exists //Überprüfen Sie, ob die gespeicherte Spieldatei existiert.
  149. if (File.Exists(Application.persistentDataPath + "/player.dat"))
  150. {
  151. //Create a Binary Formatter //Erstellen eines binären Formatierers
  152. BinaryFormatter formatter = new BinaryFormatter();
  153. FileStream file = File.Open(Application.persistentDataPath + "/player.dat", FileMode.Open);
  154. saveData = formatter.Deserialize(file) as SaveData;
  155. file.Close();
  156. // Debug.Log("Loaded");
  157. }
  158. else{
  159. saveData = new SaveData();
  160. saveData.isActive = new bool[100];
  161. saveData.stars = new int[100];
  162. saveData.highScores = new int[100];
  163. saveData.isActive[0] = true;
  164.  
  165. }
  166. }
  167.  
  168. private void OnApplicationQuit()
  169. {
  170. Save();
  171. }
  172.  
  173. private void OnDisable()
  174. {
  175. Save();
  176. }
  177.  
  178. // Update is called once per frame
  179. void Update () {
  180.  
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement