Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class GameController : MonoBehaviour {
- // Make global
- public static GameController Instance {
- get;
- set;
- }
- void Awake () {
- DontDestroyOnLoad (transform.gameObject);
- Instance = this;
- }
- void Start() {
- //Load first game scene (probably main menu)
- Application.LoadLevel(2);
- }
- // Data persisted between scenes
- public int exp = 0;
- public int armor = 0;
- public int weapon = 0;
- //...
- }
- private GameController gameController = GameController.Instance;
- public static class PlayerStats
- {
- private static int kills, deaths, assists, points;
- public static int Kills
- {
- get
- {
- return kills;
- }
- set
- {
- kills = value;
- }
- }
- public static int Deaths
- {
- get
- {
- return deaths;
- }
- set
- {
- deaths = value;
- }
- }
- public static int Assists
- {
- get
- {
- return assists;
- }
- set
- {
- assists = value;
- }
- }
- public static int Points
- {
- get
- {
- return points;
- }
- set
- {
- points = value;
- }
- }
- }
- public class PlayerProfile
- {
- public string Nick { get; set; }
- public int WinCount { get; set; }
- }
- public class GameInstaller : MonoInstaller
- {
- public override void InstallBindings()
- {
- this.Container.Bind<PlayerProfile>()
- .ToSelf()
- .AsSingle();
- }
- }
- public class WinDetector : MonoBehaviour
- {
- [Inject]
- private PlayerProfile playerProfile = null;
- private void OnCollisionEnter(Collision collision)
- {
- this.playerProfile.WinCount += 1;
- // other stuff...
- }
- }
- public interface IPlayerProfile
- {
- string Nick { get; set; }
- int WinCount { get; set; }
- void Save();
- void Load();
- }
- [JsonObject]
- public class PlayerProfile_Json : IPlayerProfile
- {
- [JsonProperty]
- public string Nick { get; set; }
- [JsonProperty]
- public int WinCount { get; set; }
- public void Save()
- {
- ...
- }
- public void Load()
- {
- ...
- }
- }
- [ProtoContract]
- public class PlayerProfile_Protobuf : IPlayerProfile
- {
- [ProtoMember(1)]
- public string Nick { get; set; }
- [ProtoMember(2)]
- public int WinCount { get; set; }
- public void Save()
- {
- ...
- }
- public void Load()
- {
- ...
- }
- }
- public class GameInstaller : MonoInstaller
- {
- // The following field can be adjusted using the inspector of the
- // installer component (in this case) or asset (in the case of using
- // a ScriptableInstaller).
- [SerializeField]
- private PlayerProfileFormat playerProfileFormat = PlayerProfileFormat.Json;
- public override void InstallBindings()
- {
- switch (playerProfileFormat) {
- case PlayerProfileFormat.Json:
- this.Container.Bind<IPlayerProfile>()
- .To<PlayerProfile_Json>()
- .AsSingle();
- break;
- case PlayerProfileFormat.Protobuf:
- this.Container.Bind<IPlayerProfile>()
- .To<PlayerProfile_Protobuf>()
- .AsSingle();
- break;
- default:
- throw new InvalidOperationException("Unexpected player profile format.");
- }
- }
- public enum PlayerProfileFormat
- {
- Json,
- Protobuf,
- }
- }
- using UnityEngine;
- /// <summary>Manages data for persistance between levels.</summary>
- public class DataManager : MonoBehaviour
- {
- /// <summary>Static reference to the instance of our DataManager</summary>
- public static DataManager instance;
- /// <summary>The player's current score.</summary>
- public int score;
- /// <summary>The player's remaining health.</summary>
- public int health;
- /// <summary>The player's remaining lives.</summary>
- public int lives;
- /// <summary>Awake is called when the script instance is being loaded.</summary>
- void Awake()
- {
- // If the instance reference has not been set, yet,
- if (instance == null)
- {
- // Set this instance as the instance reference.
- instance = this;
- }
- else if(instance != this)
- {
- // If the instance reference has already been set, and this is not the
- // the instance reference, destroy this game object.
- Destroy(gameObject);
- }
- // Do not destroy this object, when we load a new scene.
- DontDestroyOnLoad(gameObject);
- }
- }
- using UnityEngine;
- /// <summary>Manages data for persistance between play sessions.</summary>
- public class SaveManager : MonoBehaviour
- {
- /// <summary>The player's name.</summary>
- public string playerName = "";
- /// <summary>The player's score.</summary>
- public int playerScore = 0;
- /// <summary>The player's health value.</summary>
- public float playerHealth = 0f;
- /// <summary>Static record of the key for saving and loading playerName.</summary>
- private static string playerNameKey = "PLAYER_NAME";
- /// <summary>Static record of the key for saving and loading playerScore.</summary>
- private static string playerScoreKey = "PLAYER_SCORE";
- /// <summary>Static record of the key for saving and loading playerHealth.</summary>
- private static string playerHealthKey = "PLAYER_HEALTH";
- /// <summary>Saves playerName, playerScore and
- /// playerHealth to the PlayerPrefs file.</summary>
- public void Save()
- {
- // Set the values to the PlayerPrefs file using their corresponding keys.
- PlayerPrefs.SetString(playerNameKey, playerName);
- PlayerPrefs.SetInt(playerScoreKey, playerScore);
- PlayerPrefs.SetFloat(playerHealthKey, playerHealth);
- // Manually save the PlayerPrefs file to disk, in case we experience a crash
- PlayerPrefs.Save();
- }
- /// <summary>Saves playerName, playerScore and playerHealth
- // from the PlayerPrefs file.</summary>
- public void Load()
- {
- // If the PlayerPrefs file currently has a value registered to the playerNameKey,
- if (PlayerPrefs.HasKey(playerNameKey))
- {
- // load playerName from the PlayerPrefs file.
- playerName = PlayerPrefs.GetString(playerNameKey);
- }
- // If the PlayerPrefs file currently has a value registered to the playerScoreKey,
- if (PlayerPrefs.HasKey(playerScoreKey))
- {
- // load playerScore from the PlayerPrefs file.
- playerScore = PlayerPrefs.GetInt(playerScoreKey);
- }
- // If the PlayerPrefs file currently has a value registered to the playerHealthKey,
- if (PlayerPrefs.HasKey(playerHealthKey))
- {
- // load playerHealth from the PlayerPrefs file.
- playerHealth = PlayerPrefs.GetFloat(playerHealthKey);
- }
- }
- /// <summary>Deletes all values from the PlayerPrefs file.</summary>
- public void Delete()
- {
- // Delete all values from the PlayerPrefs file.
- PlayerPrefs.DeleteAll();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement