Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class GameController : MonoBehaviour {
  6.  
  7. // Make global
  8. public static GameController Instance {
  9. get;
  10. set;
  11. }
  12.  
  13. void Awake () {
  14. DontDestroyOnLoad (transform.gameObject);
  15. Instance = this;
  16. }
  17.  
  18. void Start() {
  19. //Load first game scene (probably main menu)
  20. Application.LoadLevel(2);
  21. }
  22.  
  23. // Data persisted between scenes
  24. public int exp = 0;
  25. public int armor = 0;
  26. public int weapon = 0;
  27. //...
  28. }
  29.  
  30. private GameController gameController = GameController.Instance;
  31.  
  32. public static class PlayerStats
  33. {
  34. private static int kills, deaths, assists, points;
  35.  
  36. public static int Kills
  37. {
  38. get
  39. {
  40. return kills;
  41. }
  42. set
  43. {
  44. kills = value;
  45. }
  46. }
  47.  
  48. public static int Deaths
  49. {
  50. get
  51. {
  52. return deaths;
  53. }
  54. set
  55. {
  56. deaths = value;
  57. }
  58. }
  59.  
  60. public static int Assists
  61. {
  62. get
  63. {
  64. return assists;
  65. }
  66. set
  67. {
  68. assists = value;
  69. }
  70. }
  71.  
  72. public static int Points
  73. {
  74. get
  75. {
  76. return points;
  77. }
  78. set
  79. {
  80. points = value;
  81. }
  82. }
  83. }
  84.  
  85. public class PlayerProfile
  86. {
  87. public string Nick { get; set; }
  88. public int WinCount { get; set; }
  89. }
  90.  
  91. public class GameInstaller : MonoInstaller
  92. {
  93. public override void InstallBindings()
  94. {
  95. this.Container.Bind<PlayerProfile>()
  96. .ToSelf()
  97. .AsSingle();
  98. }
  99. }
  100.  
  101. public class WinDetector : MonoBehaviour
  102. {
  103. [Inject]
  104. private PlayerProfile playerProfile = null;
  105.  
  106.  
  107. private void OnCollisionEnter(Collision collision)
  108. {
  109. this.playerProfile.WinCount += 1;
  110. // other stuff...
  111. }
  112. }
  113.  
  114. public interface IPlayerProfile
  115. {
  116. string Nick { get; set; }
  117. int WinCount { get; set; }
  118.  
  119. void Save();
  120. void Load();
  121. }
  122.  
  123. [JsonObject]
  124. public class PlayerProfile_Json : IPlayerProfile
  125. {
  126. [JsonProperty]
  127. public string Nick { get; set; }
  128. [JsonProperty]
  129. public int WinCount { get; set; }
  130.  
  131.  
  132. public void Save()
  133. {
  134. ...
  135. }
  136.  
  137. public void Load()
  138. {
  139. ...
  140. }
  141. }
  142.  
  143. [ProtoContract]
  144. public class PlayerProfile_Protobuf : IPlayerProfile
  145. {
  146. [ProtoMember(1)]
  147. public string Nick { get; set; }
  148. [ProtoMember(2)]
  149. public int WinCount { get; set; }
  150.  
  151.  
  152. public void Save()
  153. {
  154. ...
  155. }
  156.  
  157. public void Load()
  158. {
  159. ...
  160. }
  161. }
  162.  
  163. public class GameInstaller : MonoInstaller
  164. {
  165. // The following field can be adjusted using the inspector of the
  166. // installer component (in this case) or asset (in the case of using
  167. // a ScriptableInstaller).
  168. [SerializeField]
  169. private PlayerProfileFormat playerProfileFormat = PlayerProfileFormat.Json;
  170.  
  171.  
  172. public override void InstallBindings()
  173. {
  174. switch (playerProfileFormat) {
  175. case PlayerProfileFormat.Json:
  176. this.Container.Bind<IPlayerProfile>()
  177. .To<PlayerProfile_Json>()
  178. .AsSingle();
  179. break;
  180.  
  181. case PlayerProfileFormat.Protobuf:
  182. this.Container.Bind<IPlayerProfile>()
  183. .To<PlayerProfile_Protobuf>()
  184. .AsSingle();
  185. break;
  186.  
  187. default:
  188. throw new InvalidOperationException("Unexpected player profile format.");
  189. }
  190. }
  191.  
  192.  
  193. public enum PlayerProfileFormat
  194. {
  195. Json,
  196. Protobuf,
  197. }
  198. }
  199.  
  200. using UnityEngine;
  201.  
  202. /// <summary>Manages data for persistance between levels.</summary>
  203. public class DataManager : MonoBehaviour
  204. {
  205. /// <summary>Static reference to the instance of our DataManager</summary>
  206. public static DataManager instance;
  207.  
  208. /// <summary>The player's current score.</summary>
  209. public int score;
  210. /// <summary>The player's remaining health.</summary>
  211. public int health;
  212. /// <summary>The player's remaining lives.</summary>
  213. public int lives;
  214.  
  215. /// <summary>Awake is called when the script instance is being loaded.</summary>
  216. void Awake()
  217. {
  218. // If the instance reference has not been set, yet,
  219. if (instance == null)
  220. {
  221. // Set this instance as the instance reference.
  222. instance = this;
  223. }
  224. else if(instance != this)
  225. {
  226. // If the instance reference has already been set, and this is not the
  227. // the instance reference, destroy this game object.
  228. Destroy(gameObject);
  229. }
  230.  
  231. // Do not destroy this object, when we load a new scene.
  232. DontDestroyOnLoad(gameObject);
  233. }
  234. }
  235.  
  236. using UnityEngine;
  237.  
  238. /// <summary>Manages data for persistance between play sessions.</summary>
  239. public class SaveManager : MonoBehaviour
  240. {
  241. /// <summary>The player's name.</summary>
  242. public string playerName = "";
  243. /// <summary>The player's score.</summary>
  244. public int playerScore = 0;
  245. /// <summary>The player's health value.</summary>
  246. public float playerHealth = 0f;
  247.  
  248. /// <summary>Static record of the key for saving and loading playerName.</summary>
  249. private static string playerNameKey = "PLAYER_NAME";
  250. /// <summary>Static record of the key for saving and loading playerScore.</summary>
  251. private static string playerScoreKey = "PLAYER_SCORE";
  252. /// <summary>Static record of the key for saving and loading playerHealth.</summary>
  253. private static string playerHealthKey = "PLAYER_HEALTH";
  254.  
  255. /// <summary>Saves playerName, playerScore and
  256. /// playerHealth to the PlayerPrefs file.</summary>
  257. public void Save()
  258. {
  259. // Set the values to the PlayerPrefs file using their corresponding keys.
  260. PlayerPrefs.SetString(playerNameKey, playerName);
  261. PlayerPrefs.SetInt(playerScoreKey, playerScore);
  262. PlayerPrefs.SetFloat(playerHealthKey, playerHealth);
  263.  
  264. // Manually save the PlayerPrefs file to disk, in case we experience a crash
  265. PlayerPrefs.Save();
  266. }
  267.  
  268. /// <summary>Saves playerName, playerScore and playerHealth
  269. // from the PlayerPrefs file.</summary>
  270. public void Load()
  271. {
  272. // If the PlayerPrefs file currently has a value registered to the playerNameKey,
  273. if (PlayerPrefs.HasKey(playerNameKey))
  274. {
  275. // load playerName from the PlayerPrefs file.
  276. playerName = PlayerPrefs.GetString(playerNameKey);
  277. }
  278.  
  279. // If the PlayerPrefs file currently has a value registered to the playerScoreKey,
  280. if (PlayerPrefs.HasKey(playerScoreKey))
  281. {
  282. // load playerScore from the PlayerPrefs file.
  283. playerScore = PlayerPrefs.GetInt(playerScoreKey);
  284. }
  285.  
  286. // If the PlayerPrefs file currently has a value registered to the playerHealthKey,
  287. if (PlayerPrefs.HasKey(playerHealthKey))
  288. {
  289. // load playerHealth from the PlayerPrefs file.
  290. playerHealth = PlayerPrefs.GetFloat(playerHealthKey);
  291. }
  292. }
  293.  
  294. /// <summary>Deletes all values from the PlayerPrefs file.</summary>
  295. public void Delete()
  296. {
  297. // Delete all values from the PlayerPrefs file.
  298. PlayerPrefs.DeleteAll();
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement